public void initModule() throws ModuleInitializationException {
try {
Server s_server = getServer();
defaultPoolName = this.getParameter("defaultPoolName");
if (defaultPoolName == null || defaultPoolName.equalsIgnoreCase("")) {
throw new ModuleInitializationException("Default Connection Pool "
+ "Name Not Specified",
getRole());
}
logger.debug("DefaultPoolName: " + defaultPoolName);
String poolList = this.getParameter("poolNames");
// Pool names should be comma delimited
String[] poolNames = poolList.split(",");
// Initialize each connection pool
for (int i = 0; i < poolNames.length; i++) {
DatastoreConfig config =
s_server.getDatastoreConfig(poolNames[i]);
jdbcDriverClass = config.getParameter("jdbcDriverClass");
dbUsername = config.getParameter("dbUsername");
dbPassword = config.getParameter("dbPassword");
jdbcURL = config.getParameter("jdbcURL");
maxActive =
new Integer(config.getParameter("maxActive"))
.intValue();
maxIdle =
new Integer(config.getParameter("maxIdle")).intValue();
maxWait =
new Integer(config.getParameter("maxWait")).intValue();
minIdle =
new Integer(config.getParameter("minIdle")).intValue();
numTestsPerEvictionRun =
new Integer(config
.getParameter("numTestsPerEvictionRun"))
.intValue();
minEvictableIdleTimeMillis =
new Long(config
.getParameter("minEvictableIdleTimeMillis"))
.longValue();
timeBetweenEvictionRunsMillis =
new Long(config
.getParameter("timeBetweenEvictionRunsMillis"))
.longValue();
validationQuery = config.getParameter("validationQuery");
testOnBorrow =
new Boolean(config.getParameter("testOnBorrow"))
.booleanValue();
testOnReturn =
new Boolean(config.getParameter("testOnReturn"))
.booleanValue();
testWhileIdle =
new Boolean(config.getParameter("testWhileIdle"))
.booleanValue();
whenExhaustedAction =
new Byte(config.getParameter("whenExhaustedAction"))
.byteValue();
if (whenExhaustedAction != 0 && whenExhaustedAction != 1
&& whenExhaustedAction != 2) {
logger.debug("Valid values for whenExhaustedAction are: 0 - (fail), 1 - (block), or 2 - (grow)");
throw new ModuleInitializationException("A connection pool could "
+ "not be instantiated. The underlying error was an "
+ "invalid value for the whenExhaustedAction parameter."
+ "Valid values are 0 - (fail), 1 - (block), or 2 - (grow). Value specified"
+ "was \"" + whenExhaustedAction + "\".", getRole());
}
if (logger.isDebugEnabled()) {
logger.debug("poolName[" + i + "] = " + poolNames[i]);
logger.debug("JDBC driver: " + jdbcDriverClass);
logger.debug("Database username: " + dbUsername);
logger.debug("Database password: " + dbPassword);
logger.debug("JDBC connection URL: " + jdbcURL);
logger.debug("Maximum active connections: " + maxActive);
logger.debug("Maximum idle connections: " + maxIdle);
logger.debug("Maximum wait time: " + maxWait);
logger.debug("Minimum idle time: " + minIdle);
logger.debug("Number of tests per eviction run: "
+ numTestsPerEvictionRun);
logger.debug("Minimum Evictable Idle time: "
+ minEvictableIdleTimeMillis);
logger.debug("Minimum Evictable Idle time: "
+ timeBetweenEvictionRunsMillis);
logger.debug("Validation query: " + validationQuery);
logger.debug("Test on borrow: " + testOnBorrow);
logger.debug("Test on return: " + testOnReturn);
logger.debug("Test while idle: " + testWhileIdle);
logger.debug("whenExhaustedAction: " + whenExhaustedAction);
}
// Treat any parameters whose names start with "connection."
// as connection parameters
Map<String, String> cProps = new HashMap<String, String>();
for (String name : config.getParameters().keySet()) {
if (name.startsWith("connection.")) {
String realName = name.substring(11);
logger.debug("Connection property " + realName + " = "
+ config.getParameter(name));
cProps.put(realName, config.getParameter(name));
}
}
// If a ddlConverter has been specified for the pool,
// try to instantiate it so the ConnectionPool can use
// it when it provides a TableCreatingConnection.
// If a ddlConverter has been specified, it is assumed
// that a failure to initialize (construct) it should
// trigger a ModuleInitializationException (a fatal startup error).
DDLConverter ddlConverter = null;
String ddlConverterClassName =
getServer().getDatastoreConfig(poolNames[i])
.getParameter("ddlConverter");
if (ddlConverterClassName != null) {
try {
ddlConverter =
(DDLConverter) Class
.forName(ddlConverterClassName)
.newInstance();
} catch (Throwable th) {
throw new ModuleInitializationException("A DDLConverter was "
+ "specified for the pool \""
+ poolNames[i]
+ "\", but it couldn't be instantiated.",
getRole(),
th);
}
}
// Create connection pool
try {
ConnectionPool connectionPool =
new ConnectionPool(jdbcDriverClass,
jdbcURL,
dbUsername,
dbPassword,
ddlConverter,
maxActive,
maxIdle,
maxWait,
minIdle,
minEvictableIdleTimeMillis,
numTestsPerEvictionRun,
timeBetweenEvictionRunsMillis,
validationQuery,
testOnBorrow,
testOnReturn,
testWhileIdle,
whenExhaustedAction);
connectionPool.setConnectionProperties(cProps);
logger.debug("Initialized Pool: " + connectionPool);
h_ConnectionPools.put(poolNames[i], connectionPool);
logger.debug("putPoolInHash: " + h_ConnectionPools.size());
} catch (SQLException sqle) {
logger.error("Unable to initialize connection pool: "
+ poolNames[i] + ": " + sqle.getMessage());
}
}
} catch (Throwable th) {
th.printStackTrace();
throw new ModuleInitializationException("A connection pool could "
+ "not be instantiated. The underlying error was a "
+ th.getClass().getName() + "The message was \""
+ th.getMessage() + "\".", getRole());
}
}