in commons-dbcp-1.3/src/main/java/org/apache/commons/dbcp/BasicDataSource.java [1120:1255]
protected synchronized DataSource createDataSource()
throws SQLException {
// Return the pool if we have already created it
if (dataSource != null) {
return (dataSource);
}
// Load the JDBC driver class
Class driverFromCCL = null;
if (driverClassName != null) {
try {
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
driverFromCCL = Thread.currentThread().getContextClassLoader().loadClass(driverClassName);
}
} catch (Throwable t) {
String message = "Cannot load JDBC driver class '" +
driverClassName + "'";
logWriter.println(message);
t.printStackTrace(logWriter);
throw new SQLNestedException(message, t);
}
}
// Create a JDBC driver instance
Driver driver = null;
try {
if (driverFromCCL != null) {
driver = (Driver) driverFromCCL.newInstance();
if (!driver.acceptsURL(url)) {
new SQLException("No suitable driver", "08001");
}
} else {
driver = DriverManager.getDriver(url);
}
} catch (Throwable t) {
String message = "Cannot create JDBC driver of class '" +
(driverClassName != null ? driverClassName : "") +
"' for connect URL '" + url + "'";
logWriter.println(message);
t.printStackTrace(logWriter);
throw new SQLNestedException(message, t);
}
// Can't test without a validationQuery
if (validationQuery == null) {
setTestOnBorrow(false);
setTestOnReturn(false);
setTestWhileIdle(false);
}
// Create an object pool to contain our active connections
if ((abandonedConfig != null) && (abandonedConfig.getRemoveAbandoned())) {
connectionPool = new AbandonedObjectPool(null,abandonedConfig);
}
else {
connectionPool = new GenericObjectPool();
}
connectionPool.setMaxActive(maxActive);
connectionPool.setMaxIdle(maxIdle);
connectionPool.setMinIdle(minIdle);
connectionPool.setMaxWait(maxWait);
connectionPool.setTestOnBorrow(testOnBorrow);
connectionPool.setTestOnReturn(testOnReturn);
connectionPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
connectionPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
connectionPool.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
connectionPool.setTestWhileIdle(testWhileIdle);
// Set up statement pool, if desired
GenericKeyedObjectPoolFactory statementPoolFactory = null;
if (isPoolPreparedStatements()) {
statementPoolFactory = new GenericKeyedObjectPoolFactory(null,
-1, // unlimited maxActive (per key)
GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL,
0, // maxWait
1, // maxIdle (per key)
maxOpenPreparedStatements);
}
// Set up the driver connection factory we will use
if (username != null) {
connectionProperties.put("user", username);
} else {
log("DBCP DataSource configured without a 'username'");
}
if (password != null) {
connectionProperties.put("password", password);
} else {
log("DBCP DataSource configured without a 'password'");
}
DriverConnectionFactory driverConnectionFactory =
new DriverConnectionFactory(driver, url, connectionProperties);
// Set up the poolable connection factory we will use
PoolableConnectionFactory connectionFactory = null;
try {
connectionFactory =
new PoolableConnectionFactory(driverConnectionFactory,
connectionPool,
statementPoolFactory,
validationQuery,
defaultReadOnly,
defaultAutoCommit,
defaultTransactionIsolation,
defaultCatalog,
abandonedConfig);
if (connectionFactory == null) {
throw new SQLException("Cannot create PoolableConnectionFactory");
}
validateConnectionFactory(connectionFactory);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new SQLNestedException("Cannot create PoolableConnectionFactory (" + e.getMessage() + ")", e);
}
// Create and return the pooling data source to manage the connections
dataSource = new PoolingDataSource(connectionPool);
((PoolingDataSource) dataSource).setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
dataSource.setLogWriter(logWriter);
try {
for (int i = 0 ; i < initialSize ; i++) {
connectionPool.addObject();
}
} catch (Exception e) {
throw new SQLNestedException("Error preloading the connection pool", e);
}
return dataSource;
}