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

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

Introduction

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

Prototype

public void setFactory(PoolableObjectFactory factory) throws IllegalStateException 

Source Link

Document

Sets the PoolableObjectFactory factory this pool uses to create new instances.

Usage

From source file:TestObjectPool.java

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

   GenericObjectPool pool = new GenericObjectPool();
   pool.setFactory(new EmployeeFactory());

   /*First way of initializing pool
   pool.setMinIdle(5);//w  ww .j  a v  a  2 s  .  co m
   pool.setTimeBetweenEvictionRunsMillis(500);
   Thread.currentThread().sleep(600);*/

   /* second, and preferred way */
   for(int i = 0; i < 5; ++i) {
      pool.addObject();
   }

   // pool.setTestOnReturn(true);
   pool.setMinEvictableIdleTimeMillis(1000);
   pool.setTimeBetweenEvictionRunsMillis(600);

   System.err.println("Number of employees in pool: " + pool.getNumIdle());

     Thread.currentThread().sleep(1500);

   Employee employee = (Employee)pool.borrowObject();

   employee.setName("Fred Flintstone");
   employee.doWork();

   System.err.println("Employee: "  + employee);

   pool.returnObject(employee);

     System.err.println("Number of employees in pool: " + pool.getNumIdle());
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java

public void testSetFactoryWithNoActiveObjects() throws Exception {
    GenericObjectPool pool = new GenericObjectPool();
    pool.setMaxIdle(10);//from  w w  w .jav a 2  s .com
    pool.setFactory(new SimpleFactory());
    Object obj = pool.borrowObject();
    pool.returnObject(obj);
    assertEquals(1, pool.getNumIdle());
    pool.setFactory(new SimpleFactory());
    assertEquals(0, pool.getNumIdle());
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java

public void testSetFactoryWithActiveObjects() throws Exception {
    GenericObjectPool pool = new GenericObjectPool();
    pool.setMaxIdle(10);//from   w  ww .  j av a  2 s.c o m
    pool.setFactory(new SimpleFactory());
    Object obj = pool.borrowObject();
    assertNotNull(obj);
    try {
        pool.setFactory(null);
        fail("Expected IllegalStateException");
    } catch (IllegalStateException e) {
        // expected
    }
    try {
        pool.setFactory(new SimpleFactory());
        fail("Expected IllegalStateException");
    } catch (IllegalStateException e) {
        // expected
    }
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java

public void testSettersAndGetters() throws Exception {
    GenericObjectPool pool = new GenericObjectPool();
    {//from   ww  w . j a  v a2  s.  c  o m
        pool.setFactory(new SimpleFactory());
    }
    {
        pool.setMaxActive(123);
        assertEquals(123, pool.getMaxActive());
    }
    {
        pool.setMaxIdle(12);
        assertEquals(12, pool.getMaxIdle());
    }
    {
        pool.setMaxWait(1234L);
        assertEquals(1234L, pool.getMaxWait());
    }
    {
        pool.setMinEvictableIdleTimeMillis(12345L);
        assertEquals(12345L, pool.getMinEvictableIdleTimeMillis());
    }
    {
        pool.setNumTestsPerEvictionRun(11);
        assertEquals(11, pool.getNumTestsPerEvictionRun());
    }
    {
        pool.setTestOnBorrow(true);
        assertTrue(pool.getTestOnBorrow());
        pool.setTestOnBorrow(false);
        assertTrue(!pool.getTestOnBorrow());
    }
    {
        pool.setTestOnReturn(true);
        assertTrue(pool.getTestOnReturn());
        pool.setTestOnReturn(false);
        assertTrue(!pool.getTestOnReturn());
    }
    {
        pool.setTestWhileIdle(true);
        assertTrue(pool.getTestWhileIdle());
        pool.setTestWhileIdle(false);
        assertTrue(!pool.getTestWhileIdle());
    }
    {
        pool.setTimeBetweenEvictionRunsMillis(11235L);
        assertEquals(11235L, pool.getTimeBetweenEvictionRunsMillis());
    }
    {
        pool.setSoftMinEvictableIdleTimeMillis(12135L);
        assertEquals(12135L, pool.getSoftMinEvictableIdleTimeMillis());
    }
    {
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
        assertEquals(GenericObjectPool.WHEN_EXHAUSTED_BLOCK, pool.getWhenExhaustedAction());
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);
        assertEquals(GenericObjectPool.WHEN_EXHAUSTED_FAIL, pool.getWhenExhaustedAction());
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
        assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, pool.getWhenExhaustedAction());
    }
}

From source file:org.openanzo.jdbc.container.RDBQuadStorePool.java

private void setupPool(GenericObjectPool pool, RDBQuadStoreFactory factory, int maxActive, int maxIdle,
        long blockWait) {
    pool.setFactory(factory);
    pool.setMaxActive(maxActive);//from   w  w w.j  a  va 2  s. co m
    pool.setMaxIdle(maxIdle);
    pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    pool.setMaxWait(blockWait);
}

From source file:org.openpythia.dbconnection.ConnectionPoolUtils.java

/**
 * Configures an Apache DBCP pool called POOL_NAME. For the given connectionUrl
 * and connectionProperties./*  w  ww  . java2s  .  c  o  m*/
 *
 * @param connectionUrl the connection url
 * @param connectionProperties  the connectionProperties
 */
public static void configurePool(String connectionUrl, Properties connectionProperties) throws SQLException {
    try {
        ConnectionFactory connectionFactory = new DriverConnectionFactory(JDBCHandler.getOracleJDBCDriver(),
                connectionUrl, connectionProperties);

        userName = connectionProperties.getProperty("user");

        GenericObjectPool connectionPool = new GenericObjectPool();
        connectionPool.setFactory(
                new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true));

        Class.forName(POOLING_DRIVER);

        PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(POOL_DRIVER_URL);
        driver.registerPool(POOL_NAME, connectionPool);

        Connection connection = DriverManager.getConnection(POOL_DRIVER_URL + POOL_NAME);
        connection.close();

        hasBeenSuccessfullyConfigured = true;
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }

}