|
From: <pe...@us...> - 2003-12-01 15:44:58
|
Update of /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/sql
In directory sc8-pr-cvs1:/tmp/cvs-serv24187/src/java/org/neuclear/commons/sql
Modified Files:
DefaultConnectionSource.java JNDIConnectionSource.java
SQLTools.java
Added Files:
SQLContext.java XAConnectionSource.java
Removed Files:
SimpleConnectionSource.java ThreadMappedConnectionSource.java
Log Message:
Added XAConnectionSources and Transaction capability through jotm.
--- NEW FILE: SQLContext.java ---
package org.neuclear.commons.sql;
import java.sql.Connection;
import java.sql.SQLException;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
/*
NeuClear Distributed Transaction Clearing Platform
(C) 2003 Pelle Braendgaard
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id: SQLContext.java,v 1.1 2003/12/01 15:44:53 pelle Exp $
$Log: SQLContext.java,v $
Revision 1.1 2003/12/01 15:44:53 pelle
Added XAConnectionSources and Transaction capability through jotm.
*/
/**
* A thread mapped wrapper around a ConnectionSource. The same thread will always
* receive the same connection source. A new Connection is created if none exist.
* The threads connection can be specifically closed with close()
*/
public class SQLContext implements ConnectionSource{
public SQLContext(ConnectionSource source) {
this.source = source;
this.map=new HashMap();
}
/**
* Gets the threads Connection or creates a new one if one doesnt exist.
* @return
* @throws SQLException
* @throws IOException
*/
public Connection getConnection() throws SQLException, IOException {
Connection con= (Connection)map.get(Thread.currentThread());
if (con==null||con.isClosed()){
con=source.getConnection();
map.put(Thread.currentThread(),con);
}
return con;
}
/**
* if the thread has an open connection it closes it.
* @throws SQLException
*/
public void close() throws SQLException {
Connection con= (Connection)map.get(Thread.currentThread());
if (con!=null||!con.isClosed())
map.remove(Thread.currentThread());
con.close();
}
private final ConnectionSource source;
private final Map map;
}
--- NEW FILE: XAConnectionSource.java ---
package org.neuclear.commons.sql;
import org.enhydra.jdbc.standard.StandardXADataSource;
import org.neuclear.commons.NeuClearException;
import javax.sql.XADataSource;
import javax.sql.XAConnection;
import javax.naming.NamingException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
/**
*
* User: pelleb
* Date: Aug 6, 2003
* Time: 3:38:50 PM
*/
public final class XAConnectionSource implements ConnectionSource {
public XAConnectionSource(final String driver, final String url, final String user, final String password) throws SQLException, NeuClearException, NamingException{
SQLTools.loadDefaultContext();
try {
Class.forName(driver).newInstance();
} catch (InstantiationException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
this.user=user;
this.password=password;
xads = new StandardXADataSource();
((StandardXADataSource) xads).setDriverName(driver);
((StandardXADataSource) xads).setUrl(url);
((StandardXADataSource) xads).setTransactionManager(SQLTools.getTransactionManager());
}
public Connection getConnection() throws SQLException, IOException {
return xads.getXAConnection(user,password).getConnection();
}
private final String user;
private final String password;
private final XADataSource xads;
// private XAConnection xaConnection;
}
Index: DefaultConnectionSource.java
===================================================================
RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/sql/DefaultConnectionSource.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** DefaultConnectionSource.java 21 Nov 2003 04:43:42 -0000 1.2
--- DefaultConnectionSource.java 1 Dec 2003 15:44:53 -0000 1.3
***************
*** 1,4 ****
--- 1,7 ----
package org.neuclear.commons.sql;
+ import org.neuclear.commons.NeuClearException;
+
+ import javax.naming.NamingException;
import java.sql.Connection;
import java.sql.SQLException;
***************
*** 7,10 ****
--- 10,14 ----
import java.util.Map;
import java.util.HashMap;
+ import java.util.Properties;
/**
***************
*** 14,23 ****
* Time: 3:38:50 PM
*/
! public final class DefaultConnectionSource extends ThreadMappedConnectionSource {
! public final Connection createConnection() throws SQLException, IOException {
! return SQLTools.getConnection();
}
}
--- 18,37 ----
* Time: 3:38:50 PM
*/
! public final class DefaultConnectionSource implements ConnectionSource {
! public DefaultConnectionSource() throws SQLException, NeuClearException, IOException, NamingException {
! final Properties props=SQLTools.loadProperties();
! cs = new XAConnectionSource(
! props.getProperty("jdbc.class"),
! props.getProperty("jdbc.url"),
! props.getProperty("jdbc.username"),
! props.getProperty("jdbc.password")
! );
! }
! public Connection getConnection() throws SQLException, IOException {
! return cs.getConnection();
}
+ private final ConnectionSource cs;
}
Index: JNDIConnectionSource.java
===================================================================
RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/sql/JNDIConnectionSource.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** JNDIConnectionSource.java 28 Nov 2003 00:12:13 -0000 1.3
--- JNDIConnectionSource.java 1 Dec 2003 15:44:53 -0000 1.4
***************
*** 28,31 ****
--- 28,34 ----
$Id$
$Log$
+ Revision 1.4 2003/12/01 15:44:53 pelle
+ Added XAConnectionSources and Transaction capability through jotm.
+
Revision 1.3 2003/11/28 00:12:13 pelle
Getting the NeuClear web transactions working.
***************
*** 43,49 ****
/**
! * User: pelleb
! * Date: Nov 18, 2003
! * Time: 6:09:37 PM
*/
public final class JNDIConnectionSource implements ConnectionSource {
--- 46,52 ----
/**
! * The JNDIConnectionSource takes as a constructer the name of a JNDI connection source.
! * While not required, for most applications this should implement XA and there should also be
! * a JTA UserTransaction available via JNDI
*/
public final class JNDIConnectionSource implements ConnectionSource {
Index: SQLTools.java
===================================================================
RCS file: /cvsroot/neuclear/neuclear-commons/src/java/org/neuclear/commons/sql/SQLTools.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** SQLTools.java 21 Nov 2003 04:43:42 -0000 1.2
--- SQLTools.java 1 Dec 2003 15:44:54 -0000 1.3
***************
*** 1,4 ****
--- 1,15 ----
package org.neuclear.commons.sql;
+ import org.neuclear.commons.NeuClearException;
+ import org.objectweb.jotm.Jotm;
+ import org.objectweb.carol.util.configuration.CarolConfiguration;
+ import org.objectweb.carol.util.configuration.RMIConfigurationException;
+
+ import javax.transaction.UserTransaction;
+ import javax.transaction.TransactionManager;
+ import javax.transaction.SystemException;
+ import javax.naming.InitialContext;
+ import javax.naming.Context;
+ import javax.naming.NamingException;
import java.sql.SQLException;
import java.sql.DriverManager;
***************
*** 42,46 ****
! private static Properties loadProperties() throws IOException {
final Properties props=new Properties();
final File propsFile= new File(
--- 53,57 ----
! static Properties loadProperties() throws IOException {
final Properties props=new Properties();
final File propsFile= new File(
***************
*** 63,65 ****
--- 74,121 ----
return new Timestamp(date.getTime());
}
+ public final static UserTransaction getUserTransaction() throws NamingException,SystemException {
+ Context ctx = new InitialContext();
+ UserTransaction ut = null;
+ try {
+ ut = (UserTransaction)ctx.lookup(USERXACT);
+ } catch (NamingException e) {
+ getTransactionManager().getTransaction();
+ return jotm.getUserTransaction();
+
+ }
+ return ut;
+
+ }
+
+ public final static synchronized TransactionManager getTransactionManager() throws NamingException {
+ if (jotm==null){
+ Context ctx = new InitialContext();
+ jotm=new Jotm(true,false);
+ ctx.rebind(USERXACT,jotm.getUserTransaction());
+ }
+ return jotm.getTransactionManager();
+
+ }
+ final static void loadDefaultContext(){
+ try{
+ System.setProperty("java.naming.factory.initial","org.objectweb.carol.jndi.spi.MultiOrbInitialContextFactory");
+ Context ctx=new InitialContext();
+ } catch (Exception ex){
+
+ try {
+ CarolConfiguration.init();
+ getTransactionManager();
+ } catch (RMIConfigurationException e) {
+ throw new RuntimeException(e);
+ } catch (NamingException e) {
+ throw new RuntimeException(e) ;
+
+ }
+ }
+ }
+
+
+ private static Jotm jotm;
+ public static final String USERXACT = "java:comp/UserTransaction";
+
}
--- SimpleConnectionSource.java DELETED ---
--- ThreadMappedConnectionSource.java DELETED ---
|