Search in sources :

Example 11 with ConnectionFactory

use of org.apache.tomcat.dbcp.dbcp2.ConnectionFactory in project Openfire by igniterealtime.

the class EmbeddedConnectionProvider method start.

@Override
public void start() {
    File databaseDir = new File(JiveGlobals.getHomeDirectory(), File.separator + "embedded-db");
    // If the database doesn't exist, create it.
    if (!databaseDir.exists()) {
        databaseDir.mkdirs();
    }
    try {
        serverURL = "jdbc:hsqldb:" + databaseDir.getCanonicalPath() + File.separator + "openfire";
    } catch (IOException ioe) {
        Log.error("EmbeddedConnectionProvider: Error starting connection pool: ", ioe);
    }
    final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(serverURL, "sa", "");
    final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    poolableConnectionFactory.setMaxConnLifetimeMillis((long) (0.5 * JiveConstants.DAY));
    final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMinIdle(3);
    poolConfig.setMaxTotal(25);
    final GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, poolConfig);
    poolableConnectionFactory.setPool(connectionPool);
    dataSource = new PoolingDataSource<>(connectionPool);
}
Also used : ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) IOException(java.io.IOException) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) File(java.io.File) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 12 with ConnectionFactory

use of org.apache.tomcat.dbcp.dbcp2.ConnectionFactory in project druid by druid-io.

the class BasicDataSourceExtTest method testCreateConnectionFactory.

@Test
public void testCreateConnectionFactory() throws Exception {
    MetadataStorageConnectorConfig connectorConfig = new MetadataStorageConnectorConfig() {

        private final List<String> passwords = Lists.newArrayList("pwd1", "pwd2");

        @Override
        public String getUser() {
            return "testuser";
        }

        @Override
        public String getPassword() {
            return passwords.remove(0);
        }
    };
    BasicDataSourceExt basicDataSourceExt = new BasicDataSourceExt(connectorConfig);
    basicDataSourceExt.setConnectionProperties("p1=v1");
    basicDataSourceExt.addConnectionProperty("p2", "v2");
    Driver driver = EasyMock.mock(Driver.class);
    Capture<String> uriArg = Capture.newInstance();
    Capture<Properties> propsArg = Capture.newInstance();
    EasyMock.expect(driver.connect(EasyMock.capture(uriArg), EasyMock.capture(propsArg))).andReturn(null).times(2);
    EasyMock.replay(driver);
    basicDataSourceExt.setDriver(driver);
    ConnectionFactory connectionFactory = basicDataSourceExt.createConnectionFactory();
    Properties expectedProps = new Properties();
    expectedProps.put("p1", "v1");
    expectedProps.put("p2", "v2");
    expectedProps.put("user", connectorConfig.getUser());
    Assert.assertNull(connectionFactory.createConnection());
    Assert.assertEquals(connectorConfig.getConnectURI(), uriArg.getValue());
    expectedProps.put("password", "pwd1");
    Assert.assertEquals(expectedProps, propsArg.getValue());
    Assert.assertNull(connectionFactory.createConnection());
    Assert.assertEquals(connectorConfig.getConnectURI(), uriArg.getValue());
    expectedProps.put("password", "pwd2");
    Assert.assertEquals(expectedProps, propsArg.getValue());
}
Also used : ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) Driver(java.sql.Driver) List(java.util.List) Properties(java.util.Properties) Test(org.junit.Test)

Example 13 with ConnectionFactory

use of org.apache.tomcat.dbcp.dbcp2.ConnectionFactory in project cloudstack by apache.

the class TransactionLegacy method getDefaultDataSource.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static DataSource getDefaultDataSource(final String database) {
    final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://localhost:3306/" + database, "cloud", "cloud");
    final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    final GenericObjectPool connectionPool = new GenericObjectPool(poolableConnectionFactory);
    return new PoolingDataSource(connectionPool);
}
Also used : ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 14 with ConnectionFactory

use of org.apache.tomcat.dbcp.dbcp2.ConnectionFactory in project tomcat by apache.

the class BasicManagedDataSource method createPoolableConnectionFactory.

/**
 * Creates the PoolableConnectionFactory and attaches it to the connection pool.
 *
 * @param driverConnectionFactory
 *            JDBC connection factory created by {@link #createConnectionFactory()}
 * @throws SQLException
 *             if an error occurs creating the PoolableConnectionFactory
 */
@Override
protected PoolableConnectionFactory createPoolableConnectionFactory(final ConnectionFactory driverConnectionFactory) throws SQLException {
    PoolableConnectionFactory connectionFactory = null;
    try {
        connectionFactory = new PoolableManagedConnectionFactory((XAConnectionFactory) driverConnectionFactory, getRegisteredJmxName());
        connectionFactory.setValidationQuery(getValidationQuery());
        connectionFactory.setValidationQueryTimeout(getValidationQueryTimeoutDuration());
        connectionFactory.setConnectionInitSql(getConnectionInitSqls());
        connectionFactory.setDefaultReadOnly(getDefaultReadOnly());
        connectionFactory.setDefaultAutoCommit(getDefaultAutoCommit());
        connectionFactory.setDefaultTransactionIsolation(getDefaultTransactionIsolation());
        connectionFactory.setDefaultCatalog(getDefaultCatalog());
        connectionFactory.setDefaultSchema(getDefaultSchema());
        connectionFactory.setCacheState(getCacheState());
        connectionFactory.setPoolStatements(isPoolPreparedStatements());
        connectionFactory.setClearStatementPoolOnReturn(isClearStatementPoolOnReturn());
        connectionFactory.setMaxOpenPreparedStatements(getMaxOpenPreparedStatements());
        connectionFactory.setMaxConn(getMaxConnDuration());
        connectionFactory.setRollbackOnReturn(getRollbackOnReturn());
        connectionFactory.setAutoCommitOnReturn(getAutoCommitOnReturn());
        connectionFactory.setDefaultQueryTimeout(getDefaultQueryTimeoutDuration());
        connectionFactory.setFastFailValidation(getFastFailValidation());
        connectionFactory.setDisconnectionSqlCodes(getDisconnectionSqlCodes());
        validateConnectionFactory(connectionFactory);
    } catch (final RuntimeException e) {
        throw e;
    } catch (final Exception e) {
        throw new SQLException("Cannot create PoolableConnectionFactory (" + e.getMessage() + ")", e);
    }
    return connectionFactory;
}
Also used : SQLException(java.sql.SQLException) PoolableConnectionFactory(org.apache.tomcat.dbcp.dbcp2.PoolableConnectionFactory) SQLException(java.sql.SQLException)

Aggregations

ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)11 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)9 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)7 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)7 SQLException (java.sql.SQLException)4 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)4 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)4 Properties (java.util.Properties)3 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)3 DataSourceConnectionFactory (org.apache.commons.dbcp2.DataSourceConnectionFactory)2 PoolingDriver (org.apache.commons.dbcp2.PoolingDriver)2 PoolableConnectionFactory (org.apache.tomcat.dbcp.dbcp2.PoolableConnectionFactory)2 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Driver (java.sql.Driver)1 List (java.util.List)1 Map (java.util.Map)1 CommonDataSource (javax.sql.CommonDataSource)1 DataSource (javax.sql.DataSource)1