List of usage examples for org.apache.commons.pool.impl GenericObjectPool getNumIdle
public synchronized int getNumIdle()
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 2s.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: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 www. ja v a2 s . c om*/ // 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:ConnectionPoolBasics.java
public static void main(String args[]) throws Exception { GenericObjectPool gPool = new GenericObjectPool(); /*Class.forName("com.mysql.jdbc.Driver"); //from www . j a va 2 s . c o m DriverManagerConnectionFactory cf = new DriverManagerConnectionFactory( "jdbc:mysql://localhost/commons", "root", "");*/ Properties props = new Properties(); props.setProperty("Username", "root"); props.setProperty("Password", ""); ConnectionFactory cf = new DriverConnectionFactory(new com.mysql.jdbc.Driver(), "jdbc:mysql://localhost/commons", props); KeyedObjectPoolFactory kopf = new GenericKeyedObjectPoolFactory(null, 8); PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, gPool, kopf, null, false, true); for (int i = 0; i < 5; i++) { gPool.addObject(); } // PoolingDataSource pds = new PoolingDataSource(gPool); PoolingDriver pd = new PoolingDriver(); pd.registerPool("example", gPool); for (int i = 0; i < 5; i++) { gPool.addObject(); } Connection conn = java.sql.DriverManager.getConnection("jdbc:apache:commons:dbcp:example"); System.err.println("Connection: " + conn); //": Delegate: " + ((org.apache.commons.dbcp.PoolingConnection)conn).getDelegate()); // do some work with the connection PreparedStatement ps = conn.prepareStatement("Select * from customer where id = ?"); System.err.println("Active: " + gPool.getNumActive() + ", Idle: " + gPool.getNumIdle()); conn.close(); System.err.println("Active: " + gPool.getNumActive() + ", Idle: " + gPool.getNumIdle()); }
From source file:com.adaptris.core.services.splitter.ServiceWorkerPoolLegacyTest.java
@Test public void testWarmup() throws Exception { GenericObjectPool<ServiceWorkerPool.Worker> pool = createObjectPool(); warmup(pool);//from w ww . j a v a2 s . c o m assertEquals(10, pool.getNumIdle()); closeQuietly(pool); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
public void testExceptionOnPassivateDuringReturn() throws Exception { SimpleFactory factory = new SimpleFactory(); GenericObjectPool pool = new GenericObjectPool(factory); Object obj = pool.borrowObject(); factory.setThrowExceptionOnPassivate(true); pool.returnObject(obj);//from ww w . j av a 2 s.co m assertEquals(0, pool.getNumIdle()); pool.close(); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
public void testSetFactoryWithNoActiveObjects() throws Exception { GenericObjectPool pool = new GenericObjectPool(); pool.setMaxIdle(10);/* w w w . ja v a2s . co m*/ 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 testExceptionOnDestroyDuringReturn() throws Exception { SimpleFactory factory = new SimpleFactory(); factory.setThrowExceptionOnDestroy(true); GenericObjectPool pool = new GenericObjectPool(factory); pool.setTestOnReturn(true);/*from ww w . j a v a2s .co m*/ Object obj1 = pool.borrowObject(); pool.borrowObject(); factory.setValid(false); // Make validation fail pool.returnObject(obj1); assertEquals(1, pool.getNumActive()); assertEquals(0, pool.getNumIdle()); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
public void testExceptionOnDestroyDuringBorrow() throws Exception { SimpleFactory factory = new SimpleFactory(); factory.setThrowExceptionOnDestroy(true); GenericObjectPool pool = new GenericObjectPool(factory); pool.setTestOnBorrow(true);//from w ww.jav a 2 s . c o m pool.borrowObject(); factory.setValid(false); // Make validation fail on next borrow attempt try { pool.borrowObject(); fail("Expecting NoSuchElementException"); } catch (NoSuchElementException ex) { // expected } assertEquals(1, pool.getNumActive()); assertEquals(0, pool.getNumIdle()); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
public void testExceptionOnActivateDuringBorrow() throws Exception { SimpleFactory factory = new SimpleFactory(); GenericObjectPool pool = new GenericObjectPool(factory); Object obj1 = pool.borrowObject(); Object obj2 = pool.borrowObject(); pool.returnObject(obj1);/* w ww . ja v a 2 s .co m*/ pool.returnObject(obj2); factory.setThrowExceptionOnActivate(true); factory.setEvenValid(false); // Activation will now throw every other time // First attempt throws, but loop continues and second succeeds Object obj = pool.borrowObject(); assertEquals(1, pool.getNumActive()); assertEquals(0, pool.getNumIdle()); pool.returnObject(obj); factory.setValid(false); // Validation will now fail on activation when borrowObject returns // an idle instance, and then when attempting to create a new instance try { obj1 = pool.borrowObject(); fail("Expecting NoSuchElementException"); } catch (NoSuchElementException ex) { // expected } assertEquals(0, pool.getNumActive()); assertEquals(0, pool.getNumIdle()); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
public void checkEvict(boolean lifo) throws Exception { // yea this is hairy but it tests all the code paths in GOP.evict() final SimpleFactory factory = new SimpleFactory(); final GenericObjectPool pool = new GenericObjectPool(factory); pool.setSoftMinEvictableIdleTimeMillis(10); pool.setMinIdle(2);/* www .java 2 s . c o m*/ pool.setTestWhileIdle(true); pool.setLifo(lifo); PoolUtils.prefill(pool, 5); pool.evict(); factory.setEvenValid(false); factory.setOddValid(false); factory.setThrowExceptionOnActivate(true); pool.evict(); PoolUtils.prefill(pool, 5); factory.setThrowExceptionOnActivate(false); factory.setThrowExceptionOnPassivate(true); pool.evict(); factory.setThrowExceptionOnPassivate(false); factory.setEvenValid(true); factory.setOddValid(true); Thread.sleep(125); pool.evict(); assertEquals(2, pool.getNumIdle()); }