Example usage for org.apache.commons.dbcp PoolableConnectionFactory PoolableConnectionFactory

List of usage examples for org.apache.commons.dbcp PoolableConnectionFactory PoolableConnectionFactory

Introduction

In this page you can find the example usage for org.apache.commons.dbcp PoolableConnectionFactory PoolableConnectionFactory.

Prototype

public PoolableConnectionFactory(ConnectionFactory connFactory, ObjectPool pool,
        KeyedObjectPoolFactory stmtPoolFactory, String validationQuery, boolean defaultReadOnly,
        boolean defaultAutoCommit) 

Source Link

Document

Create a new PoolableConnectionFactory.

Usage

From source file:com.aaasec.sigserv.cscommon.SqLiteConnectionPool.java

private static DataSource setupDataSource(String connectURI, String userName, String password) {
    ObjectPool connectionPool = new GenericObjectPool(null);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, userName, password);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);
    PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
    return dataSource;
}

From source file:edu.psu.citeseerx.dbcp.DBCPFactory.java

protected static DataSource setupDataSource(String connectURI, String username, String password) {

    org.apache.commons.pool.ObjectPool connectionPool = new GenericObjectPool(null);
    KeyedObjectPoolFactory stmtPoolFactory = null;
    //            new GenericKeyedObjectPoolFactory(null);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, username, password);

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, stmtPoolFactory, "select 1;", // validation query
            false, // default read/write
            false // default no autocommit
    );//from w  w w  .ja  v a  2s. c  o  m
    PoolingDataSource dataSource = new PoolingDataSource(connectionPool);

    return dataSource;

}

From source file:com.ec2box.manage.util.DSPool.java

/**
 * register the data source for H2 DB/*  w  ww.jav  a 2s .  c om*/
 *
 * @return pooling database object
 */

private static PoolingDataSource registerDataSource() {

    // create a database connection
    String user = "ec2box";
    String password = "filepwd 0WJLnwhpA47EepT1A4drVnDn3vYRvJhpZi0sVdvN9SmlbKw";
    String connectionURI = "jdbc:h2:" + getDBPath() + "/ec2box;CIPHER=AES;";

    if (StringUtils.isNotEmpty(DB_OPTIONS)) {
        connectionURI = connectionURI + DB_OPTIONS;
    }

    String validationQuery = "select 1";

    try {
        Class.forName("org.h2.Driver");
    } catch (ClassNotFoundException ex) {
        log.error(ex.toString(), ex);
    }

    GenericObjectPool connectionPool = new GenericObjectPool(null);

    connectionPool.setMaxActive(MAX_ACTIVE);
    connectionPool.setTestOnBorrow(TEST_ON_BORROW);
    connectionPool.setMinIdle(MIN_IDLE);
    connectionPool.setMaxWait(MAX_WAIT);
    connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionURI, user, password);

    new PoolableConnectionFactory(connectionFactory, connectionPool, null, validationQuery, false, true);

    return new PoolingDataSource(connectionPool);

}

From source file:eu.peppol.jdbc.OxalisDataSourceFactoryDbcpImplTest.java

/**
 * Verifies that we can create a pooled jdbc data source using the JDBC .jar-file supplied in the global configuration
 * file.//  w ww  . ja  va2s  . c  o m
 *
 * @throws Exception
 */
@Test
public void testLoadJdbcDriverUsingCustomClassLoader() throws Exception {
    ConnectionFactory driverConnectionFactory = createConnectionFactory(false);

    GenericObjectPool genericObjectPool = new GenericObjectPool(null);

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(driverConnectionFactory,
            genericObjectPool, null, "select 1", false, true);
    PoolingDataSource poolingDataSource = new PoolingDataSource(genericObjectPool);

    Connection connection = poolingDataSource.getConnection();
    assertNotNull(connection);

    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("select current_date()");

    assertTrue(resultSet.next());
}

From source file:com.springsource.insight.plugin.jdbc.PoolingConnectionTest.java

@Test
public void testOperationCollection() throws SQLException {
    DataSourceConnectionFactory connFactory = new DataSourceConnectionFactory(dataSource);
    ObjectPool connPool = new GenericObjectPool();
    PoolableConnectionFactory poolFactory = new PoolableConnectionFactory(connFactory, connPool, null, null,
            false, true);//  w ww  .  ja v a2  s . com
    PoolingDataSource poolDs = new PoolingDataSource(poolFactory.getPool());
    String sql = "select * from appointment where owner = 'Agim'";
    Connection c = poolDs.getConnection();
    try {
        PreparedStatement ps = c.prepareStatement(sql);
        try {
            System.out.println("Prepared statement=" + ps.getClass());

            ResultSet rs = ps.executeQuery();
            rs.close();
        } finally {
            ps.close();
        }
    } finally {
        c.close();
    }

    ArgumentCaptor<Operation> opCaptor = ArgumentCaptor.forClass(Operation.class);
    verify(spiedOperationCollector, times(3)).enter(opCaptor.capture());

    List<Operation> ops = opCaptor.getAllValues();
    assertEquals("Mismatched number of operations", 3, ops.size());
    assertSourceCodeLocation("top-op", ops.get(0),
            "org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper", "prepareStatement");
    assertSourceCodeLocation("mid-op", ops.get(1), "org.apache.commons.dbcp.DelegatingConnection",
            "prepareStatement");
    assertSourceCodeLocation("bottom-op", ops.get(2), "org.hsqldb.jdbc.jdbcConnection", "prepareStatement");
}

From source file:com.hangum.tadpole.engine.manager.transaction.DBCPConnectionManager.java

