[CJ-dev] commonjava-projects/commonjava-datasrc/src/java/org/commonjava/datasrc/sql ConnectionPool.j
Brought to you by:
johnqueso
From: <joh...@co...> - 2004-01-30 17:36:20
|
Update of /cvsroot/commonjava/commonjava-projects/commonjava-datasrc/src/java/org/commonjava/datasrc/sql In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8549/src/java/org/commonjava/datasrc/sql Modified Files: ConnectionPool.java ConnectionPoolManager.java Log Message: added unit test for connection pool by itself, using hsqldb. Isolated the ConnectionPool to allow for single-pool direct instantiations, instead of always having to work through the manager...also improved the user/password logic for connection instantiation. Index: ConnectionPool.java =================================================================== RCS file: /cvsroot/commonjava/commonjava-projects/commonjava-datasrc/src/java/org/commonjava/datasrc/sql/ConnectionPool.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- ConnectionPool.java 20 Jan 2004 04:42:41 -0000 1.2 +++ ConnectionPool.java 29 Jan 2004 03:54:30 -0000 1.3 @@ -25,6 +25,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.commonjava.datasrc.sql.config.*; +import org.commonjava.util.Strings; import org.commonjava.util.pool.VolatileObject; import org.commonjava.util.pool.VolatileObjectPool; @@ -66,7 +67,7 @@ * * @param PoolConnectionInfo the connection information */ - ConnectionPool(PoolConnectionInfo connInfo) throws ConnectionPoolException + public ConnectionPool(PoolConnectionInfo connInfo) throws ConnectionPoolException { String driverName = connInfo.getDriverName(); @@ -103,7 +104,7 @@ * @throws ConnectionPoolException if there are not valid connections * and a new connection could not be established. */ - Connection getConnection() throws ConnectionPoolException + public Connection getConnection() throws ConnectionPoolException { try { @@ -182,10 +183,29 @@ public ReusableConnection() throws SQLException { - conn = DriverManager.getConnection( - connInfo.getUrl(), - connInfo.getProperties() - ); + String user = connInfo.getUser(); + if(user == null){ + user = connInfo.getProperties().getProperty(PoolConnectionInfo.USER_PROPERTY); + } + + String password = connInfo.getPassword(); + if(password == null){ + password = connInfo.getProperties().getProperty(PoolConnectionInfo.PASSWORD_PROPERTY); + } + + if(!Strings.empty(user)){ + conn = DriverManager.getConnection( + connInfo.getUrl(), + user, + password + ); + } + else{ + conn = DriverManager.getConnection( + connInfo.getUrl(), + connInfo.getProperties() + ); + } } private Connection getConnection(){ Index: ConnectionPoolManager.java =================================================================== RCS file: /cvsroot/commonjava/commonjava-projects/commonjava-datasrc/src/java/org/commonjava/datasrc/sql/ConnectionPoolManager.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- ConnectionPoolManager.java 20 Jan 2004 04:42:41 -0000 1.2 +++ ConnectionPoolManager.java 29 Jan 2004 03:54:30 -0000 1.3 @@ -11,17 +11,13 @@ package org.commonjava.datasrc.sql; -import java.io.IOException; -import java.io.InputStream; import java.sql.Connection; -import java.sql.SQLException; import java.util.Hashtable; import java.util.Iterator; -import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.commonjava.datasrc.sql.config.*; +import org.commonjava.datasrc.sql.config.PoolConnectionInfo; /** * Create and manage all ConnectionPools. ConnectionPool datasources @@ -80,79 +76,28 @@ * @return Connection a database connection * @throws ConnectionPoolException if a connection could not be established */ - public synchronized Connection getConnection(String datasource) + public Connection getConnection(PoolConnectionInfo info) throws ConnectionPoolException { checkClosed(); - ConnectionPool pool = (ConnectionPool)pools.get(datasource); - - // if the pool is not already loaded, lazily create it - if(pool == null) - { - if(LOG.isDebugEnabled()){ - LOG.debug("initializing pools"); - } + ConnectionPool pool = null; + synchronized(pools){ + pool = (ConnectionPool)pools.get(info); - // use the classloader to find the file in the classpath - ClassLoader cl = this.getClass().getClassLoader(); - InputStream in = cl.getResourceAsStream(PERSISTENT_PROPERTIES); - - if(in == null) - { - throw new ConnectionPoolException( - "fail to find " + PERSISTENT_PROPERTIES + " in the classpath" - ); - } - - try + // if the pool is not already loaded, lazily create it + if(pool == null) { - Properties properties = new Properties(); - properties.load(in); - - String key = CONN_POOL_DATASOURCE + "." + datasource; - - String connProp = properties.getProperty(key); - - if(connProp == null) - throw new ConnectionPoolException("No " + key + " found in " + PERSISTENT_PROPERTIES); - - PoolConnectionInfo connInfo = new PoolConnectionInfo(connProp); - if(LOG.isDebugEnabled()){ - LOG.debug("creating connection pool"); - } - - pool = new ConnectionPool(connInfo); - pools.put(datasource, pool); - } - catch(SQLException ex) - { - throw new ConnectionPoolException( - "Unexpected SQLException", - ex - ); - } - catch(IOException ex) - { - throw new ConnectionPoolException( - "Failed to read " + PERSISTENT_PROPERTIES, - ex - ); - } - finally - { - try - { - if(in != null) - in.close(); - } - catch(IOException ignore) - { + LOG.debug("initializing pool for " + info.getName()); } + + pool = new ConnectionPool(info); + pools.put(info, pool); } } + if(LOG.isDebugEnabled()){ LOG.debug("retrieving connection from pool"); } @@ -198,7 +143,8 @@ for(Iterator iter = pools.keySet().iterator(); iter.hasNext();) { - String key = (String)iter.next(); + PoolConnectionInfo info = (PoolConnectionInfo)iter.next(); + String key = (String)info.getName(); buffer.append("datasource="); buffer.append(key); buffer.append("\n"); |