Example usage for org.apache.commons.pool ObjectPool getNumIdle

List of usage examples for org.apache.commons.pool ObjectPool getNumIdle

Introduction

In this page you can find the example usage for org.apache.commons.pool ObjectPool getNumIdle.

Prototype

int getNumIdle() throws UnsupportedOperationException;

Source Link

Usage

From source file:com.tera.common.database.factory.DatabaseFactoryStatistics.java

/**
 * Factory method that will use supplied database factory for statistics
 * //from  w w w  .j ava 2 s  .c  o  m
 * @param dbFactory
 * @return
 */
public static DatabaseFactoryStatistics getStats(DatabaseFactory dbFactory) {
    DatabaseFactoryStatistics stats = new DatabaseFactoryStatistics();
    if (dbFactory instanceof AbstractDatabaseFactory) {
        AbstractDatabaseFactory adbFactory = (AbstractDatabaseFactory) dbFactory;
        ObjectPool pool = adbFactory.getPool();
        stats.currentActiveConnections = pool.getNumActive();
        stats.currentIdleConnections = pool.getNumIdle();
    }
    return stats;
}

From source file:ManualPoolingDriverExample.java

public static void printDriverStats() throws Exception {
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
    ObjectPool connectionPool = driver.getConnectionPool("example");

    System.out.println("NumActive: " + connectionPool.getNumActive());
    System.out.println("NumIdle: " + connectionPool.getNumIdle());
}

From source file:com.meidusa.amoeba.net.poolable.MultipleLoadBalanceObjectPool.java

public int getNumIdle() throws UnsupportedOperationException {
    int idle = 0;
    for (ObjectPool pool : objectPools) {
        idle += pool.getNumIdle();
    }//from w ww. java 2s  .c o m
    return idle;
}

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

public void testUnsupportedOperations() throws Exception {
    if (!getClass().equals(TestBaseObjectPool.class)) {
        return; // skip redundant tests
    }/*from w w w .  jav  a 2 s .c om*/
    ObjectPool pool = new BaseObjectPool() {
        public Object borrowObject() {
            return null;
        }

        public void returnObject(Object obj) {
        }

        public void invalidateObject(Object obj) {
        }
    };

    assertTrue("Negative expected.", pool.getNumIdle() < 0);
    assertTrue("Negative expected.", pool.getNumActive() < 0);

    try {
        pool.clear();
        fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
        // expected
    }

    try {
        pool.addObject();
        fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
        // expected
    }

    try {
        pool.setFactory(null);
        fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
        // expected
    }
}

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

