User: lqd
Date: 02/03/04 15:35:08
Modified: src/main/org/jboss/pool/jdbc Tag: Branch_2_4
JDBCConnectionFactory.java JDBCPoolDataSource.java
Log:
- more changes from proprietary PrintWriter logging to log4j
- no more NPE at startup
Revision Changes Path
No revision
No revision
1.1.1.1.2.2 +231 -167
jbosspool/src/main/org/jboss/pool/jdbc/Attic/JDBCConnectionFactory.java
Index: JDBCConnectionFactory.java
===================================================================
RCS file:
/cvsroot/jboss/jbosspool/src/main/org/jboss/pool/jdbc/Attic/JDBCConnectionFactory.java,v
retrieving revision 1.1.1.1.2.1
retrieving revision 1.1.1.1.2.2
diff -u -r1.1.1.1.2.1 -r1.1.1.1.2.2
--- JDBCConnectionFactory.java 27 Feb 2002 08:14:03 -0000 1.1.1.1.2.1
+++ JDBCConnectionFactory.java 4 Mar 2002 23:35:08 -0000 1.1.1.1.2.2
@@ -3,13 +3,15 @@
*/
package org.jboss.pool.jdbc;
-import java.io.PrintWriter;
import java.sql.*;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.jboss.pool.cache.ObjectCache;
-import org.jboss.pool.*;
+import org.jboss.pool.ObjectPool;
+import org.jboss.pool.PoolObjectFactory;
+
+import org.jboss.logging.Logger;
/**
* Object factory that creates java.sql.Connections. This is meant for use
@@ -20,169 +22,231 @@
*
* @author Aaron Mulder ([EMAIL PROTECTED])
*/
-public class JDBCConnectionFactory extends PoolObjectFactory {
- private String url;
- private Properties props;
- private String userName;
- private String password;
- private PrintWriter log;
- private int psCacheSize = 10;
- private ObjectPool pool;
-
- /**
- * Creates a new factory. You must configure it with JDBC properties
- * before you can use it.
- */
- public JDBCConnectionFactory() {
- }
-
- /**
- * Sets the JDBC URL used to create new connections.
- */
- public void setConnectURL(String url) {this.url = url;}
-
- /**
- * Gets the JDBC URL used to create new connections.
- */
- public String getConnectURL() {return url;}
-
- /**
- * Sets the JDBC Propeties used to create new connections.
- * This is optional, and will only be used if present.
- */
- public void setConnectProperties(Properties props) {this.props = props;}
-
- /**
- * Gets the JDBC Properties used to create new connections.
- */
- public Properties getConnectProperties() {return props;}
-
- /**
- * Sets the JDBC user name used to create new connections.
- * This is optional, and will only be used if present.
- */
- public void setUser(String userName) {this.userName = userName;}
-
- /**
- * Gets the JDBC user name used to create new connections.
- */
- public String getUser() {return userName;}
-
- /**
- * Sets the JDBC password used to create new connections.
- * This is optional, and will only be used if present.
- */
- public void setPassword(String password) {this.password = password;}
-
- /**
- * Gets the JDBC password used to create new connections.
- */
- public String getPassword() {return password;}
-
- /**
- * Sets the number of PreparedStatements to be cached for each
- * Connection. Your DB product may impose a limit on the number
- * of open PreparedStatements. The default value is 10.
- */
- public void setPSCacheSize(int size) {
- psCacheSize = size;
- }
-
- /**
- * Gets the number of PreparedStatements to be cached for each
- * Connection. The default value is 10.
- */
- public int getPSCacheSize() {
- return psCacheSize;
- }
-
- /**
- * Validates that connection properties were set (at least a URL).
- */
- public void poolStarted(ObjectPool pool, PrintWriter log) {
- super.poolStarted(pool);
- if(url == null)
- throw new IllegalStateException("Must specify JDBC connection URL to
"+getClass().getName());
- this.pool = pool;
- }
-
- /**
- * Cleans up.
- */
- public void poolClosing(ObjectPool pool) {
- super.poolClosing(pool);
- this.pool = null;
- log = null;
- }
-
- /**
- * Creates a new JDBC Connection.
- */
- public Object createObject(Object parameters) {
- try {
- if(userName != null && userName.length() > 0)
- return DriverManager.getConnection(url, userName, password);
- else if(props != null)
- return DriverManager.getConnection(url, props);
- else
- return DriverManager.getConnection(url);
- } catch(SQLException e) {
- if(log != null)
- e.printStackTrace(log);
- }
- return null;
- }
-
- /**
- * Wraps the connection with a ConnectionInPool.
- * @see org.jboss.pool.jdbc.ConnectionInPool
- */
- public Object prepareObject(Object pooledObject) {
- Connection con = (Connection)pooledObject;
- ConnectionInPool wrapper = new ConnectionInPool(con);
- wrapper.setPSCacheSize(psCacheSize);
- return wrapper;
- }
-
- /**
- * Returns the original connection from a ConnectionInPool.
- * @see org.jboss.pool.jdbc.ConnectionInPool
- */
- public Object translateObject(Object clientObject) {
- return ((ConnectionInPool)clientObject).getUnderlyingConnection();
- }
-
- /**
- * Closes all outstanding work for the connection, rolls it back, and
- * returns the underlying connection to the pool.
- */
- public Object returnObject(Object clientObject) {
- ConnectionInPool wrapper = (ConnectionInPool)clientObject;
- Connection con = wrapper.getUnderlyingConnection();
- try {
- wrapper.reset();
- } catch(SQLException e) {
- pool.markObjectAsInvalid(clientObject);
- }
- return con;
- }
-
- /**
- * Closes a connection.
- */
- public void deleteObject(Object pooledObject) {
- Connection con = (Connection)pooledObject;
- try {
- con.rollback();
- } catch(SQLException e) {}
-
- // Removed all the cached PreparedStatements for this Connection
- ObjectCache cache = (ObjectCache)ConnectionInPool.psCaches.remove(con);
- if(cache != null)
- cache.close();
-
- try {
- con.close();
- } catch(SQLException e) {}
- }
+public class JDBCConnectionFactory
+ extends PoolObjectFactory
+{
+ private String url;
+ private Properties props;
+ private String userName;
+ private String password;
+ private int psCacheSize = 10;
+ private ObjectPool pool;
+
+ private static Logger log = Logger.getLogger( JDBCConnectionFactory.class );
+
+ /**
+ * Creates a new factory. You must configure it with JDBC properties
+ * before you can use it.
+ */
+ public JDBCConnectionFactory()
+ {
+ }
+
+ /**
+ * Sets the JDBC URL used to create new connections.
+ */
+ public void setConnectURL(String url)
+ {
+ this.url = url;
+ }
+
+ /**
+ * Gets the JDBC URL used to create new connections.
+ */
+ public String getConnectURL()
+ {
+ return url;
+ }
+
+ /**
+ * Sets the JDBC Propeties used to create new connections.
+ * This is optional, and will only be used if present.
+ */
+ public void setConnectProperties(Properties props)
+ {
+ this.props = props;
+ }
+
+ /**
+ * Gets the JDBC Properties used to create new connections.
+ */
+ public Properties getConnectProperties()
+ {
+ return props;
+ }
+
+ /**
+ * Sets the JDBC user name used to create new connections.
+ * This is optional, and will only be used if present.
+ */
+ public void setUser(String userName)
+ {
+ this.userName = userName;
+ }
+
+ /**
+ * Gets the JDBC user name used to create new connections.
+ */
+ public String getUser()
+ {
+ return userName;
+ }
+
+ /**
+ * Sets the JDBC password used to create new connections.
+ * This is optional, and will only be used if present.
+ */
+ public void setPassword(String password)
+ {
+ this.password = password;
+ }
+
+ /**
+ * Gets the JDBC password used to create new connections.
+ */
+ public String getPassword()
+ {
+ return password;
+ }
+
+ /**
+ * Sets the number of PreparedStatements to be cached for each
+ * Connection. Your DB product may impose a limit on the number
+ * of open PreparedStatements. The default value is 10.
+ */
+ public void setPSCacheSize(int size)
+ {
+ psCacheSize = size;
+ }
+
+ /**
+ * Gets the number of PreparedStatements to be cached for each
+ * Connection. The default value is 10.
+ */
+ public int getPSCacheSize()
+ {
+ return psCacheSize;
+ }
+
+ /**
+ * Validates that connection properties were set (at least a URL).
+ */
+ public void poolStarted(ObjectPool pool)
+ {
+ if( log.isDebugEnabled() )
+ log.debug( "Starting" );
+
+ super.poolStarted(pool);
+ if(url == null)
+ {
+ log.error( "Must specify JDBC connection URL" );
+ throw new IllegalStateException("Must specify JDBC connection URL to
"+getClass().getName());
+ }
+ this.pool = pool;
+ }
+
+ /**
+ * Cleans up.
+ */
+ public void poolClosing(ObjectPool pool)
+ {
+ if( log.isDebugEnabled() )
+ log.debug( "Stopping" );
+
+ super.poolClosing(pool);
+ this.pool = null;
+ }
+
+ /**
+ * Creates a new JDBC Connection.
+ */
+ public Object createObject(Object parameters)
+ {
+ if( log.isTraceEnabled() )
+ log.trace( "Opening new connection" );
+
+ try
+ {
+ if(userName != null && userName.length() > 0)
+ return DriverManager.getConnection(url, userName, password);
+ else if(props != null)
+ return DriverManager.getConnection(url, props);
+ else
+ return DriverManager.getConnection(url);
+ }
+ catch(SQLException e)
+ {
+ log.error( "SQL Error", e );
+ }
+
+ return null;
+ }
+
+ /**
+ * Wraps the connection with a ConnectionInPool.
+ * @see org.jboss.pool.jdbc.ConnectionInPool
+ */
+ public Object prepareObject(Object pooledObject)
+ {
+ Connection con = (Connection)pooledObject;
+ ConnectionInPool wrapper = new ConnectionInPool(con);
+ wrapper.setPSCacheSize(psCacheSize);
+ return wrapper;
+ }
+
+ /**
+ * Returns the original connection from a ConnectionInPool.
+ * @see org.jboss.pool.jdbc.ConnectionInPool
+ */
+ public Object translateObject(Object clientObject)
+ {
+ return ((ConnectionInPool)clientObject).getUnderlyingConnection();
+ }
+
+ /**
+ * Closes all outstanding work for the connection, rolls it back, and
+ * returns the underlying connection to the pool.
+ */
+ public Object returnObject(Object clientObject)
+ {
+ ConnectionInPool wrapper = (ConnectionInPool)clientObject;
+ Connection con = wrapper.getUnderlyingConnection();
+ try
+ {
+ wrapper.reset();
+ } catch(SQLException e)
+ {
+ pool.markObjectAsInvalid(clientObject);
+ }
+ return con;
+ }
+
+ /**
+ * Closes a connection.
+ */
+ public void deleteObject(Object pooledObject)
+ {
+ Connection con = (Connection)pooledObject;
+ try
+ {
+ con.rollback();
+ } catch(SQLException ignored)
+ {
+ }
+
+ // Removed all the cached PreparedStatements for this Connection
+ ObjectCache cache = (ObjectCache)ConnectionInPool.psCaches.remove(con);
+ if(cache != null)
+ cache.close();
+
+ try
+ {
+ con.close();
+ } catch(SQLException ignored)
+ {
+ }
+ }
}
+/*
+vim:tabstop=3:et:shiftwidth=3
+*/
1.1.1.1.2.2 +246 -188
jbosspool/src/main/org/jboss/pool/jdbc/Attic/JDBCPoolDataSource.java
Index: JDBCPoolDataSource.java
===================================================================
RCS file:
/cvsroot/jboss/jbosspool/src/main/org/jboss/pool/jdbc/Attic/JDBCPoolDataSource.java,v
retrieving revision 1.1.1.1.2.1
retrieving revision 1.1.1.1.2.2
diff -u -r1.1.1.1.2.1 -r1.1.1.1.2.2
--- JDBCPoolDataSource.java 27 Feb 2002 08:14:03 -0000 1.1.1.1.2.1
+++ JDBCPoolDataSource.java 4 Mar 2002 23:35:08 -0000 1.1.1.1.2.2
@@ -5,11 +5,18 @@
import java.sql.Connection;
import java.io.PrintWriter;
-import java.util.*;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.Properties;
import javax.sql.DataSource;
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 non-transactional JDBC pools. This handles configuration
@@ -23,61 +30,73 @@
*
* @author Aaron Mulder ([EMAIL PROTECTED])
*/
-public class JDBCPoolDataSource implements DataSource, Referenceable, ObjectFactory
{
- private static HashMap sources = new HashMap();
- /**
- * Gets all the current JDBC pool data sources.
- */
- public static Collection getDataSources() {
- return new HashSet(sources.values());
- }
- /**
- * Gets a specific JDBC pool data source by pool name.
- */
- public static JDBCPoolDataSource getDataSource(String poolName) {
- return (JDBCPoolDataSource)sources.get(poolName);
- }
-
- private ObjectPool pool;
- private JDBCConnectionFactory factory;
- private PrintWriter logWriter;
- private int timeout;
- private boolean initialized = false;
- private String jndiName;
-
- /**
- * Creates a new JDBC pool data source. Be sure to configure it and then
- * call initialize before you try to use it.
- */
- public JDBCPoolDataSource() {
- pool = new ObjectPool();
- factory = new JDBCConnectionFactory();
- PoolDriver.instance();
- }
-
-// Unique properties
- /**
- * If you use this to set a JNDI name, this pool will be bound to that name
- * using the default InitialContext. You can also do this manually if you
- * have additional requirements.
- */
- public void setJNDIName(String name) throws NamingException {
- InitialContext ctx = new InitialContext();
- if(jndiName != null && !jndiName.equals(name))
- ctx.unbind(jndiName);
- if(name != null)
- ctx.bind(name, this);
- jndiName = name;
- }
-
- /**
- * Gets the JNDI name this pool is bound to. Only valid if you used
- * setJNDIName to bind it.
- * @see #setJNDIName
- */
- public String getJNDIName() {return jndiName;}
+public class JDBCPoolDataSource
+ implements DataSource, Referenceable, ObjectFactory
+{
+ private static Logger log = Logger.getLogger( JDBCPoolDataSource.class );
+
+ private static HashMap sources = new HashMap();
+ /**
+ * Gets all the current JDBC pool data sources.
+ */
+ public static Collection getDataSources()
+ {
+ return new HashSet(sources.values());
+ }
+
+ /**
+ * Gets a specific JDBC pool data source by pool name.
+ */
+ public static JDBCPoolDataSource getDataSource(String poolName)
+ {
+ return (JDBCPoolDataSource)sources.get(poolName);
+ }
+
+ private ObjectPool pool;
+ private JDBCConnectionFactory factory;
+ private PrintWriter logWriter;
+ private int timeout;
+ private boolean initialized = false;
+ private String jndiName;
+
+ /**
+ * Creates a new JDBC pool data source. Be sure to configure it and then
+ * call initialize before you try to use it.
+ */
+ public JDBCPoolDataSource()
+ {
+ pool = new ObjectPool();
+ factory = new JDBCConnectionFactory();
+ PoolDriver.instance();
+ }
+
+ // Unique properties
+ /**
+ * If you use this to set a JNDI name, this pool will be bound to that name
+ * using the default InitialContext. You can also do this manually if you
+ * have additional requirements.
+ */
+ public void setJNDIName(String name)
+ throws NamingException
+ {
+ InitialContext ctx = new InitialContext();
+ if(jndiName != null && !jndiName.equals(name))
+ ctx.unbind(jndiName);
+ if(name != null)
+ ctx.bind(name, this);
+ jndiName = name;
+ }
+
+ /**
+ * Gets the JNDI name this pool is bound to. Only valid if you used
+ * setJNDIName to bind it.
+ * @see #setJNDIName
+ */
+ public String getJNDIName() {
+ return jndiName;
+ }
-// JDBC properties
+ // JDBC properties
public void setJDBCURL(String url) {factory.setConnectURL(url);}
public String getJDBCURL() {return factory.getConnectURL();}
public void setJDBCProperties(Properties props)
{factory.setConnectProperties(props);}
@@ -122,137 +141,176 @@
* Initializes the pool. You need to have configured all the pool and
* JDBC properties first.
*/
- public void initialize() {
- initialized = true;
- pool.setObjectFactory(factory);
- pool.initialize();
- }
-
- /**
- * Returns a string describing the pool status (number of connections
- * created, used, and maximum).
- */
- public String getPoolStatus() {
- return pool.toString();
- }
-
- /**
- * 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 {
- setJNDIName(null);
- } catch(NamingException e) {
- if(logWriter != null)
- e.printStackTrace(logWriter);
- }
- sources.remove(pool.getName());
- pool.shutDown();
- pool = null;
- factory = null;
- }
-
- /**
- * Gets a connection from the pool.
- */
- public Connection getConnection() throws java.sql.SQLException {
- if(!initialized) initialize();
- return (Connection)pool.getObject();
- }
-
- /**
- * Gets a connection from the pool. If a new connection must be
- * created, it will use the specified user name and password. If there is
- * a connection available in the pool, it will be used, regardless of the
- * user name and password use to created it initially.
- */
- public Connection getConnection(String user, String password) throws
java.sql.SQLException {
- if(!initialized) initialize();
- factory.setUser(user);
- factory.setPassword(password);
- return (Connection)pool.getObject();
- }
-
- /**
- * Gets a log writer used to record pool events.
- */
- 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);
- }
-
- /**
- * This property is not used by this implementation.
- */
- public int getLoginTimeout() throws java.sql.SQLException {
- return timeout;
- }
-
- /**
- * This property is not used by this implementation.
- */
- public void setLoginTimeout(int timeout) throws java.sql.SQLException {
- this.timeout = timeout;
- }
-
- /**
- * This method is used for parsing JDBCProperties
- */
- private static Properties parseProperties(String string) {
- Properties props = new Properties();
- if(string == null || string.length() == 0) return props;
- int lastPos = -1;
- int pos = string.indexOf(";");
- while(pos > -1) {
- addProperty(props, string.substring(lastPos+1, pos));
- lastPos = pos;
- pos = string.indexOf(";", lastPos+1);
- }
- addProperty(props, string.substring(lastPos+1));
- return props;
- }
-
- /**
- * This method is used for parsing JDBCProperties
- */
- private static void addProperty(Properties props, String property) {
- int pos = property.indexOf("=");
- if(pos < 0) {
- System.err.println("Unable to parse property '"+property+"' - please
use 'name=value'");
- return;
- }
- props.setProperty(property.substring(0, pos), property.substring(pos+1));
- }
-
- // Referenceable implementation ----------------------------------
- /**
- * Gets a reference to this data source.
- */
- public Reference getReference() {
- return new Reference(getClass().getName(), new StringRefAddr("JDBCPool",
pool.getName()), getClass().getName(), null);
- }
-
- // ObjectFactory implementation ----------------------------------
- /**
- * Decodes a reference to a specific pool data source.
- */
- public Object getObjectInstance(Object obj, Name name, Context nameCtx,
- Hashtable environment) {
- if(obj instanceof Reference) {
- Reference ref = (Reference)obj;
- if(ref.getClassName().equals(getClass().getName())) {
- RefAddr addr = ref.get("JDBCPool");
- return sources.get((String)addr.getContent());
- }
- }
- return null;
- }
+ public void initialize()
+ {
+ initialized = true;
+ pool.setObjectFactory(factory);
+ pool.initialize();
+ }
+
+ /**
+ * Returns a string describing the pool status (number of connections
+ * created, used, and maximum).
+ */
+ public String getPoolStatus()
+ {
+ return pool.toString();
+ }
+
+ /**
+ * 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
+ {
+ setJNDIName(null);
+ } catch(NamingException e)
+ {
+ log.warn( "Can't unbind from JNDI", e );
+ }
+ sources.remove(pool.getName());
+ pool.shutDown();
+ pool = null;
+ factory = null;
+ }
+
+ /**
+ * Gets a connection from the pool.
+ */
+ public Connection getConnection()
+ throws java.sql.SQLException
+ {
+ if(!initialized)
+ initialize();
+
+ return (Connection)pool.getObject();
+ }
+
+ /**
+ * Gets a connection from the pool. If a new connection must be
+ * created, it will use the specified user name and password. If there is
+ * a connection available in the pool, it will be used, regardless of the
+ * user name and password use to created it initially.
+ */
+ public Connection getConnection(String user, String password)
+ throws java.sql.SQLException
+ {
+ if(!initialized)
+ initialize();
+
+ factory.setUser(user);
+ factory.setPassword(password);
+ return (Connection)pool.getObject();
+ }
+
+ /**
+ * Gets a log writer used to record pool events.
+ */
+ 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
+ {
+ if( writer == null )
+ {
+ logWriter = null;
+ }
+ else
+ {
+ if( logWriter == null )
+ {
+ logWriter = new CategoryWriter( log.getCategory() );
+ }
+ }
+ }
+
+ /**
+ * This property is not used by this implementation.
+ */
+ public int getLoginTimeout()
+ throws java.sql.SQLException
+ {
+ return timeout;
+ }
+
+ /**
+ * This property is not used by this implementation.
+ */
+ public void setLoginTimeout(int timeout)
+ throws java.sql.SQLException
+ {
+ this.timeout = timeout;
+ }
+
+ /**
+ * This method is used for parsing JDBCProperties
+ */
+ private static Properties parseProperties(String string)
+ {
+ Properties props = new Properties();
+ if(string == null || string.length() == 0)
+ return props;
+
+ int lastPos = -1;
+ int pos = string.indexOf(";");
+ while(pos > -1)
+ {
+ addProperty(props, string.substring(lastPos+1, pos));
+ lastPos = pos;
+ pos = string.indexOf(";", lastPos+1);
+ }
+ addProperty(props, string.substring(lastPos+1));
+ return props;
+ }
+
+ /**
+ * This method is used for parsing JDBCProperties
+ */
+ private static void addProperty(Properties props, String property)
+ {
+ int pos = property.indexOf("=");
+ if(pos < 0) {
+ System.err.println("Unable to parse property '"+property+"' - please use
'name=value'");
+ return;
+ }
+ props.setProperty(property.substring(0, pos), property.substring(pos+1));
+ }
+
+ // Referenceable implementation ----------------------------------
+ /**
+ * Gets a reference to this data source.
+ */
+ public Reference getReference() {
+ return new Reference(getClass().getName(), new StringRefAddr("JDBCPool",
pool.getName()), getClass().getName(), null);
+ }
+
+ // ObjectFactory implementation ----------------------------------
+ /**
+ * Decodes a reference to a specific pool data source.
+ */
+ public Object getObjectInstance(Object obj, Name name, Context nameCtx,
Hashtable environment)
+ {
+ if(obj instanceof Reference)
+ {
+ Reference ref = (Reference)obj;
+ if(ref.getClassName().equals(getClass().getName()))
+ {
+ RefAddr addr = ref.get("JDBCPool");
+ return sources.get((String)addr.getContent());
+ }
+ }
+ return null;
+ }
}
+/*
+vim:tabstop=3:et:shiftwidth=3
+*/
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development