List of usage examples for org.apache.commons.pool.impl GenericObjectPool setTimeBetweenEvictionRunsMillis
public synchronized void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
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);/*from w w w. ja v a2 s .c o 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:eu.peppol.jdbc.OxalisDataSourceFactoryDbcpImpl.java
/** * Creates a DataSource with connection pooling as provided by Apache DBCP * * @return a DataSource// w w w . j av a2s.com */ public static DataSource configureAndCreateDataSource() { log.debug("Configuring DataSource wrapped in a Database Connection Pool, using custom loader"); GlobalConfiguration globalConfiguration = GlobalConfigurationImpl.getInstance(); String jdbcDriverClassPath = globalConfiguration.getJdbcDriverClassPath(); log.debug("Loading JDBC Driver with custom class path: " + jdbcDriverClassPath); // Creates a new class loader, which will be used for loading our JDBC driver URLClassLoader urlClassLoader = getOxalisClassLoaderForJdbc(jdbcDriverClassPath); String className = globalConfiguration.getJdbcDriverClassName(); String connectURI = globalConfiguration.getJdbcConnectionURI(); String userName = globalConfiguration.getJdbcUsername(); String password = globalConfiguration.getJdbcPassword(); log.debug("className=" + className); log.debug("connectURI=" + connectURI); log.debug("userName=" + userName); log.debug("password=" + password); // Loads the JDBC Driver in a separate class loader Driver driver = getJdbcDriver(jdbcDriverClassPath, urlClassLoader, className); Properties properties = new Properties(); properties.put("user", userName); properties.put("password", password); // DBCP factory which will produce JDBC Driver instances ConnectionFactory driverConnectionFactory = new DriverConnectionFactory(driver, connectURI, properties); // DBCP object pool holding our driver connections GenericObjectPool genericObjectPool = new GenericObjectPool(null); genericObjectPool.setMaxActive(100); genericObjectPool.setMaxIdle(30); genericObjectPool.setMaxWait(10000); genericObjectPool.setTestOnBorrow(true); // Test the connection returned from the pool genericObjectPool.setTestWhileIdle(true); // Test idle instances visited by the pool maintenance thread and destroy any that fail validation genericObjectPool.setTimeBetweenEvictionRunsMillis(60 * 60 * 1000); // Test every hour // DBCP Factory holding the pooled connection, which are created by the driver connection factory and held in the supplied pool PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(driverConnectionFactory, genericObjectPool, null, null, false, true); String validationQuery = globalConfiguration.getValidationQuery(); poolableConnectionFactory.setValidationQuery(validationQuery); // Creates the actual DataSource instance PoolingDataSource poolingDataSource = new PoolingDataSource(genericObjectPool); return poolingDataSource; }
From source file:com.qualogy.qafe.business.resource.rdb.DriverManagerDataSource.java
public void init(ApplicationContext context) { DriverManagerResource driverManagerResource = (DriverManagerResource) getBindResource(); String userName = driverManagerResource.getUsername(); String password = driverManagerResource.getPassword(); String url = driverManagerResource.getUrl(); String driverClassName = driverManagerResource.getDriverClassName(); DataSource springDS = new org.springframework.jdbc.datasource.DriverManagerDataSource(url, userName, password);// ww w. j av a 2s.c o m ((org.springframework.jdbc.datasource.DriverManagerDataSource) springDS) .setDriverClassName(driverClassName); GenericObjectPool pool = new GenericObjectPool(); pool.setMinEvictableIdleTimeMillis(300000); pool.setTimeBetweenEvictionRunsMillis(60000); PoolableConnectionFactory connectionFactory = new PoolableConnectionFactory( new DataSourceConnectionFactory(springDS), pool, null, null, false, true); PoolingDataSource poolingDataSource = new PoolingDataSource(pool); poolingDataSource.setAccessToUnderlyingConnectionAllowed(true); setDataSource(poolingDataSource); postInit(context); }
From source file:com.totalchange.bunman.cddb.impl.CddbQuerierImpl.java
public CddbQuerierImpl(String hostname, int port, int maxConnections, long idleTimeout) { GenericObjectPool<CDDB> pool = new GenericObjectPool<CDDB>(new CddbPoolableObjectFactory(hostname, port)); pool.setMaxActive(maxConnections);//from w ww . j a va2 s . co m pool.setMinEvictableIdleTimeMillis(idleTimeout); pool.setTimeBetweenEvictionRunsMillis(TIME_BETWEEN_EVICTION_RUNS); this.cddbPool = pool; this.executor = Executors.newFixedThreadPool(maxConnections); }
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 a2 s .c o 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:com.google.acre.keystore.MySQLKeyStore.java
@SuppressWarnings("unused") private void setupDriver(String connectURI, String username, String password) throws SQLException, ClassNotFoundException { GenericObjectPool connectionPool = new GenericObjectPool(null); connectionPool.setTimeBetweenEvictionRunsMillis(5 * 60 * 1000 - 13); // check if jdbc connections are still alive every 5 min - 13 msec ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, username, password); new PoolableConnectionFactory(connectionFactory, connectionPool, null, TEST_QUERY, false, true); Class.forName("org.apache.commons.dbcp.PoolingDriver"); PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(DRIVER_PREFIX); driver.registerPool(DATASOURCE_NAME, connectionPool); active = true;/* w w w . ja v a 2s .co m*/ // Now we can just use the connect string "jdbc:apache:commons:dbcp:keystore" // to access our pool of Connections. }
From source file:lineage2.commons.dbcp.BasicDataSource.java
/** * Constructor for BasicDataSource./*from www . ja va 2 s .co m*/ * @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.zotoh.core.db.JDBCPoolManager.java
private synchronized JDBCPool create(String pool, JDBCInfo param, Properties props) throws SQLException { if (existsPool(pool)) { throw new SQLException("Jdbc Pool already exists: " + pool); }//from www. j a v a 2s . c om PoolableConnectionFactory pcf; DriverConnectionFactory dcf; GenericObjectPool gop; DBVendor dbv; ObjectPool p; Driver d; tlog().debug("JDBCPoolMgr: Driver : {}", param.getDriver()); tlog().debug("JDBCPoolMgr: URL : {}", param.getUrl()); // Ute.loadDriver(param.getDriver()); d = DriverManager.getDriver(param.getUrl()); dbv = DBUte.getDBVendor(param); dcf = new DriverConnectionFactory(d, param.getUrl(), props); gop = new GenericObjectPool(); gop.setMaxActive(asInt(props.getProperty("max-conns"), 10)); gop.setTestOnBorrow(true); gop.setMaxIdle(gop.getMaxActive()); gop.setMinIdle(asInt(props.getProperty("min-conns"), 2)); gop.setMaxWait(asLong(props.getProperty("max-wait4-conn-millis"), 1500L)); gop.setMinEvictableIdleTimeMillis(asLong(props.getProperty("evict-conn-ifidle-millis"), 300000L)); gop.setTimeBetweenEvictionRunsMillis(asLong(props.getProperty("check-evict-every-millis"), 60000L)); pcf = new PoolableConnectionFactory(dcf, gop, null, null, true, false); pcf.setDefaultReadOnly(false); p = pcf.getPool(); JDBCPool j = new JDBCPool(dbv, param, p); _ps.put(pool, j); tlog().debug("JDBCPoolMgr: Added db pool: {}, info= {}", pool, param); return j; }
From source file:com.nesscomputing.syslog4j.impl.pool.generic.GenericSyslogPoolFactory.java
protected void configureGenericObjectPool(GenericObjectPool<AbstractSyslogWriter> genericObjectPool) throws SyslogRuntimeException { SyslogPoolConfigIF poolConfig = null; try {/*from ww w.j av a 2 s.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
/** * Tests addObject contention between ensureMinIdle triggered by the Evictor * with minIdle > 0 and borrowObject. *//* w w w . j a v a 2 s . co m*/ public void testEvictAddObjects() throws Exception { SimpleFactory factory = new SimpleFactory(); factory.setMakeLatency(300); factory.setMaxActive(2); GenericObjectPool pool = new GenericObjectPool(factory); pool.setMaxActive(2); pool.setMinIdle(1); pool.borrowObject(); // numActive = 1, numIdle = 0 // Create a test thread that will run once and try a borrow after // 150ms fixed delay TestThread borrower = new TestThread(pool, 1, 150, false); Thread borrowerThread = new Thread(borrower); // Set evictor to run in 100 ms - will create idle instance pool.setTimeBetweenEvictionRunsMillis(100); borrowerThread.start(); // Off to the races borrowerThread.join(); assertTrue(!borrower.failed()); pool.close(); }