public void testPOFAddObjectUsage() throws Exception {
    final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory();
    final ObjectPool pool;
    try {//from   www . ja  v  a 2  s  . co m
        pool = makeEmptyPool(factory);
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    final List expectedMethods = new ArrayList();

    assertEquals(0, pool.getNumActive());
    assertEquals(0, pool.getNumIdle());
    // addObject should make a new object, pasivate it and put it in the pool
    pool.addObject();
    assertEquals(0, pool.getNumActive());
    assertEquals(1, pool.getNumIdle());
    expectedMethods.add(new MethodCall("makeObject").returned(ZERO));
    // StackObjectPool, SoftReferenceObjectPool also validate on add
    if (pool instanceof StackObjectPool || pool instanceof SoftReferenceObjectPool) {
        expectedMethods.add(new MethodCall("validateObject", ZERO).returned(Boolean.TRUE));
    }
    expectedMethods.add(new MethodCall("passivateObject", ZERO));
    assertEquals(expectedMethods, factory.getMethodCalls());

    //// Test exception handling of addObject
    reset(pool, factory, expectedMethods);

    // makeObject Exceptions should be propagated to client code from addObject
    factory.setMakeObjectFail(true);
    try {
        pool.addObject();
        fail("Expected addObject to propagate makeObject exception.");
    } catch (PrivateException pe) {
        // expected
    }
    expectedMethods.add(new MethodCall("makeObject"));
    assertEquals(expectedMethods, factory.getMethodCalls());

    clear(factory, expectedMethods);

    // passivateObject Exceptions should be propagated to client code from addObject
    factory.setMakeObjectFail(false);
    factory.setPassivateObjectFail(true);
    try {
        pool.addObject();
        fail("Expected addObject to propagate passivateObject exception.");
    } catch (PrivateException pe) {
        // expected
    }
    expectedMethods.add(new MethodCall("makeObject").returned(ONE));
    // StackObjectPool, SofReferenceObjectPool also validate on add
    if (pool instanceof StackObjectPool || pool instanceof SoftReferenceObjectPool) {
        expectedMethods.add(new MethodCall("validateObject", ONE).returned(Boolean.TRUE));
    }
    expectedMethods.add(new MethodCall("passivateObject", ONE));
    assertEquals(expectedMethods, factory.getMethodCalls());
}

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

public void testClosedPoolBehavior() throws Exception {
    final ObjectPool pool;
    try {//  ww  w .jav  a2 s .  c  o m
        pool = makeEmptyPool(new MethodCallPoolableObjectFactory());
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    Object o1 = pool.borrowObject();
    Object o2 = pool.borrowObject();

    pool.close();

    try {
        pool.addObject();
        fail("A closed pool must throw an IllegalStateException when addObject is called.");
    } catch (IllegalStateException ise) {
        // expected
    }

    try {
        pool.borrowObject();
        fail("A closed pool must throw an IllegalStateException when borrowObject is called.");
    } catch (IllegalStateException ise) {
        // expected
    }

    // The following should not throw exceptions just because the pool is closed.
    if (pool.getNumIdle() >= 0) {
        assertEquals("A closed pool shouldn't have any idle objects.", 0, pool.getNumIdle());
    }
    if (pool.getNumActive() >= 0) {
        assertEquals("A closed pool should still keep count of active objects.", 2, pool.getNumActive());
    }
    pool.returnObject(o1);
    if (pool.getNumIdle() >= 0) {
        assertEquals("returnObject should not add items back into the idle object pool for a closed pool.", 0,
                pool.getNumIdle());
    }
    if (pool.getNumActive() >= 0) {
        assertEquals("A closed pool should still keep count of active objects.", 1, pool.getNumActive());
    }
    pool.invalidateObject(o2);
    if (pool.getNumIdle() >= 0) {
        assertEquals("invalidateObject must not add items back into the idle object pool.", 0,
                pool.getNumIdle());
    }
    if (pool.getNumActive() >= 0) {
        assertEquals("A closed pool should still keep count of active objects.", 0, pool.getNumActive());
    }
    pool.clear();
    pool.close();
}

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

public void testPOFReturnObjectUsages() throws Exception {
    final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory();
    final ObjectPool pool;
    try {//from www  .j  a  va  2  s  .  c o  m
        pool = makeEmptyPool(factory);
    } catch (UnsupportedOperationException uoe) {
        return; // test not supported
    }
    final List expectedMethods = new ArrayList();
    Object obj;

    /// Test correct behavior code paths
    obj = pool.borrowObject();
    clear(factory, expectedMethods);

    // returned object should be passivated
    pool.returnObject(obj);
    // StackObjectPool, SoftReferenceObjectPool also validate on return
    if (pool instanceof StackObjectPool || pool instanceof SoftReferenceObjectPool) {
        expectedMethods.add(new MethodCall("validateObject", obj).returned(Boolean.TRUE));
    }
    expectedMethods.add(new MethodCall("passivateObject", obj));
    assertEquals(expectedMethods, factory.getMethodCalls());

    //// Test exception handling of returnObject
    reset(pool, factory, expectedMethods);
    pool.addObject();
    pool.addObject();
    pool.addObject();
    assertEquals(3, pool.getNumIdle());
    // passivateObject should swallow exceptions and not add the object to the pool
    obj = pool.borrowObject();
    pool.borrowObject();
    assertEquals(1, pool.getNumIdle());
    assertEquals(2, pool.getNumActive());
    clear(factory, expectedMethods);
    factory.setPassivateObjectFail(true);
    pool.returnObject(obj);
    // StackObjectPool, SoftReferenceObjectPool also validate on return
    if (pool instanceof StackObjectPool || pool instanceof SoftReferenceObjectPool) {
        expectedMethods.add(new MethodCall("validateObject", obj).returned(Boolean.TRUE));
    }
    expectedMethods.add(new MethodCall("passivateObject", obj));
    removeDestroyObjectCall(factory.getMethodCalls()); // The exact timing of destroyObject is flexible here.
    assertEquals(expectedMethods, factory.getMethodCalls());
    assertEquals(1, pool.getNumIdle()); // Not returned
    assertEquals(1, pool.getNumActive()); // But not in active count

    // destroyObject should swallow exceptions too
    reset(pool, factory, expectedMethods);
    obj = pool.borrowObject();
    clear(factory, expectedMethods);
    factory.setPassivateObjectFail(true);
    factory.setDestroyObjectFail(true);
    pool.returnObject(obj);
}

From source file:org.apache.jcs.auxiliary.disk.jdbc.JDBCDiskCachePoolAccess.java

/**
 * How many are idle in the pool.//  ww w  .  ja va2  s.c o  m
 * <p>
 * @return number idle
 */
public int getNumIdleInPool() {
    int numIdle = 0;
    try {
        PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(DRIVER_NAME);
        ObjectPool connectionPool = driver.getConnectionPool(this.getPoolName());

        if (log.isDebugEnabled()) {
            log.debug(connectionPool);
        }
        numIdle = connectionPool.getNumIdle();
    } catch (Exception e) {
        log.error(e);
    }
    return numIdle;
}

From source file:org.apache.ojb.broker.accesslayer.ConnectionFactoryPooledImpl.java

public Connection checkOutJdbcConnection(JdbcConnectionDescriptor jcd) throws LookupException {
    ObjectPool op = (ObjectPool) poolMap.get(jcd.getPBKey());
    if (op == null) {
        synchronized (poolSynch) {
            log.info("Create new connection pool:" + jcd);
            op = createConnectionPool(jcd);
            poolMap.put(jcd.getPBKey(), op);
        }/*  www .  j a  v a 2s . c  o  m*/
    }
    final Connection conn;
    try {
        conn = (Connection) op.borrowObject();
    } catch (NoSuchElementException e) {
        int active = 0;
        int idle = 0;
        try {
            active = op.getNumActive();
            idle = op.getNumIdle();
        } catch (Exception ignore) {
        }
        throw new LookupException("Could not borrow connection from pool, seems ObjectPool is exhausted."
                + " Active/Idle instances in pool=" + active + "/" + idle + ". "
                + JdbcConnectionDescriptor.class.getName() + ":  " + jcd, e);
    } catch (Exception e) {
        int active = 0;
        int idle = 0;
        try {
            active = op.getNumActive();
            idle = op.getNumIdle();
        } catch (Exception ignore) {
        }
        throw new LookupException("Could not borrow connection from pool." + " Active/Idle instances in pool="
                + active + "/" + idle + ". " + JdbcConnectionDescriptor.class.getName() + ":  " + jcd, e);
    }
    return conn;
}

From source file:org.eclipse.osee.jdbc.internal.PoolFactory.java

public Map<String, String> getPoolStats() {
    Map<String, String> stats = new LinkedHashMap<String, String>();

    PoolingDriver driver = poolingDriver.get();

    stats.put("db.pool.driver", poolConfiguration.getPoolConnectionDriver());

    String poolVersion = String.format("%s.%s", driver.getMajorVersion(), driver.getMinorVersion());
    stats.put("db.pool.version", poolVersion);

    String[] names = driver.getPoolNames();
    int count = 0;
    for (String name : names) {

        try {//w  w  w. j ava 2 s . co m
            ObjectPool<?> pool = driver.getConnectionPool(name);
            stats.put(String.format("db.pool.%s.id", count), name);
            stats.put(String.format("db.pool.%s.active", count), String.valueOf(pool.getNumActive()));
            stats.put(String.format("db.pool.%s.idle", count), String.valueOf(pool.getNumIdle()));
        } catch (SQLException ex) {
            // Do Nothing
        } finally {
            count++;
        }
    }
    return stats;
}