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:com.linuxrouter.netcool.test.DbPoolTest.java

public static void main(String[] args) {
    String host = "192.168.0.201";
    String port = "4100";
    String dbName = "alerts";
    String url = "jdbc:sybase:Tds:" + host + ":" + port + "/" + dbName;
    Driver drv = new com.sybase.jdbc3.jdbc.SybDriver();
    try {/*w  ww.  ja  va  2  s  .  c o m*/
        DriverManager.registerDriver(drv);
    } catch (SQLException ex) {
        Logger.getLogger(DbPoolTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, "root", "omni12@#");
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    PoolingDataSource<PoolableConnection> poolingDataSource = new PoolingDataSource<>(connectionPool);
    try {
        Connection con = poolingDataSource.getConnection();
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery("select * from alerts.status");
        int x = 0;
        while (rs.next()) {
            //System.out.println(":::" + rs.getString(1));
            x++;
        }
        System.out.println("::::::" + x);
    } catch (SQLException ex) {
        Logger.getLogger(DbPoolTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:hu.neuron.java.jdbc.PoolingDataSourceExample.java

private static PoolingDataSource<PoolableConnection> setupDataSource() {
    ////from www .j  a va  2s. c  o  m
    // 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.
    //
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(URL, USER, PASS);

    //
    // Next we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    //
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);
    connectionPool = new GenericObjectPool<>(poolableConnectionFactory);

    connectionPool.setMaxTotal(10);
    connectionPool.setMinIdle(20);
    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself,
    // passing in the object pool we created.
    //
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);

    return dataSource;
}

From source file:JDBCPool.dbcp.demo.offical.PoolingDriverExample.java

/**
 * ?connectURI?PoolingDriver/*from   www.j  ava  2s.c om*/
 * @param connectURI
 * @throws Exception
 */
public static void setupDriver(String connectURI) throws Exception {

    // 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.
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, null);

    // Next, we'll create the PoolableConnectionFactory, which wraps the "real" Connections 
    // created by the ConnectionFactory with the classes that implement the pooling functionality.
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);

    // 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.
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);

    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself...
    //
    Class.forName("org.apache.commons.dbcp2.PoolingDriver");
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");

    //
    // ...and register our pool with it.
    //
    driver.registerPool("example", connectionPool);

    //
    // Now we can just use the connect string "jdbc:apache:commons:dbcp:example" to access our pool of Connections.
    //
}

From source file:PoolingDataSourceExample.java

public static DataSource setupDataSource(String connectURI) {
    ///*from w  w  w  . ja v  a  2 s  .  co  m*/
    // 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.
    //
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, null);

    //
    // Next we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    //
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);

    //
    // 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.
    //
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);

    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself,
    // passing in the object pool we created.
    //
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);

    return dataSource;
}

From source file:dgw.mt940.db.util.PoolingDataSourceExample.java

public static DataSource setupDataSource(String connectURI) {
    ///* w  w  w.  j av a  2s.co  m*/
    // 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.
    //
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, null);

    //
    // Next we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    //
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);

    //
    // 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.
    //
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);

    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself,
    // passing in the object pool we created.
    //
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);

    return dataSource;
}

From source file:PoolingDriverExample.java

public static void setupDriver(String connectURI) throws Exception {
    ///*from   w  w w .  ja  va2  s  .  c  om*/
    // 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.
    //
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, null);

    //
    // Next, we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    //
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);

    //
    // 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.
    //
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);

    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself...
    //
    Class.forName("org.apache.commons.dbcp2.PoolingDriver");
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");

    //
    // ...and register our pool with it.
    //
    driver.registerPool("example", connectionPool);

    //
    // Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
    // to access our pool of Connections.
    //
}

From source file:com.parallax.server.blocklyprop.db.utils.DataSourceSetup.java