private DataSource makePool(final String userId, UserDBDAO userDB) {
    GenericObjectPool connectionPool = new GenericObjectPool();
    connectionPool.setMaxActive(2);//from  ww w  .  j  ava2  s.  c  om
    //      connectionPool.setWhenExhaustedAction((byte)1);
    //      connectionPool.setMaxWait(1000 * 60);                // 1.
    //      connectionPool.setTimeBetweenEvictionRunsMillis(3 * 1000);
    connectionPool.setTestWhileIdle(true);

    String passwdDecrypt = "";
    try {
        passwdDecrypt = CipherManager.getInstance().decryption(userDB.getPasswd());
    } catch (Exception e) {
        passwdDecrypt = userDB.getPasswd();
    }
    ConnectionFactory cf = new DriverManagerConnectionFactory(userDB.getUrl(), userDB.getUsers(),
            passwdDecrypt);

    PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, connectionPool, null, null, false, true);
    DataSource ds = new PoolingDataSource(connectionPool);
    mapDataSource.put(TadpoleSQLTransactionManager.getKey(userId, userDB), ds);

    return ds;
}

From source file:com.foxelbox.foxbukkit.database.DatabaseConnectionPool.java

private DatabaseConnectionPool() {
    try {/*from ww  w .j a v  a 2 s .co m*/
        Class.forName("com.mysql.jdbc.Driver");
    } catch (Exception e) {
        System.err.println("Error loading JBBC MySQL: " + e.toString());
    }

    GenericObjectPool connectionPool = new GenericObjectPool(null);
    connectionPool.setMaxActive(10);
    connectionPool.setMaxIdle(5);
    connectionPool.setTestOnBorrow(true);
    connectionPool.setTestOnReturn(true);
    connectionPool.setTestWhileIdle(true);

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
            FoxBukkit.instance.configuration.getValue("database-uri",
                    "jdbc:mysql://localhost:3306/foxbukkit_database"),
            FoxBukkit.instance.configuration.getValue("database-user", "root"),
            FoxBukkit.instance.configuration.getValue("database-password", "password"));
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, new StackKeyedObjectPoolFactory(), "SELECT 1", false, true);
    poolableConnectionFactory.setValidationQueryTimeout(3);

    dataSource = new PoolingDataSource(connectionPool);

    try {
        initialize();
    } catch (SQLException exc) {
        System.err.println("Error initializing MySQL Database");
        exc.printStackTrace();
    }
}

From source file:com.haulmont.yarg.loaders.factory.DefaultLoaderFactory.java

public static DataSource setupDataSource(String driver, String connectURI, String username, String password,
        Integer maxActive, Integer maxIdle, Integer maxWait) {
    try {//from   w w  w . j  a v a2 s . com
        Class.forName(driver);
        final AbandonedConfig config = new AbandonedConfig();
        config.setLogAbandoned(true);

        AbandonedObjectPool connectionPool = new AbandonedObjectPool(null, config);

        connectionPool.setMaxIdle(maxIdle);
        connectionPool.setMaxActive(maxActive);
        if (maxWait != null) {
            connectionPool.setMaxWait(maxWait);
        }

        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, username,
                password);

        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
                connectionPool, null, null, false, true);

        connectionPool.setFactory(poolableConnectionFactory);
        PoolingDataSource dataSource = new PoolingDataSource(connectionPool);

        return dataSource;
    } catch (ClassNotFoundException e) {
        throw new InitializationException("An error occurred during creation of new datasource object", e);
    }
}

From source file:com.qualogy.qafe.business.resource.rdb.DriverManagerDataSource.java

public void init(ApplicationContext context) {
    DriverManagerResource driverManagerResource = (DriverManagerResource) getBindResource();
    String userName = driverManagerResource.getUsername();
    String password = driverManagerResource.getPassword();
    String url = driverManagerResource.getUrl();
    String driverClassName = driverManagerResource.getDriverClassName();

    DataSource springDS = new org.springframework.jdbc.datasource.DriverManagerDataSource(url, userName,
            password);/*ww  w .ja va2s. c o m*/
    ((org.springframework.jdbc.datasource.DriverManagerDataSource) springDS)
            .setDriverClassName(driverClassName);

    GenericObjectPool pool = new GenericObjectPool();
    pool.setMinEvictableIdleTimeMillis(300000);
    pool.setTimeBetweenEvictionRunsMillis(60000);
    PoolableConnectionFactory connectionFactory = new PoolableConnectionFactory(
            new DataSourceConnectionFactory(springDS), pool, null, null, false, true);

    PoolingDataSource poolingDataSource = new PoolingDataSource(pool);
    poolingDataSource.setAccessToUnderlyingConnectionAllowed(true);
    setDataSource(poolingDataSource);

    postInit(context);
}

From source file:iudex.da.DataSourceFactory.java

public DataSource create(Map<String, String> params) {
    String className = params.remove("dsf.driver.class");
    if (className != null) {
        try {/*from w  w w .ja va  2  s  . c  o m*/
            Class<?> driverClass = Class.forName(className);
            assert (driverClass != null);
        } catch (ClassNotFoundException x) {
            throw new RuntimeException(x);
        }
    }
    setLogWriter();

    String uri = params.remove("dsf.uri");

    Properties props = new Properties();
    for (Map.Entry<String, String> e : params.entrySet()) {
        props.setProperty(e.getKey(), e.getValue());
    }
    DriverManagerConnectionFactory conFactory = new DriverManagerConnectionFactory(uri, props);

    ObjectPool conPool = new GenericObjectPool(null);
    //min, max, etc. connections

    // Sets self on conPool
    new PoolableConnectionFactory(conFactory, conPool, null, //stmtPoolFactory
            null, false, true);

    return new PoolingDataSource(conPool);
}