User: lqd
Date: 02/03/04 15:35:08
Modified: src/main/org/jboss/pool/jdbc/xa Tag: Branch_2_4
XAConnectionFactory.java XAPoolDataSource.java
XAPoolDriver.java
Log:
- more changes from proprietary PrintWriter logging to log4j
- no more NPE at startup
Revision Changes Path
No revision
No revision
1.2.2.2 +91 -65
jbosspool/src/main/org/jboss/pool/jdbc/xa/Attic/XAConnectionFactory.java
Index: XAConnectionFactory.java
===================================================================
RCS file:
/cvsroot/jboss/jbosspool/src/main/org/jboss/pool/jdbc/xa/Attic/XAConnectionFactory.java,v
retrieving revision 1.2.2.1
retrieving revision 1.2.2.2
diff -u -r1.2.2.1 -r1.2.2.2
--- XAConnectionFactory.java 27 Feb 2002 08:14:03 -0000 1.2.2.1
+++ XAConnectionFactory.java 4 Mar 2002 23:35:08 -0000 1.2.2.2
@@ -3,7 +3,6 @@
*/
package org.jboss.pool.jdbc.xa;
-import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import javax.sql.ConnectionEvent;
@@ -21,6 +20,7 @@
import org.jboss.pool.jdbc.xa.wrapper.TransactionListener;
import org.jboss.pool.jdbc.xa.wrapper.XAConnectionImpl;
import org.jboss.pool.jdbc.xa.wrapper.XAResourceImpl;
+import org.jboss.logging.Logger;
/**
* Object factory for JDBC 2.0 standard extension XAConnections. You pool the
@@ -42,7 +42,9 @@
* REVISIONS:
* 20010703 bill added code for transaction isolation
*/
-public class XAConnectionFactory extends PoolObjectFactory {
+public class XAConnectionFactory
+ extends PoolObjectFactory
+{
public static final int DEFAULT_ISOLATION = -1;
private InitialContext ctx;
@@ -55,10 +57,11 @@
private ConnectionEventListener listener, errorListener;
private TransactionListener transListener;
private ObjectPool pool;
- private PrintWriter log;
private HashMap wrapperTx, rms;
private TransactionManager tm;
+ private static Logger log = Logger.getLogger( XAConnectionFactory.class );
+
/**
* Creates a new factory. You must set the XADataSource and
* TransactionManager JNDI name before the factory can be used.
@@ -105,8 +108,7 @@
rms.remove(con);
}
} catch(Exception e) {
- if(log != null)
- e.printStackTrace(log);
+ log.error( "Unable to deregister with TransactionManager", e );
throw new RuntimeException("Unable to deregister with
TransactionManager: "+e);
}
@@ -241,9 +243,12 @@
/**
* Verifies that the data source and transaction manager are accessible.
*/
- public void poolStarted(ObjectPool pool, PrintWriter log) {
+ public void poolStarted(ObjectPool pool)
+ {
+ if( log.isDebugEnabled() )
+ log.debug( "Starting" );
+
super.poolStarted(pool);
- this.log = log;
this.pool = pool;
if(source == null)
throw new IllegalStateException("Must specify XADataSource to
"+getClass().getName());
@@ -258,68 +263,86 @@
}
}
- /**
- * Creates a new XAConnection from the provided XADataSource.
- */
- public Object createObject(Object parameters) {
- try {
- if(userName != null && userName.length() > 0)
- return source.getXAConnection(userName, password);
- else
- return source.getXAConnection();
- } catch(SQLException e) {
- if(log != null)
- e.printStackTrace(log);
- }
- return null;
- }
+ /**
+ * Creates a new XAConnection from the provided XADataSource.
+ */
+ public Object createObject(Object parameters)
+ {
+ if( log.isTraceEnabled() )
+ log.trace( "Opening new XAConnection" );
+
+ try
+ {
+ if(userName != null && userName.length() > 0)
+ return source.getXAConnection(userName, password);
+ else
+ return source.getXAConnection();
+ }
+ catch(SQLException e)
+ {
+ log.error( "Can't get an XAConnection", e );
+ }
+ return null;
+ }
- /**
- * Registers the XAConnection's XAResource with the current transaction (if
- * there is one). Sets listeners that will handle deregistering and
- * returning the XAConnection to the pool via callbacks.
- */
- public Object prepareObject(Object pooledObject) {
- XAConnection con = (XAConnection)pooledObject;
- con.addConnectionEventListener(listener);
- Transaction trans = null;
- try {
- if(tm.getStatus() != Status.STATUS_NO_TRANSACTION) {
- trans = tm.getTransaction();
- XAResource res = con.getXAResource();
- trans.enlistResource(res);
- rms.put(con, res);
- if(log != null) log.println("Resource '"+res+"' enlisted for
'"+con+"'.");
- } else {
- if(log != null) log.println("No transaction right now.");
- }
- } catch(Exception e) {
- if(log != null)
- e.printStackTrace(log);
- con.removeConnectionEventListener(listener);
- throw new RuntimeException("Unable to register with TransactionManager:
"+e);
- }
- if(con instanceof XAConnectionImpl) {
- ((XAConnectionImpl)con).setTransactionListener(transListener);
- ((XAConnectionImpl)con).setPSCacheSize(psCacheSize);
- if (transactionIsolation != DEFAULT_ISOLATION)
+ /**
+ * Registers the XAConnection's XAResource with the current transaction (if
+ * there is one). Sets listeners that will handle deregistering and
+ * returning the XAConnection to the pool via callbacks.
+ */
+ public Object prepareObject(Object pooledObject)
+ {
+ boolean debug = log.isDebugEnabled();
+ XAConnection con = (XAConnection)pooledObject;
+ con.addConnectionEventListener(listener);
+ Transaction trans = null;
+ try
+ {
+ if(tm.getStatus() != Status.STATUS_NO_TRANSACTION)
+ {
+ trans = tm.getTransaction();
+ XAResource res = con.getXAResource();
+ trans.enlistResource(res);
+ rms.put(con, res);
+ if( debug )
+ log.debug( "Resource '" + res + "' enlisted for '" + con + "'." );
+ } else
+ {
+ if( debug )
+ log.debug( "No transaction right now." );
+ }
+ }
+ catch(Exception e)
+ {
+ log.error( "Unable to register with TransactionManager", e );
+ con.removeConnectionEventListener(listener);
+ throw new RuntimeException("Unable to register with TransactionManager:
"+e);
+ }
+
+ if(con instanceof XAConnectionImpl)
+ {
+ ((XAConnectionImpl)con).setTransactionListener(transListener);
+ ((XAConnectionImpl)con).setPSCacheSize(psCacheSize);
+ if (transactionIsolation != DEFAULT_ISOLATION)
+ {
+ try
{
- try
- {
-
((XAConnectionImpl)con).setTransactionIsolation(transactionIsolation);
- }
- catch (SQLException sex)
- {
- throw new RuntimeException("Unable to setTransactionIsolation: "
+ sex.getMessage());
- }
+
((XAConnectionImpl)con).setTransactionIsolation(transactionIsolation);
}
- if(trans != null) {
- wrapperTx.put(con, trans); // For JDBC 1/2 wrappers, remember which
- wrapperTx.put(trans, con); // connection goes with a given
transaction
+ catch (SQLException sex)
+ {
+ throw new RuntimeException("Unable to setTransactionIsolation: " +
sex.getMessage());
}
- }
- return con;
- }
+ }
+
+ if(trans != null)
+ {
+ wrapperTx.put(con, trans); // For JDBC 1/2 wrappers, remember which
+ wrapperTx.put(trans, con); // connection goes with a given transaction
+ }
+ }
+ return con;
+ }
/**
* Closes a connection.
@@ -346,3 +369,6 @@
return null;
}
}
+/*
+vim:tabstop=3:et:shiftwidth=3
+*/
1.3.2.2 +76 -26
jbosspool/src/main/org/jboss/pool/jdbc/xa/Attic/XAPoolDataSource.java
Index: XAPoolDataSource.java
===================================================================
RCS file:
/cvsroot/jboss/jbosspool/src/main/org/jboss/pool/jdbc/xa/Attic/XAPoolDataSource.java,v
retrieving revision 1.3.2.1
retrieving revision 1.3.2.2
diff -u -r1.3.2.1 -r1.3.2.2
--- XAPoolDataSource.java 27 Feb 2002 08:14:03 -0000 1.3.2.1
+++ XAPoolDataSource.java 4 Mar 2002 23:35:08 -0000 1.3.2.2
@@ -5,12 +5,19 @@
import java.sql.Connection;
import java.io.PrintWriter;
-import java.util.*;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.HashSet;
import javax.sql.*;
import javax.naming.*;
import javax.naming.spi.*;
+
import org.jboss.pool.ObjectPool;
+import org.jboss.logging.Logger;
+import org.jboss.logging.log4j.CategoryWriter;
+
/**
* DataSource for transactional JDBC pools. This handles configuration
* parameters for both the pool and the JDBC connection. It is important that
@@ -29,7 +36,10 @@
* 20010701 danch added code for timeout in blocking.
* 20010703 bill added code for transaction isolation and setting the ps cache size
*/
-public class XAPoolDataSource implements DataSource, Referenceable, ObjectFactory {
+public class XAPoolDataSource
+ implements DataSource, Referenceable, ObjectFactory
+{
+ private static Logger log = Logger.getLogger( XAPoolDataSource.class );
private static HashMap sources = new HashMap();
/**
* Gets all the current JDBC pool data sources.
@@ -44,7 +54,6 @@
return (XAPoolDataSource)sources.get(poolName);
}
-
private ObjectPool pool;
private XAConnectionFactory factory;
private PrintWriter logWriter;
@@ -56,12 +65,16 @@
* Creates a new XA pool data source. Be sure to configure it and then
* call initialize before you try to use it.
*/
- public XAPoolDataSource() {
+ public XAPoolDataSource()
+ {
+ log.info( "Creating XA Pool" );
pool = new ObjectPool();
- try {
+ try
+ {
factory = new XAConnectionFactory();
- } catch(NamingException e) {
- e.printStackTrace();
+ } catch(NamingException e)
+ {
+ log.error( "Can't get XAConnectionFactory", e);
}
XAPoolDriver.instance();
}
@@ -72,7 +85,12 @@
* using the default InitialContext. You can also do this manually if you
* have additional requirements.
*/
- public void setJNDIName(String name) throws NamingException {
+ public void setJNDIName(String name)
+ throws NamingException
+ {
+ if( log.isDebugEnabled() )
+ log.debug( "Binding to JNDI name " + name );
+
InitialContext ctx = new InitialContext();
if(jndiName != null && !jndiName.equals(name))
ctx.unbind(jndiName);
@@ -86,7 +104,10 @@
* setJNDIName to bind it.
* @see #setJNDIName
*/
- public String getJNDIName() {return jndiName;}
+ public String getJNDIName()
+ {
+ return jndiName;
+ }
// XA properties
public void setDataSource(XADataSource ds) {factory.setDataSource(ds);}
@@ -133,13 +154,14 @@
public void setTimestampUsed(boolean timestamp)
{pool.setTimestampUsed(timestamp);}
public boolean isTimestampUsed() {return pool.isTimestampUsed();}
- // Other methods
+ // Other methods
- /**
- * Initializes the pool. You need to have configured all the pool and
- * XA properties first.
- */
- public void initialize() {
+ /**
+ * Initializes the pool. You need to have configured all the pool and
+ * XA properties first.
+ */
+ public void initialize()
+ {
initialized = true;
pool.setObjectFactory(factory);
pool.initialize();
@@ -149,7 +171,8 @@
* Returns a string describing the pool status (number of connections
* created, used, and maximum).
*/
- public String getPoolStatus() {
+ public String getPoolStatus()
+ {
return pool.toString();
}
@@ -157,12 +180,17 @@
* Shuts down this data source and the underlying pool. If you used
* setJNDI name to bind it in JNDI, it is unbound.
*/
- public void close() {
- try {
+ public void close()
+ {
+ if( log.isDebugEnabled() )
+ log.debug( "Closing DataSource");
+
+ try
+ {
setJNDIName(null);
- } catch(NamingException e) {
- if(logWriter != null)
- e.printStackTrace(logWriter);
+ } catch(NamingException e)
+ {
+ log.warn( "Can't unbind from JNDI", e );
}
sources.remove(pool.getName());
pool.shutDown();
@@ -173,8 +201,12 @@
/**
* Gets a connection from the pool.
*/
- public Connection getConnection() throws java.sql.SQLException {
+ public Connection getConnection()
+ throws java.sql.SQLException
+ {
if(!initialized) initialize();
+ if( log.isTraceEnabled() )
+ log.trace( "Getting a Connection" );
return ((XAConnection)pool.getObject()).getConnection();
}
@@ -186,6 +218,8 @@
*/
public Connection getConnection(String user, String password) throws
java.sql.SQLException {
if(!initialized) initialize();
+ if( log.isTraceEnabled() )
+ log.trace( "Getting a connection for user " + user + " with password " +
password );
factory.setUser(user);
factory.setPassword(password);
return ((XAConnection)pool.getObject()).getConnection();
@@ -194,16 +228,29 @@
/**
* Gets a log writer used to record pool events.
*/
- public PrintWriter getLogWriter() throws java.sql.SQLException {
+ public PrintWriter getLogWriter()
+ throws java.sql.SQLException
+ {
return logWriter;
}
/**
* Sets a log writer used to record pool events.
*/
- public void setLogWriter(PrintWriter writer) throws java.sql.SQLException {
- logWriter = writer;
- //pool.setLogWriter(writer);
+ public void setLogWriter(PrintWriter writer)
+ throws java.sql.SQLException
+ {
+ if( writer == null )
+ {
+ logWriter = null;
+ }
+ else
+ {
+ if( logWriter == null )
+ {
+ logWriter = new CategoryWriter( log.getCategory() );
+ }
+ }
}
/**
@@ -244,3 +291,6 @@
return null;
}
}
+/*
+vim:tabstop=3:et:shiftwidth=3
+*/
1.1.1.1.2.1 +107 -81
jbosspool/src/main/org/jboss/pool/jdbc/xa/Attic/XAPoolDriver.java
Index: XAPoolDriver.java
===================================================================
RCS file:
/cvsroot/jboss/jbosspool/src/main/org/jboss/pool/jdbc/xa/Attic/XAPoolDriver.java,v
retrieving revision 1.1.1.1
retrieving revision 1.1.1.1.2.1
diff -u -r1.1.1.1 -r1.1.1.1.2.1
--- XAPoolDriver.java 15 May 2001 07:58:24 -0000 1.1.1.1
+++ XAPoolDriver.java 4 Mar 2002 23:35:08 -0000 1.1.1.1.2.1
@@ -7,6 +7,8 @@
import java.util.Properties;
import javax.sql.*;
+import org.jboss.logging.Logger;
+
/**
* JDBC Driver to access pooled JDBC connections. Supports JDBC 2.0
* transactional XAConnections. You will get a java.sql.Connection back
@@ -23,85 +25,109 @@
*
* @author Aaron Mulder ([EMAIL PROTECTED])
*/
-public class XAPoolDriver implements Driver {
- private final static String URL_START = "jdbc:minerva:xa:";
- private final static XAPoolDriver instance;
- static {
- instance = new XAPoolDriver();
- try {
- DriverManager.registerDriver(XAPoolDriver.instance());
- } catch(SQLException e) {
- System.err.println("Unable to register Minerva XA DB pool driver!");
- e.printStackTrace();
- }
- }
-
- /**
- * Gets the singleton driver instance.
- */
- public static XAPoolDriver instance() {
- return instance;
- }
-
- private XAPoolDriver() {
- }
-
- /**
- * Tells which URLs this driver can handle.
- */
- public boolean acceptsURL(String url) throws java.sql.SQLException {
- return url.startsWith(URL_START);
- }
-
- /**
- * Retrieves a connection from a connection pool.
- */
- public Connection connect(String url, Properties props) throws
java.sql.SQLException {
- if(url.startsWith(URL_START)) {
- return getXAConnection(url.substring(URL_START.length()));
- }
- return null; // No SQL Exception here!
- }
-
- private Connection getXAConnection(String name) {
- Connection con = null;
- try {
- DataSource source = XAPoolDataSource.getDataSource(name);
- if(source != null)
- con = source.getConnection();
- } catch(Exception e) {
- e.printStackTrace();
- }
- return con;
- }
-
- /**
- * Returns the driver version.
- */
- public int getMajorVersion() {
- return 2;
- }
-
- /**
- * Returns the driver version.
- */
- public int getMinorVersion() {
- return 0;
- }
-
- /**
- * Returns no properties. You do not need properties to connect to the
- * pool, and the properties for the underlying driver are not managed here.
- */
- public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws
SQLException {
- return new DriverPropertyInfo[0];
- }
-
- /**
- * Returns <B>false</B> since it is not known which underlying driver will
- * be used and what its capabilities are.
- */
- public boolean jdbcCompliant() {
- return false;
- }
+
+public class XAPoolDriver implements Driver
+{
+ private final static Logger log = Logger.getLogger( XAPoolDriver.class );
+ private final static String URL_START = "jdbc:minerva:xa:";
+ private final static XAPoolDriver instance;
+
+ static
+ {
+ instance = new XAPoolDriver();
+ try
+ {
+ DriverManager.registerDriver(XAPoolDriver.instance());
+ } catch(SQLException e)
+ {
+ log.error( "Unable to register Minerva XA DB pool driver!", e );
+ }
+ }
+
+ /**
+ * Gets the singleton driver instance.
+ */
+ public static XAPoolDriver instance()
+ {
+ return instance;
+ }
+
+ private XAPoolDriver()
+ {
+ }
+
+ /**
+ * Tells which URLs this driver can handle.
+ */
+ public boolean acceptsURL(String url)
+ throws java.sql.SQLException
+ {
+ return url.startsWith(URL_START);
+ }
+
+ /**
+ * Retrieves a connection from a connection pool.
+ */
+ public Connection connect(String url, Properties props)
+ throws java.sql.SQLException
+ {
+ if(url.startsWith(URL_START))
+ {
+ return getXAConnection(url.substring(URL_START.length()));
+ }
+ return null; // No SQL Exception here!
+ }
+
+ private Connection getXAConnection(String name)
+ {
+ Connection con = null;
+ try
+ {
+ DataSource source = XAPoolDataSource.getDataSource(name);
+ if(source != null)
+ con = source.getConnection();
+ } catch(Exception e)
+ {
+ log.error( "Can't get DataSource from XAPool", e );
+ }
+ return con;
+ }
+
+ /**
+ * Returns the driver version.
+ */
+ public int getMajorVersion()
+ {
+ return 2;
+ }
+
+ /**
+ * Returns the driver version.
+ */
+ public int getMinorVersion()
+ {
+ return 0;
+ }
+
+ /**
+ * Returns no properties. You do not need properties to connect to the
+ * pool, and the properties for the underlying driver are not managed here.
+ */
+ public DriverPropertyInfo[] getPropertyInfo(String url, Properties info)
+ throws SQLException
+ {
+ return new DriverPropertyInfo[0];
+ }
+
+ /**
+ * Returns <B>false</B> since it is not known which underlying driver will
+ * be used and what its capabilities are.
+ */
+ public boolean jdbcCompliant()
+ {
+ return false;
+ }
}
+/*
+vim:tabstop=3:et:shiftwidth=3
+*/
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development