Example usage for org.apache.commons.dbcp2 PoolableConnectionFactory setPool

List of usage examples for org.apache.commons.dbcp2 PoolableConnectionFactory setPool

Introduction

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

Prototype

public synchronized void setPool(ObjectPool<PoolableConnection> pool) 

Source Link

Document

Sets the ObjectPool in which to pool Connection s.

Usage

From source file:ch.ethz.coss.nervous.pulse.sql.SqlConnection.java

private DataSource setup() {

    try {/*from w w w.  j av  a2  s  . c  om*/
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    } catch (Exception e) {
        e.printStackTrace();
        Log.getInstance().append(Log.FLAG_ERROR, "Error loading the SQL driver");
        return null;
    }

    ConnectionFactory cf = null;
    try {
        cf = new DriverManagerConnectionFactory("jdbc:mysql://" + hostname + ":" + port + "/" + database,
                username, password);
        // System.out.println("CF - " + cf.toString());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, null);

    ObjectPool<PoolableConnection> connPool = new GenericObjectPool<PoolableConnection>(pcf);

    pcf.setPool(connPool);
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<PoolableConnection>(connPool);
    // System.out.println("DataSource -- " + dataSource);
    return dataSource;
}

From source file:com.tealcube.minecraft.bukkit.facecore.database.MySqlDatabasePool.java

@Override
public boolean initialize() {
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
            "jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);/*from w w  w .j  ava2s  . c o m*/
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    poolingDataSource = new PoolingDataSource<>(connectionPool);
    return true;
}

From source file:br.com.hslife.orcamento.repository.ConnectionFactory.java

private ConnectionFactory() {
    try {/*w w w.ja  v a  2s . c  o  m*/
        Class.forName("com.mysql.jdbc.Driver");
    } catch (Exception e) {
        e.printStackTrace();
    }

    Properties properties = new Properties();
    properties.setProperty("user", "orcamento");
    properties.setProperty("password", "d1nh31r0"); // or get properties from some configuration file

    DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
            "jdbc:mysql://localhost:3306/orcamento", properties);

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);

    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);

    this.dataSource = dataSource;
}

From source file:com.github.brandtg.switchboard.MysqlReplicator.java

@Override
public void start() throws Exception {
    // DBCP2 pool
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcString, user, password);
    ObjectName poolName = new ObjectName(JdbcBasedLogIndex.class.getCanonicalName(),
            URLEncoder.encode(jdbcString, ENCODING), "replicatorConnectionPool");
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            poolName);/*from ww  w .  j a  va  2  s.c o m*/
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    this.dataSource = new PoolingDataSource<PoolableConnection>(connectionPool);
    LOG.info("Opened connection pool to {} as {}", jdbcString, user);

    // Replication applier
    applier = new MysqlReplicationApplier(inputStream, dataSource);

    super.start();
}

From source file:com.github.brandtg.switchboard.TestMysqlReplicationApplier.java

private PoolingDataSource<PoolableConnection> getDataSource() throws Exception {
    // DBCP2 pool
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbc, "root", "");
    ObjectName poolName = new ObjectName(JdbcBasedLogIndex.class.getCanonicalName(),
            URLEncoder.encode(jdbc, "UTF-8"), "replicatorConnectionPool");
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            poolName);//from  w  w  w  .j ava  2  s. com
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    return new PoolingDataSource<PoolableConnection>(connectionPool);
}

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

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

    ObjectName poolName = new ObjectName("no.difi.oxalis", "connectionPool", "TestPool");
    PoolableConnectionFactory factory = new PoolableConnectionFactory(driverConnectionFactory, poolName);

    GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<>(factory);
    factory.setPool(pool);

    pool.setMaxTotal(10);
    pool.setMaxWaitMillis(100);

    assertEquals(pool.getFactory(), factory);

    PoolableConnectionFactory pcf = (PoolableConnectionFactory) ((GenericObjectPool<?>) pool).getFactory();
    ObjectPool<PoolableConnection> pool1 = pcf.getPool();

    PoolingDataSource<PoolableConnection> poolingDataSource = new PoolingDataSource<>(pool);

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

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

    assertTrue(resultSet.next());
}

From source file:de.alexandria.cms.config.SpringConfigBackendDatabase.java

private ObjectPool<PoolableConnection> getObjectPool() {
    PoolableConnectionFactory poolableConnectionFactory = getPoolableConnectionFactory();
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);
    return connectionPool;
}

From source file:com.mirth.connect.donkey.server.data.jdbc.DBCPConnectionPool.java

public DBCPConnectionPool(String url, String username, String password, int maxConnections) {
    this.maxConnections = maxConnections;

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, username, password);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);/*from   w  w w.  j a  v a2 s  .  co  m*/
    poolableConnectionFactory.setDefaultAutoCommit(false);

    GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);
    connectionPool.setMaxTotal(maxConnections);
    connectionPool.setMaxIdle(maxConnections);

    poolableConnectionFactory.setPool(connectionPool);

    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<PoolableConnection>(
            connectionPool);
    dataSource.setAccessToUnderlyingConnectionAllowed(true);

    this.dataSource = dataSource;
}

From source file:io.dockstore.common.BasicPostgreSQL.java

public BasicPostgreSQL(HierarchicalINIConfiguration settings) {
    if (dataSource == null) {
        try {//ww  w .  j a v a  2  s  .co  m
            String nullConfigs = "";
            String host = settings.getString(Constants.POSTGRES_HOST);
            if (host == null) {
                nullConfigs += "postgresHost ";
            }

            String user = settings.getString(Constants.POSTGRES_USERNAME);
            if (user == null) {
                nullConfigs += "postgresUser ";
            }

            String pass = settings.getString(Constants.POSTGRES_PASSWORD);
            if (pass == null) {
                nullConfigs += "postgresPass ";
            }

            String db = settings.getString(Constants.POSTGRES_DBNAME);
            if (db == null) {
                nullConfigs += "postgresDBName ";
            }

            String maxConnections = settings.getString(Constants.POSTGRES_MAX_CONNECTIONS, "5");

            if (!nullConfigs.trim().isEmpty()) {
                throw new NullPointerException("The following configuration values are null: " + nullConfigs
                        + ". Please check your configuration file.");
            }

            Class.forName("org.postgresql.Driver");

            String url = "jdbc:postgresql://" + host + "/" + db;
            LOG.debug("PostgreSQL URL is: " + url);
            Properties props = new Properties();
            props.setProperty("user", user);
            props.setProperty("password", pass);
            // props.setProperty("ssl","true");
            props.setProperty("initialSize", "5");
            props.setProperty("maxActive", maxConnections);

            ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, props);
            PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
                    connectionFactory, null);
            poolableConnectionFactory.setValidationQuery("select count(*) from container;");
            ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
            poolableConnectionFactory.setPool(connectionPool);
            dataSource = new PoolingDataSource<>(connectionPool);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.github.brandtg.switchboard.JdbcBasedLogIndex.java

@Override
public void start() throws Exception {
    Class.forName(driverClass);/*  w  ww  .j  a va2  s.  com*/
    LOG.info("Loaded driver {}", driverClass);

    // DBCP2 pool
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionString, user, password);
    ObjectName poolName = new ObjectName(JdbcBasedLogIndex.class.getCanonicalName(),
            URLEncoder.encode(connectionString, ENCODING), "indexConnectionPool");
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            poolName);
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    this.dataSource = new PoolingDataSource<PoolableConnection>(connectionPool);
    LOG.info("Opened connection pool to {} as {}", connectionString, user);

    createTable();
}