Here's some initial work...it may look like a lot of modifications, but
I really haven't changed anything major yet. Thoughts?
Index: ConnectionPool.java
===================================================================
RCS file:
/products/cvs/turbine/turbine/src/java/org/apache/turbine/util/db/pool/ConnectionPool.java,v
retrieving revision 1.2
diff -u -r1.2 ConnectionPool.java
--- ConnectionPool.java 2000/09/05 21:09:34 1.2
+++ ConnectionPool.java 2000/09/07 00:41:11
@@ -75,7 +75,9 @@
*/
public class ConnectionPool // implements Runnable
{
- /** Pool containing database connections. */
+ /**
+ * Pool containing database connections.
+ */
private Stack pool = null;
/**
@@ -160,11 +162,7 @@
protected void finalize()
throws Throwable
{
- while (!pool.empty())
- {
- DBConnection dbcon = (DBConnection)pool.pop();
- closeConnection(dbcon);
- }
+ disconnect();
}
/**
@@ -272,7 +270,7 @@
}
else
{
- closeConnection(con);
+ con.close();
totalConnections--;
connectionAttemptsCounter = 0;
@@ -333,21 +331,18 @@
}
/**
- * Force the close of a database connection.
+ * Close all connections to the database,
*
- * @param dbcon Connection to close.
+ * @exception Exception Trouble closing the connections.
*/
- private void closeConnection ( DBConnection dbcon )
+ protected synchronized void disconnect() throws Exception
{
- try
+ if ( pool != null )
{
- Connection conn = dbcon.getConnection();
- if ( conn != null && ! conn.isClosed() )
- conn.close();
- }
- catch (Exception e)
- {
- // Currently ignoring a connection close error.
+ while ( !pool.isEmpty() )
+ {
+ ((DBConnection)pool.pop()).close();
+ }
}
}
@@ -368,7 +363,7 @@
}
else
{
- closeConnection(connection);
+ connection.close();
totalConnections--;
}
}
Index: DBBroker.java
===================================================================
RCS file:
/products/cvs/turbine/turbine/src/java/org/apache/turbine/util/db/pool/DBBroker.java,v
retrieving revision 1.4
diff -u -r1.4 DBBroker.java
--- DBBroker.java 2000/09/05 21:09:34 1.4
+++ DBBroker.java 2000/09/07 00:41:12
@@ -58,10 +58,15 @@
// Java Core Classes
import java.util.Hashtable;
+import java.util.Properties;
// Turbine Utility Classes
+import org.apache.turbine.util.Log;
import org.apache.turbine.util.db.map.DatabaseMap;
import org.apache.turbine.util.db.map.MapBuilder;
+import org.apache.turbine.services.BaseService;
+import org.apache.turbine.services.InitializationException;
+import org.apache.turbine.services.ServiceBroker;
import org.apache.turbine.services.resources.TurbineResources;
/**
@@ -86,25 +91,31 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Frank Y. Kim</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Brett McLaughlin</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Greg Ritter</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Daniel Rall</a>
* @version $Id: DBBroker.java,v 1.4 2000/09/05 21:09:34 gonzalo Exp $
*/
-public class DBBroker
+public class DBBroker extends BaseService
{
- /** The single instance of this class. */
+ /**
+ * Name of the default map builder and map.
+ */
+ public static final String DEFAULT = "default";
+
+ /**
+ * The single instance of this class.
+ */
private static DBBroker instance = null;
/**
* The various connection pools this broker contains. Keyed by
- * database url.
+ * database URL.
*/
- private static Hashtable pools = null;
-
- /** The DatabaseMap which contains the tables in this DB. */
- private static Hashtable dbMaps = null;
+ protected Hashtable pools = null;
- /** Name of the default map builder and map. */
- public static final String DEFAULT = "default";
-
+ /**
+ * The DatabaseMap which contains the tables in this DB.
+ */
+ protected Hashtable dbMaps = null;
/**
* This constructor is private to force clients to use
@@ -112,18 +123,37 @@
*/
private DBBroker()
{
+ super();
+ try
+ {
+ init();
+ }
+ catch (InitializationException e)
+ {
+ Log.error(e);
+ }
+ }
+
+ /**
+ * Initializes this <code>Service</code>.
+ */
+ public void init() throws InitializationException
+ {
+ super.init();
pools = new Hashtable();
dbMaps = new Hashtable();
}
/**
* This method returns a connection using the values specified in
- * a properties file. The format of the values is this:
+ * a properties file. The format of the values is as follows:
*
+ * <pre>
* database.[name].driver
* database.[name].url
* database.[name].username
* database.[name].password
+ * </pre>
*
* @param name A String.
* @return A DBConnection.
@@ -259,10 +289,7 @@
*/
public void addDatabaseMap(String name)
{
- synchronized( dbMaps )
- {
- dbMaps.put(name, new DatabaseMap(name) );
- }
+ addDatabaseMap( new DatabaseMap(name) );
}
/**
@@ -320,7 +347,7 @@
/**
* The method through which this class is accessed.
*
- * @return A DBBroker, the single instance of this class.
+ * @return A <code>DBBroker</code>, the single instance of this
class.
*/
public static DBBroker getInstance()
{
@@ -356,10 +383,33 @@
{
pool = (ConnectionPool) pools.get( url );
}
+
+ // If there is a ConnecitonPool associated with this
+ // connection's URL, release the connection from that
pool.
if ( pool != null )
+ {
pool.releaseConnection( connection );
- // Else if there is no pool for this connection don't
- // do anything.
+ }
+ }
+ }
+ }
+
+ /**
+ * Releases the database connections for all pools on service
shutdown.
+ */
+ public synchronized void shutdown()
+ {
+ if ( pools != null )
+ {
+ // Release connections for each pool.
+ java.util.Iterator poolIter = pools.values().iterator();
+ while ( poolIter.hasNext() )
+ {
+ try
+ {
+ ((ConnectionPool)poolIter.next()).disconnect();
+ }
+ catch (Exception ignored) {}
}
}
}
Index: DBConnection.java
===================================================================
RCS file:
/products/cvs/turbine/turbine/src/java/org/apache/turbine/util/db/pool/DBConnection.java,v
retrieving revision 1.2
diff -u -r1.2 DBConnection.java
--- DBConnection.java 2000/09/05 21:09:35 1.2
+++ DBConnection.java 2000/09/07 00:41:13
@@ -73,6 +73,7 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Frank Y. Kim</a>
* @author <a href="mailto:[EMAIL PROTECTED]">John D.
McNally</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Daniel L. Rall</a>
* @version $Id: DBConnection.java,v 1.2 2000/09/05 21:09:35 gonzalo
Exp $
*/
public class DBConnection
@@ -189,7 +190,7 @@
/**
* Create a Java SQL statement for this connection.
*
- * @return A java.sql.Statement.
+ * @return A new <code>Statement</code>.
*/
public java.sql.Statement createStatement()
{
@@ -204,5 +205,23 @@
Log.error(e);
}
return stmt;
+ }
+
+ /**
+ * Force the close of this database connection.
+ *
+ * @param dbConn The connection to close.
+ */
+ public void close()
+ {
+ Connection conn = getConnection();
+ try
+ {
+ if ( conn != null && !conn.isClosed() )
+ {
+ conn.close();
+ }
+ }
+ catch (Exception ignored) {}
}
}
--
Daniel Rall <[EMAIL PROTECTED]>
http://collab.net/ | open source | do the right thing
------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Search: <http://www.mail-archive.com/turbine%40list.working-dogs.com/>
Problems?: [EMAIL PROTECTED]