Added "db.poolname" configuration (so that multiple applications can share a single DBCP). Also add a shutdown method to allow web applications to clean up on undeploy.

git-svn-id: http://scm.dspace.org/svn/repo/trunk@2469 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
Graham Triggs
2007-12-29 22:20:07 +00:00
parent 38587c5295
commit 0499f077b9
2 changed files with 31 additions and 3 deletions

View File

@@ -97,6 +97,9 @@ public class DatabaseManager
/** True if initialization has been done */ /** True if initialization has been done */
private static boolean initialized = false; private static boolean initialized = false;
/** Name to use for the pool */
private static String poolName = "dspacepool";
/** /**
* This regular expression is used to perform sanity checks * This regular expression is used to perform sanity checks
* on database names (i.e. tables and columns). * on database names (i.e. tables and columns).
@@ -509,7 +512,7 @@ public class DatabaseManager
initialize(); initialize();
return DriverManager return DriverManager
.getConnection("jdbc:apache:commons:dbcp:dspacepool"); .getConnection("jdbc:apache:commons:dbcp:" + poolName);
} }
/** /**
@@ -1385,10 +1388,25 @@ public class DatabaseManager
} }
} }
/**
* Provide a means for a (web) application to cleanly terminate the connection pool.
* @throws SQLException
*/
public static synchronized void shutdown() throws SQLException
{
if (initialized)
{
initialized = false;
PoolingDriver driver = (PoolingDriver)DriverManager.getDriver("jdbc:apache:commons:dbcp:" + poolName);
if (driver != null)
driver.closePool(poolName);
}
}
/** /**
* Initialize the DatabaseManager. * Initialize the DatabaseManager.
*/ */
private static void initialize() throws SQLException private static synchronized void initialize() throws SQLException
{ {
if (initialized) if (initialized)
{ {
@@ -1491,6 +1509,12 @@ public class DatabaseManager
false, // read only is not default for now false, // read only is not default for now
false); // Autocommit defaults to none false); // Autocommit defaults to none
// Obtain a poolName from the config, default is "dspacepool"
if (ConfigurationManager.getProperty("db.poolname") != null)
{
poolName = ConfigurationManager.getProperty("db.poolname");
}
// //
// Finally, we create the PoolingDriver itself... // Finally, we create the PoolingDriver itself...
// //
@@ -1499,7 +1523,7 @@ public class DatabaseManager
// //
// ...and register our pool with it. // ...and register our pool with it.
// //
driver.registerPool("dspacepool", connectionPool); driver.registerPool(poolName, connectionPool);
// Old SimplePool init // Old SimplePool init
//DriverManager.registerDriver(new SimplePool()); //DriverManager.registerDriver(new SimplePool());

View File

@@ -55,6 +55,10 @@ db.maxidle = -1
# Determine if prepared statement should be cached. (default is true) # Determine if prepared statement should be cached. (default is true)
db.statementpool = true db.statementpool = true
# Specify a name for the connection pool (useful if you have multiple applications sharing Tomcat's dbcp)
# If not specified, defaults to 'dspacepool'
# db.poolname = dspacepool
##### Email settings ###### ##### Email settings ######
# SMTP mail server # SMTP mail server