use of org.apache.tomcat.dbcp.dbcp2.PoolableConnection in project kie-wb-common by kiegroup.
the class DBCPDataSourceProvider method deploy.
@Override
public DataSourceDeploymentInfo deploy(DataSourceDef dataSourceDef) throws Exception {
DriverDef driverDef = null;
for (DriverDef _driverDef : driverProvider.getDeployments()) {
if (_driverDef.getUuid().equals(dataSourceDef.getDriverUuid())) {
driverDef = _driverDef;
break;
}
}
if (driverDef == null) {
throw new Exception("Required driver: " + dataSourceDef.getDriverUuid() + " is not deployed");
}
final URI uri = artifactResolver.resolve(driverDef.getGroupId(), driverDef.getArtifactId(), driverDef.getVersion());
if (uri == null) {
throw new Exception("Unable to get driver library artifact for driver: " + driverDef);
}
final Properties properties = new Properties();
properties.setProperty("user", dataSourceDef.getUser());
properties.setProperty("password", dataSourceDef.getPassword());
final URLConnectionFactory urlConnectionFactory = buildConnectionFactory(uri, driverDef.getDriverClass(), dataSourceDef.getConnectionURL(), properties);
// Connection Factory that the pool will use for creating connections.
ConnectionFactory connectionFactory = new DBCPConnectionFactory(urlConnectionFactory);
// Poolable connection factory
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
// The pool to be used by the ConnectionFactory
ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
// Set the factory's pool property to the owning pool
poolableConnectionFactory.setPool(connectionPool);
// Finally create DataSource
PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);
DataSourceDeploymentInfo deploymentInfo = new DataSourceDeploymentInfo(dataSourceDef.getUuid(), true, dataSourceDef.getUuid(), false);
deploymentRegistry.put(deploymentInfo.getDeploymentId(), new DBCPDataSource(dataSource));
deploymentInfos.put(deploymentInfo.getDeploymentId(), deploymentInfo);
deployedDataSources.put(deploymentInfo.getDeploymentId(), dataSourceDef);
return deploymentInfo;
}
use of org.apache.tomcat.dbcp.dbcp2.PoolableConnection in project tomee by apache.
the class DbcpNPEXAConnectionTest method check.
@Test
public void check() throws SQLException {
final Connection con = ejb.newConn();
// no NPE
con.close();
Assert.assertTrue("Connection was not closed", con.isClosed());
final GenericObjectPool<PoolableConnection> pool = GenericObjectPool.class.cast(Reflections.get(ds, "connectionPool"));
assertEquals(0, pool.getNumActive());
}
use of org.apache.tomcat.dbcp.dbcp2.PoolableConnection in project datanucleus-rdbms by datanucleus.
the class DBCP2BuiltinConnectionPoolFactory method createConnectionPool.
/* (non-Javadoc)
* @see org.datanucleus.store.rdbms.datasource.ConnectionPoolFactory#createConnectionPool(org.datanucleus.store.StoreManager)
*/
public ConnectionPool createConnectionPool(StoreManager storeMgr) {
// Load the database driver
String dbDriver = storeMgr.getConnectionDriverName();
if (!StringUtils.isWhitespace(dbDriver)) {
loadDriver(dbDriver, storeMgr.getNucleusContext().getClassLoaderResolver(null));
}
String dbURL = storeMgr.getConnectionURL();
PoolingDataSource ds = null;
GenericObjectPool<PoolableConnection> connectionPool;
try {
// Create a factory to be used by the pool to create the connections
Properties dbProps = getPropertiesForDriver(storeMgr);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(dbURL, dbProps);
// Wrap the connections and statements with pooled variants
PoolableConnectionFactory poolableCF = null;
poolableCF = new PoolableConnectionFactory(connectionFactory, null);
String testSQL = null;
if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_TEST_SQL)) {
testSQL = storeMgr.getStringProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_TEST_SQL);
poolableCF.setValidationQuery(testSQL);
}
if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_VALIDATION_TIMEOUT)) {
int validationTimeout = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_VALIDATION_TIMEOUT);
if (validationTimeout >= 0) {
poolableCF.setValidationQueryTimeout(validationTimeout);
}
}
// Create the actual pool of connections, and apply any properties
connectionPool = new GenericObjectPool(poolableCF);
poolableCF.setPool(connectionPool);
if (testSQL != null) {
connectionPool.setTestOnBorrow(true);
}
if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_IDLE)) {
int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_IDLE);
if (value > 0) {
connectionPool.setMaxIdle(value);
}
}
if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MIN_IDLE)) {
int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MIN_IDLE);
if (value > 0) {
connectionPool.setMinIdle(value);
}
}
if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_ACTIVE)) {
int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_ACTIVE);
if (value > 0) {
connectionPool.setMaxTotal(value);
}
}
if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_WAIT)) {
int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MAX_WAIT);
if (value > 0) {
connectionPool.setMaxWaitMillis(value);
}
}
if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_TIME_BETWEEN_EVICTOR_RUNS_MILLIS)) {
// how often should the evictor run (if ever, default is -1 = off)
int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_TIME_BETWEEN_EVICTOR_RUNS_MILLIS);
if (value > 0) {
connectionPool.setTimeBetweenEvictionRunsMillis(value);
// in each eviction run, evict at least a quarter of "maxIdle" connections
int maxIdle = connectionPool.getMaxIdle();
int numTestsPerEvictionRun = (int) Math.ceil((double) maxIdle / 4);
connectionPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
}
}
if (storeMgr.hasProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS)) {
// how long may a connection sit idle in the pool before it may be evicted
int value = storeMgr.getIntProperty(RDBMSPropertyNames.PROPERTY_CONNECTION_POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS);
if (value > 0) {
connectionPool.setMinEvictableIdleTimeMillis(value);
}
}
// Create the datasource
ds = new org.datanucleus.store.rdbms.datasource.dbcp2.PoolingDataSource(connectionPool);
} catch (Exception e) {
throw new DatastorePoolException("DBCP2", dbDriver, dbURL, e);
}
return new DBCPConnectionPool(ds, connectionPool);
}
use of org.apache.tomcat.dbcp.dbcp2.PoolableConnection in project athenz by yahoo.
the class AthenzDataSourceTest method testClearPoolConnectionsException.
@Test
public void testClearPoolConnectionsException() throws Exception {
ObjectPool<PoolableConnection> pool = Mockito.mock(ObjectPool.class);
Mockito.doThrow(new SQLException()).when(pool).clear();
AthenzDataSource dataSource = new AthenzDataSource(pool);
dataSource.clearPoolConnections();
}
use of org.apache.tomcat.dbcp.dbcp2.PoolableConnection in project logging-log4j2 by apache.
the class PoolingDriverConnectionSource method setupDriver.
private void setupDriver(final String connectionString, final PoolableConnectionFactoryConfig poolableConnectionFactoryConfig) throws SQLException {
//
// First, we'll create a ConnectionFactory that the
// pool will use to create Connections.
// We'll use the DriverManagerConnectionFactory,
// using the connect string passed in the command line
// arguments.
//
final Property[] properties = getProperties();
final char[] userName = getUserName();
final char[] password = getPassword();
final ConnectionFactory connectionFactory;
if (properties != null && properties.length > 0) {
if (userName != null || password != null) {
throw new SQLException("Either set the userName and password, or set the Properties, but not both.");
}
connectionFactory = new DriverManagerConnectionFactory(connectionString, toProperties(properties));
} else {
connectionFactory = new DriverManagerConnectionFactory(connectionString, toString(userName), toString(password));
}
//
// Next, we'll create the PoolableConnectionFactory, which wraps
// the "real" Connections created by the ConnectionFactory with
// the classes that implement the pooling functionality.
//
final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
if (poolableConnectionFactoryConfig != null) {
poolableConnectionFactoryConfig.init(poolableConnectionFactory);
}
//
// Now we'll need a ObjectPool that serves as the
// actual pool of connections.
//
// We'll use a GenericObjectPool instance, although
// any ObjectPool implementation will suffice.
//
@SuppressWarnings("resource") final ObjectPool<PoolableConnection> // This GenericObjectPool will be closed on shutdown
connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
// Set the factory's pool property to the owning pool
poolableConnectionFactory.setPool(connectionPool);
loadDriver(poolingDriverClassName);
final PoolingDriver driver = getPoolingDriver();
if (driver != null) {
getLogger().debug("Registering DBCP pool '{}' with pooling driver {}: {}", poolName, driver, connectionPool);
driver.registerPool(poolName, connectionPool);
}
//
// Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
// to access our pool of Connections.
//
}
Aggregations