List of usage examples for org.apache.commons.pool ObjectPool addObject
void addObject() throws Exception;
From source file:edu.illinois.enforcemop.examples.apache.pool.TestObjectPool.java
public void testClosedPoolBehavior() throws Exception { final ObjectPool pool; try {/*ww w . j av a 2s .co 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 testPOFAddObjectUsage() throws Exception { final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory(); final ObjectPool pool; try {//from www .j ava 2 s . c o 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.TestBaseObjectPool.java
public void testUnsupportedOperations() throws Exception { if (!getClass().equals(TestBaseObjectPool.class)) { return; // skip redundant tests }/* w w w. jav a2s. 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 testPOFBorrowObjectUsages() throws Exception { final MethodCallPoolableObjectFactory factory = new MethodCallPoolableObjectFactory(); final ObjectPool pool; try {/* w w w.java 2 s . co m*/ pool = makeEmptyPool(factory); } catch (UnsupportedOperationException uoe) { return; // test not supported } if (pool instanceof GenericObjectPool) { ((GenericObjectPool) pool).setTestOnBorrow(true); } final List expectedMethods = new ArrayList(); Object obj; /// Test correct behavior code paths // existing idle object should be activated and validated pool.addObject(); clear(factory, expectedMethods); obj = pool.borrowObject(); expectedMethods.add(new MethodCall("activateObject", ZERO)); expectedMethods.add(new MethodCall("validateObject", ZERO).returned(Boolean.TRUE)); assertEquals(expectedMethods, factory.getMethodCalls()); pool.returnObject(obj); //// Test exception handling of borrowObject reset(pool, factory, expectedMethods); // makeObject Exceptions should be propagated to client code from borrowObject factory.setMakeObjectFail(true); try { obj = pool.borrowObject(); fail("Expected borrowObject to propagate makeObject exception."); } catch (PrivateException pe) { // expected } expectedMethods.add(new MethodCall("makeObject")); assertEquals(expectedMethods, factory.getMethodCalls()); // when activateObject fails in borrowObject, a new object should be borrowed/created reset(pool, factory, expectedMethods); pool.addObject(); clear(factory, expectedMethods); factory.setActivateObjectFail(true); expectedMethods.add(new MethodCall("activateObject", obj)); try { obj = pool.borrowObject(); fail("Expecting NoSuchElementException"); } catch (NoSuchElementException ex) { // Expected - newly created object will also fail to activate } // Idle object fails activation, new one created, also fails expectedMethods.add(new MethodCall("makeObject").returned(ONE)); expectedMethods.add(new MethodCall("activateObject", ONE)); removeDestroyObjectCall(factory.getMethodCalls()); // The exact timing of destroyObject is flexible here. assertEquals(expectedMethods, factory.getMethodCalls()); // when validateObject fails in borrowObject, a new object should be borrowed/created reset(pool, factory, expectedMethods); pool.addObject(); clear(factory, expectedMethods); factory.setValidateObjectFail(true); expectedMethods.add(new MethodCall("activateObject", ZERO)); expectedMethods.add(new MethodCall("validateObject", ZERO)); try { obj = pool.borrowObject(); } catch (NoSuchElementException ex) { // Expected - newly created object will also fail to validate } // Idle object is activated, but fails validation. // New instance is created, activated and then fails validation expectedMethods.add(new MethodCall("makeObject").returned(ONE)); expectedMethods.add(new MethodCall("activateObject", ONE)); expectedMethods.add(new MethodCall("validateObject", ONE)); removeDestroyObjectCall(factory.getMethodCalls()); // The exact timing of destroyObject is flexible here. // Second activate and validate are missing from expectedMethods assertTrue(factory.getMethodCalls().containsAll(expectedMethods)); }
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 w ww. j a v a 2s . co 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:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
public void testAddObject() throws Exception { assertEquals("should be zero idle", 0, pool.getNumIdle()); pool.addObject();/*from w ww . j a v a 2 s . c o m*/ assertEquals("should be one idle", 1, pool.getNumIdle()); assertEquals("should be zero active", 0, pool.getNumActive()); Object obj = pool.borrowObject(); assertEquals("should be zero idle", 0, pool.getNumIdle()); assertEquals("should be one active", 1, pool.getNumActive()); pool.returnObject(obj); assertEquals("should be one idle", 1, pool.getNumIdle()); assertEquals("should be zero active", 0, pool.getNumActive()); ObjectPool op = new GenericObjectPool(); try { op.addObject(); fail("Expected IllegalStateException when there is no factory."); } catch (IllegalStateException ise) { // expected } op.close(); }
From source file:org.opensubsystems.core.persist.jdbc.connectionpool.dbcp.DBCPDatabaseConnectionFactoryImpl.java
/** * {@inheritDoc}/*from w w w. ja v a 2 s . com*/ */ @Override protected Object createConnectionPool(String strConnectionPoolName, Database database, String strDriverName, String strUrl, String strUser, String strPassword, int iTransactionIsolation) throws OSSException { // I am using here the PoolingDriver instead of PoolingDataSource because // in DBCP version 1.1 the PoolingDriver has clear way how to shutdown // the pool and PoolingDataSource doesn't. // This code was inspired by method setupDriver from // ManualPoolingDriverExample.java in commons-dbcp package v 1.6 ObjectPool connectionPool; ConnectionFactory connectionFactory; PoolableConnectionFactory poolableConnectionFactory; PooledDatabaseConnectionFactorySetupReader setupReader = new PooledDatabaseConnectionFactorySetupReader( strConnectionPoolName, database.getDatabaseTypeIdentifier()); int iInitialPoolSize = setupReader .getIntegerParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_INITIAL_SIZE) .intValue(); int iMinimalPoolSize = setupReader .getIntegerParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_MIN_SIZE).intValue(); int iMaximalPoolSize = setupReader .getIntegerParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_MAX_SIZE).intValue(); boolean bCanGrow = setupReader .getBooleanParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_CAN_GROW) .booleanValue(); long lMaxWaitTimeForConnection = setupReader .getLongParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_WAIT_PERIOD).longValue(); boolean bValidateOnBorrow = setupReader .getBooleanParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_VALIDATE_BORROW) .booleanValue(); boolean bValidateOnReturn = setupReader .getBooleanParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_VALIDATE_RETURN) .booleanValue(); boolean bValidateOnIdle = setupReader .getBooleanParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_VALIDATE_IDLE) .booleanValue(); long lTimeBetweenEvictionRunsMillis = setupReader .getLongParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_IDLE_CHECK_PERIOD) .longValue(); int iNumTestsPerEvictionRun = setupReader .getIntegerParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_IDLE_CHECK_SIZE) .intValue(); long lMinEvictableIdleTimeMillis = setupReader .getLongParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_IDLE_PERIOD).longValue(); int iPreparedStatementCacheSize = setupReader.getIntegerParameterValue( PooledDatabaseConnectionFactorySetupReader.DBPOOL_PREPSTATEMENT_CACHE_SIZE).intValue(); // First, 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. connectionPool = new GenericObjectPool(null, // factory will be specified below iMaximalPoolSize, bCanGrow ? GenericObjectPool.WHEN_EXHAUSTED_GROW : GenericObjectPool.WHEN_EXHAUSTED_BLOCK, lMaxWaitTimeForConnection, iMaximalPoolSize, // max idle - if no connections are used // the pool should not fall under this size iMinimalPoolSize, // min idle - if connection count falls // under this limit (e.g. closed connections) // new connections will be created bValidateOnBorrow, bValidateOnReturn, lTimeBetweenEvictionRunsMillis, iNumTestsPerEvictionRun, lMinEvictableIdleTimeMillis, bValidateOnIdle); // Next, we'll create a ConnectionFactory that the pool will use to // create Connections. I am using DriverManagerConnectionFactory instead // of DriverConnectionFactory because it allows me to specify user name // and password directly connectionFactory = new DriverManagerConnectionFactory(strUrl, strUser, strPassword); // This configuration of prepared statement caching is inspired by // Commons-DBCP Wiki available at http://wiki.apache.org/jakarta-commons/DBCP // null can be used as parameter because this parameter is set in // PoolableConnectionFactory when creating a new PoolableConnection // 0 according to documentation should mean no limit KeyedObjectPoolFactory statementPool = null; if (iPreparedStatementCacheSize >= 0) { statementPool = new GenericKeyedObjectPoolFactory(null, iPreparedStatementCacheSize); } // Now we'll create the PoolableConnectionFactory, which wraps // the "real" Connections created by the ConnectionFactory with // the classes that implement the pooling functionality. poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, statementPool, DatabaseImpl.getInstance().getConnectionTestStatement(), false, // not read-only connection false, // Default auto commit is false iTransactionIsolation); // PoolableConnectionFactory doesn't support the initialSize attribute of // DBCP so I have replicated the code from BasicDataSource v1.37 here try { for (int iIndex = 0; iIndex < iInitialPoolSize; iIndex++) { connectionPool.addObject(); } } catch (Exception e) { throw new OSSDatabaseAccessException("Error preloading the connection pool", e); } if (GlobalConstants.ERROR_CHECKING) { // Put this check here to trick checkstyle assert poolableConnectionFactory != null : "Poolable connection factory cannot be null."; } return connectionPool; }
From source file:org.quickserver.net.server.impl.BasicPoolManager.java
public void initPool(ObjectPool objectPool, PoolConfig opConfig) { int initSize = opConfig.getInitSize(); try {/* ww w. j a v a 2 s. c o m*/ while (objectPool.getNumIdle() < initSize) objectPool.addObject(); } catch (Exception e) { logger.log(Level.FINE, "Error: {0}", e); } }