List of usage examples for org.apache.commons.pool.impl GenericObjectPool setNumTestsPerEvictionRun
public synchronized void setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
From source file:edu.jhu.pha.vospace.DbPoolServlet.java
@Override public void init() throws ServletException { ServletContext context = this.getServletContext(); Configuration conf = (Configuration) context.getAttribute("configuration"); try {//from w w w . j a v a 2 s . co m Class.forName(conf.getString("db.driver")); } catch (ClassNotFoundException e) { logger.error(e); throw new ServletException(e); } GenericObjectPool pool = new GenericObjectPool(null); pool.setMinEvictableIdleTimeMillis(6 * 60 * 60 * 1000); pool.setTimeBetweenEvictionRunsMillis(30 * 60 * 1000); pool.setNumTestsPerEvictionRun(-1); DriverManagerConnectionFactory cf = new DriverManagerConnectionFactory(conf.getString("db.url"), conf.getString("db.login"), conf.getString("db.password")); PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, pool, null, "SELECT * FROM mysql.db", false, true); new PoolingDriver().registerPool("dbPool", pool); }
From source file:lineage2.commons.dbcp.BasicDataSource.java
/** * Constructor for BasicDataSource.//from w w w . j a v a2s . com * @param driver String * @param connectURI String * @param uname String * @param passwd String * @param maxActive int * @param maxIdle int * @param idleTimeOut int * @param idleTestPeriod int * @param poolPreparedStatements boolean */ public BasicDataSource(String driver, String connectURI, String uname, String passwd, int maxActive, int maxIdle, int idleTimeOut, int idleTestPeriod, boolean poolPreparedStatements) { GenericObjectPool<?> connectionPool = new GenericObjectPool<>(null); connectionPool.setMaxActive(maxActive); connectionPool.setMaxIdle(maxIdle); connectionPool.setMinIdle(1); connectionPool.setMaxWait(-1L); connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW); connectionPool.setTestOnBorrow(false); connectionPool.setTestWhileIdle(true); connectionPool.setTimeBetweenEvictionRunsMillis(idleTestPeriod * 1000L); connectionPool.setNumTestsPerEvictionRun(maxActive); connectionPool.setMinEvictableIdleTimeMillis(idleTimeOut * 1000L); GenericKeyedObjectPoolFactory<?, ?> statementPoolFactory = null; if (poolPreparedStatements) { statementPoolFactory = new GenericKeyedObjectPoolFactory<>(null, -1, GenericObjectPool.WHEN_EXHAUSTED_FAIL, 0L, 1, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL); } Properties connectionProperties = new Properties(); connectionProperties.put("user", uname); connectionProperties.put("password", passwd); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, connectionProperties); @SuppressWarnings("unused") PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, statementPoolFactory, "SELECT 1", false, true); PoolingDataSource dataSource = new PoolingDataSource(connectionPool); _connectionPool = connectionPool; _source = dataSource; }
From source file:com.nesscomputing.syslog4j.impl.pool.generic.GenericSyslogPoolFactory.java
protected void configureGenericObjectPool(GenericObjectPool<AbstractSyslogWriter> genericObjectPool) throws SyslogRuntimeException { SyslogPoolConfigIF poolConfig = null; try {// w w w. j ava2s . co m poolConfig = (SyslogPoolConfigIF) this.syslog.getConfig(); } catch (ClassCastException cce) { throw new SyslogRuntimeException("config must implement interface SyslogPoolConfigIF"); } genericObjectPool.setMaxActive(poolConfig.getMaxActive()); genericObjectPool.setMaxIdle(poolConfig.getMaxIdle()); genericObjectPool.setMaxWait(poolConfig.getMaxWait()); genericObjectPool.setMinEvictableIdleTimeMillis(poolConfig.getMinEvictableIdleTimeMillis()); genericObjectPool.setMinIdle(poolConfig.getMinIdle()); genericObjectPool.setNumTestsPerEvictionRun(poolConfig.getNumTestsPerEvictionRun()); genericObjectPool.setSoftMinEvictableIdleTimeMillis(poolConfig.getSoftMinEvictableIdleTimeMillis()); genericObjectPool.setTestOnBorrow(poolConfig.isTestOnBorrow()); genericObjectPool.setTestOnReturn(poolConfig.isTestOnReturn()); genericObjectPool.setTestWhileIdle(poolConfig.isTestWhileIdle()); genericObjectPool.setTimeBetweenEvictionRunsMillis(poolConfig.getTimeBetweenEvictionRunsMillis()); genericObjectPool.setWhenExhaustedAction(poolConfig.getWhenExhaustedAction()); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
private void checkEvictionOrder(boolean lifo) throws Exception { SimpleFactory factory = new SimpleFactory(); GenericObjectPool pool = new GenericObjectPool(factory); pool.setNumTestsPerEvictionRun(2); pool.setMinEvictableIdleTimeMillis(100); pool.setLifo(lifo);//from ww w. j a v a 2 s .co m for (int i = 0; i < 5; i++) { pool.addObject(); Thread.sleep(100); } // Order, oldest to youngest, is "0", "1", ...,"4" pool.evict(); // Should evict "0" and "1" Object obj = pool.borrowObject(); assertTrue("oldest not evicted", !obj.equals("0")); assertTrue("second oldest not evicted", !obj.equals("1")); // 2 should be next out for FIFO, 4 for LIFO assertEquals("Wrong instance returned", lifo ? "4" : "2", obj); // Two eviction runs in sequence factory = new SimpleFactory(); pool = new GenericObjectPool(factory); pool.setNumTestsPerEvictionRun(2); pool.setMinEvictableIdleTimeMillis(100); pool.setLifo(lifo); for (int i = 0; i < 5; i++) { pool.addObject(); Thread.sleep(100); } pool.evict(); // Should evict "0" and "1" pool.evict(); // Should evict "2" and "3" obj = pool.borrowObject(); assertEquals("Wrong instance remaining in pool", "4", obj); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
private void checkEvictorVisiting(boolean lifo) throws Exception { VisitTrackerFactory factory = new VisitTrackerFactory(); GenericObjectPool pool = new GenericObjectPool(factory); pool.setNumTestsPerEvictionRun(2); pool.setMinEvictableIdleTimeMillis(-1); pool.setTestWhileIdle(true);/*w w w .ja va 2 s . c o m*/ pool.setLifo(lifo); pool.setTestOnReturn(false); pool.setTestOnBorrow(false); for (int i = 0; i < 8; i++) { pool.addObject(); } pool.evict(); // Visit oldest 2 - 0 and 1 Object obj = pool.borrowObject(); pool.returnObject(obj); obj = pool.borrowObject(); pool.returnObject(obj); // borrow, return, borrow, return // FIFO will move 0 and 1 to end // LIFO, 7 out, then in, then out, then in pool.evict(); // Should visit 2 and 3 in either case for (int i = 0; i < 8; i++) { VisitTracker tracker = (VisitTracker) pool.borrowObject(); if (tracker.getId() >= 4) { assertEquals("Unexpected instance visited " + tracker.getId(), 0, tracker.getValidateCount()); } else { assertEquals("Instance " + tracker.getId() + " visited wrong number of times.", 1, tracker.getValidateCount()); } } factory = new VisitTrackerFactory(); pool = new GenericObjectPool(factory); pool.setNumTestsPerEvictionRun(3); pool.setMinEvictableIdleTimeMillis(-1); pool.setTestWhileIdle(true); pool.setLifo(lifo); pool.setTestOnReturn(false); pool.setTestOnBorrow(false); for (int i = 0; i < 8; i++) { pool.addObject(); } pool.evict(); // 0, 1, 2 pool.evict(); // 3, 4, 5 obj = pool.borrowObject(); pool.returnObject(obj); obj = pool.borrowObject(); pool.returnObject(obj); obj = pool.borrowObject(); pool.returnObject(obj); // borrow, return, borrow, return // FIFO 3,4,5,6,7,0,1,2 // LIFO 7,6,5,4,3,2,1,0 // In either case, pointer should be at 6 pool.evict(); // Should hit 6,7,0 - 0 for second time for (int i = 0; i < 8; i++) { VisitTracker tracker = (VisitTracker) pool.borrowObject(); if (tracker.getId() != 0) { assertEquals("Instance " + tracker.getId() + " visited wrong number of times.", 1, tracker.getValidateCount()); } else { assertEquals("Instance " + tracker.getId() + " visited wrong number of times.", 2, tracker.getValidateCount()); } } // Randomly generate a pools with random numTests // and make sure evictor cycles through elements appropriately int[] smallPrimes = { 2, 3, 5, 7 }; Random random = new Random(); random.setSeed(System.currentTimeMillis()); for (int i = 0; i < 4; i++) { pool.setNumTestsPerEvictionRun(smallPrimes[i]); for (int j = 0; j < 5; j++) { pool = new GenericObjectPool(factory); pool.setNumTestsPerEvictionRun(3); pool.setMinEvictableIdleTimeMillis(-1); pool.setTestWhileIdle(true); pool.setLifo(lifo); pool.setTestOnReturn(false); pool.setTestOnBorrow(false); pool.setMaxIdle(-1); int instanceCount = 10 + random.nextInt(20); pool.setMaxActive(instanceCount); for (int k = 0; k < instanceCount; k++) { pool.addObject(); } // Execute a random number of evictor runs int runs = 10 + random.nextInt(50); for (int k = 0; k < runs; k++) { pool.evict(); } // Number of times evictor should have cycled through the pool int cycleCount = (runs * pool.getNumTestsPerEvictionRun()) / instanceCount; // Look at elements and make sure they are visited cycleCount // or cycleCount + 1 times VisitTracker tracker = null; int visitCount = 0; for (int k = 0; k < instanceCount; k++) { tracker = (VisitTracker) pool.borrowObject(); assertTrue(pool.getNumActive() <= pool.getMaxActive()); visitCount = tracker.getValidateCount(); assertTrue(visitCount >= cycleCount && visitCount <= cycleCount + 1); } } } }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
public void testEvictionSoftMinIdle() throws Exception { GenericObjectPool pool = null; class TimeTest extends BasePoolableObjectFactory { private final long createTime; public TimeTest() { createTime = System.currentTimeMillis(); }//from w w w .ja v a 2 s . c om public Object makeObject() throws Exception { return new TimeTest(); } public long getCreateTime() { return createTime; } } pool = new GenericObjectPool(new TimeTest()); pool.setMaxIdle(5); pool.setMaxActive(5); pool.setNumTestsPerEvictionRun(5); pool.setMinEvictableIdleTimeMillis(3000L); pool.setSoftMinEvictableIdleTimeMillis(1000L); pool.setMinIdle(2); Object[] active = new Object[5]; Long[] creationTime = new Long[5]; for (int i = 0; i < 5; i++) { active[i] = pool.borrowObject(); creationTime[i] = new Long(((TimeTest) active[i]).getCreateTime()); } for (int i = 0; i < 5; i++) { pool.returnObject(active[i]); } // Soft evict all but minIdle(2) Thread.sleep(1500L); pool.evict(); assertEquals("Idle count different than expected.", 2, pool.getNumIdle()); // Hard evict the rest. Thread.sleep(2000L); pool.evict(); assertEquals("Idle count different than expected.", 0, pool.getNumIdle()); }
From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java
public void testSettersAndGetters() throws Exception { GenericObjectPool pool = new GenericObjectPool(); {// w ww .j av a 2 s . c om 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.apache.hadoop.hive.metastore.datasource.DbCPDataSourceProvider.java
@Override public DataSource create(Configuration hdpConfig) throws SQLException { LOG.debug("Creating dbcp connection pool for the MetaStore"); String driverUrl = DataSourceProvider.getMetastoreJdbcDriverUrl(hdpConfig); String user = DataSourceProvider.getMetastoreJdbcUser(hdpConfig); String passwd = DataSourceProvider.getMetastoreJdbcPasswd(hdpConfig); int maxPoolSize = hdpConfig.getInt(MetastoreConf.ConfVars.CONNECTION_POOLING_MAX_CONNECTIONS.getVarname(), ((Long) MetastoreConf.ConfVars.CONNECTION_POOLING_MAX_CONNECTIONS.getDefaultVal()).intValue()); long connectionTimeout = hdpConfig.getLong(CONNECTION_TIMEOUT_PROPERTY, 30000L); int connectionMaxIlde = hdpConfig.getInt(CONNECTION_MAX_IDLE_PROPERTY, GenericObjectPool.DEFAULT_MAX_IDLE); int connectionMinIlde = hdpConfig.getInt(CONNECTION_MIN_IDLE_PROPERTY, GenericObjectPool.DEFAULT_MIN_IDLE); boolean testOnBorrow = hdpConfig.getBoolean(CONNECTION_TEST_BORROW_PROPERTY, GenericObjectPool.DEFAULT_TEST_ON_BORROW); long evictionTimeMillis = hdpConfig.getLong(CONNECTION_MIN_EVICT_MILLIS_PROPERTY, GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS); boolean testWhileIdle = hdpConfig.getBoolean(CONNECTION_TEST_IDLEPROPERTY, GenericObjectPool.DEFAULT_TEST_WHILE_IDLE); long timeBetweenEvictionRuns = hdpConfig.getLong(CONNECTION_TIME_BETWEEN_EVICTION_RUNS_MILLIS, GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS); int numTestsPerEvictionRun = hdpConfig.getInt(CONNECTION_NUM_TESTS_PER_EVICTION_RUN, GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN); boolean testOnReturn = hdpConfig.getBoolean(CONNECTION_TEST_ON_RETURN, GenericObjectPool.DEFAULT_TEST_ON_RETURN); long softMinEvictableIdleTimeMillis = hdpConfig.getLong(CONNECTION_SOFT_MIN_EVICTABLE_IDLE_TIME, GenericObjectPool.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS); boolean lifo = hdpConfig.getBoolean(CONNECTION_LIFO, GenericObjectPool.DEFAULT_LIFO); GenericObjectPool objectPool = new GenericObjectPool(); objectPool.setMaxActive(maxPoolSize); objectPool.setMaxWait(connectionTimeout); objectPool.setMaxIdle(connectionMaxIlde); objectPool.setMinIdle(connectionMinIlde); objectPool.setTestOnBorrow(testOnBorrow); objectPool.setTestWhileIdle(testWhileIdle); objectPool.setMinEvictableIdleTimeMillis(evictionTimeMillis); objectPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRuns); objectPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun); objectPool.setTestOnReturn(testOnReturn); objectPool.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis); objectPool.setLifo(lifo);/* w ww . j ava2 s. c o m*/ ConnectionFactory connFactory = new DriverManagerConnectionFactory(driverUrl, user, passwd); // This doesn't get used, but it's still necessary, see // https://git1-us-west.apache.org/repos/asf?p=commons-dbcp.git;a=blob;f=doc/ManualPoolingDataSourceExample.java; // h=f45af2b8481f030b27364e505984c0eef4f35cdb;hb=refs/heads/DBCP_1_5_x_BRANCH new PoolableConnectionFactory(connFactory, objectPool, null, null, false, true); return new PoolingDataSource(objectPool); }
From source file:org.apache.ojb.broker.accesslayer.ConnectionFactoryDBCPImpl.java
protected ObjectPool createConnectionPool(GenericObjectPool.Config config, AbandonedConfig ac) { final GenericObjectPool connectionPool; final boolean doRemoveAbandoned = ac != null && ac.getRemoveAbandoned(); if (doRemoveAbandoned) { connectionPool = new AbandonedObjectPool(null, ac); } else {/*w w w .ja v a 2 s. com*/ connectionPool = new GenericObjectPool(); } connectionPool.setMaxActive(config.maxActive); connectionPool.setMaxIdle(config.maxIdle); connectionPool.setMinIdle(config.minIdle); connectionPool.setMaxWait(config.maxWait); connectionPool.setTestOnBorrow(config.testOnBorrow); connectionPool.setTestOnReturn(config.testOnReturn); connectionPool.setTimeBetweenEvictionRunsMillis(config.timeBetweenEvictionRunsMillis); connectionPool.setNumTestsPerEvictionRun(config.numTestsPerEvictionRun); connectionPool.setMinEvictableIdleTimeMillis(config.minEvictableIdleTimeMillis); connectionPool.setTestWhileIdle(config.testWhileIdle); return connectionPool; }
From source file:org.graylog2.syslog4j.impl.pool.generic.GenericSyslogPoolFactory.java
protected void configureGenericObjectPool(GenericObjectPool genericObjectPool) throws SyslogRuntimeException { SyslogPoolConfigIF poolConfig = null; try {//ww w.j a va 2 s .c o m poolConfig = (SyslogPoolConfigIF) this.syslog.getConfig(); } catch (ClassCastException cce) { throw new SyslogRuntimeException("config must implement interface SyslogPoolConfigIF"); } genericObjectPool.setMaxActive(poolConfig.getMaxActive()); genericObjectPool.setMaxIdle(poolConfig.getMaxIdle()); genericObjectPool.setMaxWait(poolConfig.getMaxWait()); genericObjectPool.setMinEvictableIdleTimeMillis(poolConfig.getMinEvictableIdleTimeMillis()); genericObjectPool.setMinIdle(poolConfig.getMinIdle()); genericObjectPool.setNumTestsPerEvictionRun(poolConfig.getNumTestsPerEvictionRun()); genericObjectPool.setSoftMinEvictableIdleTimeMillis(poolConfig.getSoftMinEvictableIdleTimeMillis()); genericObjectPool.setTestOnBorrow(poolConfig.isTestOnBorrow()); genericObjectPool.setTestOnReturn(poolConfig.isTestOnReturn()); genericObjectPool.setTestWhileIdle(poolConfig.isTestWhileIdle()); genericObjectPool.setTimeBetweenEvictionRunsMillis(poolConfig.getTimeBetweenEvictionRunsMillis()); genericObjectPool.setWhenExhaustedAction(poolConfig.getWhenExhaustedAction()); }