Example usage for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool

List of usage examples for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool

Introduction

In this page you can find the example usage for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool.

Prototype

public GenericObjectPool(PoolableObjectFactory factory) 

Source Link

Document

Create a new GenericObjectPool using the specified factory.

Usage

From source file:DBCPDemo.java

public static void main(String args[]) throws Exception {

    // create a generic pool
    GenericObjectPool pool = new GenericObjectPool(null);

    // use the connection factory which will wraped by
    // the PoolableConnectionFactory
    DriverManagerConnectionFactory cf = new DriverManagerConnectionFactory(
            "jdbc:jtds:sqlserver://myserver:1433/tandem", "user", "pass");

    PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, pool, null, "SELECT * FROM mysql.db",
            false, true);/*from  ww w  .  java 2 s .co  m*/

    // register our pool and give it a name
    new PoolingDriver().registerPool("myPool", pool);

    // get a connection and test it
    Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:myPool");

    // now we can use this pool the way we want.
    System.err.println("Are we connected? " + !conn.isClosed());

    System.err.println("Idle Connections: " + pool.getNumIdle() + ", out of " + pool.getNumActive());

}

From source file:it.holiday69.phpwebserver.httpd.fastcgi.impl.PooledConnectionFactory.java

public PooledConnectionFactory(PoolableObjectFactory poolableObjectFactory) {
    this.pool = new GenericObjectPool(poolableObjectFactory);
}

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

/**
 * register the data source for H2 DB/*from w  w w . ja  v a2  s. c  o m*/
 *
 * @return pooling database object
 */

private static PoolingDataSource registerDataSource() {

    // create a database connection
    String user = "tethrnetbox";
    String password = "filepwd 45WJLnwhpA47EepT162hrVnDn3vYRvJhpZi0sVdvN9Sdsf";
    String connectionURI = "jdbc:h2:" + DB_PATH + "/tethrnetbox;CIPHER=AES";

    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(25);
    connectionPool.setTestOnBorrow(true);
    connectionPool.setMinIdle(2);
    connectionPool.setMaxWait(15000);
    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:io.awacs.agent.NettyChannelPool.java

NettyChannelPool(io.netty.bootstrap.Bootstrap bootstrap, List<InetSocketAddress> addresses) {
    PoolableObjectFactory<ChannelFuture> factory = createChannelFactory(bootstrap, addresses);
    this.pool = new GenericObjectPool<>(factory);
    this.pool.setTestOnBorrow(true);
}

From source file:com.github.autermann.matlab.server.MatlabInstancePool.java

public MatlabInstancePool(MatlabInstancePoolConfiguration config) {
    final InstanceFactory factory = new InstanceFactory(config.getInstanceConfig());
    this.pool = new GenericObjectPool<MatlabInstance>(factory);
    this.pool.setMaxActive(config.getNumThreads());
    this.pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
}

From source file:monasca.common.middleware.HttpClientFactory.java

HttpClientFactory(String host, int port, boolean useHttps, int timeout, boolean clientAuth, String keyStore,
        String keyPass, String trustStore, String trustPass, String adminToken, int maxActive,
        long timeBetweenEvictionRunsMillis, long minEvictableIdleTimeMillis) {
    clientPool = new HttpClientPoolFactory(host, port, useHttps, timeout, clientAuth, keyStore, keyPass,
            trustStore, trustPass, adminToken, maxActive, timeBetweenEvictionRunsMillis,
            minEvictableIdleTimeMillis);
    pool = new GenericObjectPool(clientPool);
}

From source file:com.totalchange.bunman.cddb.impl.CddbQuerierImpl.java

public CddbQuerierImpl(String hostname, int port, int maxConnections, long idleTimeout) {
    GenericObjectPool<CDDB> pool = new GenericObjectPool<CDDB>(new CddbPoolableObjectFactory(hostname, port));
    pool.setMaxActive(maxConnections);// w ww . j  a  v a 2 s.  co m
    pool.setMinEvictableIdleTimeMillis(idleTimeout);
    pool.setTimeBetweenEvictionRunsMillis(TIME_BETWEEN_EVICTION_RUNS);

    this.cddbPool = pool;
    this.executor = Executors.newFixedThreadPool(maxConnections);
}

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

/**
 * register the data source for H2 DB/*from ww w .j a  v  a 2  s  .  co  m*/
 *
 * @return pooling database object
 */

private static PoolingDataSource registerDataSource() {

    // create a database connection
    String user = "keybox";
    String password = "filepwd 45WJLnwhpA47EepT162hrVnDn3vYRvJhpZi0sVdvN9Sdsf";
    String connectionURI = "jdbc:h2:" + getDBPath() + "/keybox;CIPHER=AES";

    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:com.claresco.tinman.servlet.ConnectionPooling.java

/**
 * Constructor //from  w w w  . j a v  a 2  s . co m
 *
 * Params:
 *
 *
 */
public ConnectionPooling(String connectionURL, String userName, String password, String driverName)
        throws ClassNotFoundException, SQLException {
    Class.forName(driverName);

    Properties props = new Properties();
    props.setProperty("user", userName);
    props.setProperty("password", password);

    ObjectPool connectionPool = new GenericObjectPool(null);

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionURL, props);

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

    Class.forName("org.apache.commons.dbcp.PoolingDriver");
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(myPoolingDriverName);
    driver.registerPool(myPoolName, connectionPool);
}

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

private DatabaseConnectionPool() {
    try {/* w w w  .j a  v  a  2 s .c  o  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();
    }
}