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

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

Introduction

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

Prototype

public GenericObjectPool(PooledObjectFactory<T> factory) 

Source Link

Document

Create a new GenericObjectPool using defaults from GenericObjectPoolConfig .

Usage

From source file:com.newlandframework.rpc.serialize.protostuff.ProtostuffSerializePool.java

public ProtostuffSerializePool(final int maxTotal, final int minIdle, final long maxWaitMillis,
        final long minEvictableIdleTimeMillis) {
    ProtostuffPool = new GenericObjectPool<ProtostuffSerialize>(new ProtostuffSerializeFactory());

    GenericObjectPoolConfig config = new GenericObjectPoolConfig();

    config.setMaxTotal(maxTotal);//from   www  . j a va2 s.c  om
    config.setMinIdle(minIdle);
    config.setMaxWaitMillis(maxWaitMillis);
    config.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

    ProtostuffPool.setConfig(config);
}

From source file:co.edu.unbosque.swii.PoolTest.java

@Test
public void quePasaCuandoSeCierraUnaConexionAntesDeRetornarla() throws Exception {
    FabricaConexiones fc = new FabricaConexiones("aretico.com", 5432, "software_2", "grupo7_5", pwd);
    ObjectPool<Connection> pool = new GenericObjectPool<Connection>(fc);
    for (int i = 0; i < 6; i++) {
        Connection c = pool.borrowObject();
        pool.returnObject(c);//from  w  ww.  jav  a  2 s  . com
    }
}

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 ww  . j a v a2  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(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:co.edu.unbosque.swii.PoolTest.java

@Test
public void quePasaCuandoSeRetornaUnaconexionContransaccionIniciada() throws Exception {
    FabricaConexiones fc = new FabricaConexiones("aretico.com", 5432, "software_2", "grupo7_5", pwd);
    ObjectPool<Connection> pool = new GenericObjectPool<Connection>(fc);
    Connection c = pool.borrowObject();
    c.close();/*www.j a  v a  2  s  . c  o  m*/
    pool.returnObject(c);
}

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);/*  ww  w. j  a  va 2 s . c o  m*/
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    poolingDataSource = new PoolingDataSource<>(connectionPool);
    return true;
}

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

public static void setDs(BasicDataSource datasource) {
    ///*from   ww  w. j a v a  2s .  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.
    //
    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.adaptris.core.services.splitter.ServiceWorkerPoolTest.java

@Test
public void testCloseQuietly() {
    closeQuietly((GenericObjectPool) null);
    closeQuietly(new GenericObjectPool(new DummyPooledObjectFactory()));
    closeQuietly(new GenericObjectPool(new DummyPooledObjectFactory()) {

        @Override/*w  ww.  ja  v a2  s . c om*/
        public void close() {
            throw new RuntimeException();
        }

    });
}

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

private ConnectionFactory() {
    try {// ww w  .  jav a  2  s .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:co.edu.unbosque.swii.PoolTest.java

@Test(threadPoolSize = 5, invocationCount = 5)
public void midaTiemposParaInsertar1000RegistrosConSingleton()
        throws ClassNotFoundException, SQLException, Exception {
    long tiempo = System.currentTimeMillis();
    String SQL;//from   w w  w . j  av a  2 s.  co m
    for (int x = 0; x < 1000; x++) {
        FabricaConexiones fc = new FabricaConexiones("aretico.com", 5432, "software_2", "grupo7_5", pwd);
        ObjectPool<Connection> pool = new GenericObjectPool<Connection>(fc);
        Connection c = pool.borrowObject();
        SQL = "INSERT INTO grupo7.RegistroHilos( registro, hilo, fecha) VALUES (?, ?, now());";
        PreparedStatement parametros = c.prepareStatement(SQL);
        parametros.setInt(1, x);
        parametros.setInt(2, (int) Thread.currentThread().getId());
        parametros.executeUpdate();
        pool.returnObject(c);
    }
    System.out.println("Tiempo de ejecucion " + (System.currentTimeMillis() - tiempo));
}

From source file:com.testtubebaby.artemismain.core.CompData.java

/**
 * Seals this CompData, preventing further attempts to modify it.
 * @return//from  w w  w.  ja  v  a 2  s.com
 *      whether this CompData has already been sealed.
 */
public CompData seal() {
    if (sealed == false) {
        sealed = true;
        compPools = new ObjectPool[compToInd.size()];
        int[] data = pooledCompIndexes.getData();
        int size = pooledCompIndexes.size();
        for (int i = 0; i < size; i++) {
            compPools[data[i]] = new GenericObjectPool<>(new ComponentFactory(indToComp.get(data[i])));
        }
    }
    return this;
}