public static PoolingDataSource connect(Configuration configuration) throws ClassNotFoundException {
    String driver = configuration.getString("database.driver");
    String url = configuration.getString("database.url");
    String username = configuration.getString("database.username");
    String password = configuration.getString("database.password");

    Class.forName(driver);//from   w w w  .  ja v a  2  s  .  c  om

    //
    // 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.
    //
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, username, password);

    //
    // Next we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    //
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);
    poolableConnectionFactory.setValidationQuery("SELECT 1");
    poolableConnectionFactory.setMaxConnLifetimeMillis(5000);

    //
    // 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.
    //
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);

    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself,
    // passing in the object pool we created.
    //
    PoolingDataSource<PoolableConnection> dataSourceInstance = new PoolingDataSource<>(connectionPool);

    for (NeedsDataSource dataSourceUser : dataSourceUsers) {
        dataSourceUser.setDataSource(dataSourceInstance);
    }
    DataSourceSetup.dataSource = dataSourceInstance;
    return dataSourceInstance;
}

From source file:edumsg.core.PostgresConnection.java

public static void initSource() {
    try {/*from ww w . j a va  2s. c om*/
        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException ex) {
            LOGGER.log(Level.SEVERE, "Error loading Postgres driver: " + ex.getMessage(), ex);
        }
        try {
            readConfFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Properties props = new Properties();
        //  System.out.println(DB_USERNAME);
        props.setProperty("user", DB_USERNAME);
        props.setProperty("password", DB_PASSWORD);
        props.setProperty("initialSize", DB_INIT_CONNECTIONS);
        props.setProperty("maxActive", DB_MAX_CONNECTIONS);

        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(DB_URL, props);
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
                null);
        poolableConnectionFactory.setPoolStatements(true);

        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxIdle(Integer.parseInt(DB_INIT_CONNECTIONS));
        poolConfig.setMaxTotal(Integer.parseInt(DB_MAX_CONNECTIONS));
        ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory,
                poolConfig);
        poolableConnectionFactory.setPool(connectionPool);

        Class.forName("org.apache.commons.dbcp2.PoolingDriver");
        dbDriver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
        dbDriver.registerPool(DB_NAME, connectionPool);

        dataSource = new PoolingDataSource<>(connectionPool);
    } catch (Exception ex) {
        LOGGER.log(Level.SEVERE, "Got error initializing data source: " + ex.getMessage(), ex);
    }
}

From source file:com.ebay.pulsar.analytics.dao.DBFactory.java

public static void setDs(BasicDataSource datasource) {
    ////w ww.  j  ava 2 s  .co m
    // 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.
    //
    try {
        Class.forName(datasource.getDriverClassName());
    } catch (ClassNotFoundException e) {
    }
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(datasource.getUrl(),
            datasource.getUsername(), datasource.getPassword());

    //
    // Next we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    //
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);

    //
    // 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.
    //
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);

    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself,
    // passing in the object pool we created.
    //
    PoolingDataSource<PoolableConnection> poolingDS = new PoolingDataSource<>(connectionPool);
    ds = poolingDS;
}

From source file:com.yahoo.athenz.common.server.db.DataSourceFactory.java

static PoolableDataSource create(ConnectionFactory connectionFactory) {

    // setup our pool config object

    GenericObjectPoolConfig config = setupPoolConfig();

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);// w w w .j ava2 s  .c  o m

    // Set max lifetime of a connection in milli-secs, after which it will
    // always fail activation, passivation, and validation.
    // Value of -1 means infinite life time. The default value
    // defined in this class is 10 minutes.
    long connTtlMillis = retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_TTL, MAX_TTL_CONN_MS);
    poolableConnectionFactory.setMaxConnLifetimeMillis(connTtlMillis);
    if (LOG.isInfoEnabled()) {
        LOG.info("Setting Time-To-Live interval for live connections (" + connTtlMillis + ") milli-secs");
    }

    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, config);
    poolableConnectionFactory.setPool(connectionPool);

    AthenzDataSource dataSource = new AthenzDataSource(connectionPool);
    return dataSource;
}