List of usage examples for org.apache.commons.pool2.impl GenericObjectPoolConfig setNumTestsPerEvictionRun
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
From source file:org.jooby.jedis.Redis.java
private GenericObjectPoolConfig poolConfig(final Config config) { GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setBlockWhenExhausted(config.getBoolean("blockWhenExhausted")); poolConfig.setEvictionPolicyClassName(config.getString("evictionPolicyClassName")); poolConfig.setJmxEnabled(config.getBoolean("jmxEnabled")); poolConfig.setJmxNamePrefix(config.getString("jmxNamePrefix")); poolConfig.setLifo(config.getBoolean("lifo")); poolConfig.setMaxIdle(config.getInt("maxIdle")); poolConfig.setMaxTotal(config.getInt("maxTotal")); poolConfig.setMaxWaitMillis(config.getDuration("maxWait", TimeUnit.MILLISECONDS)); poolConfig.setMinEvictableIdleTimeMillis(config.getDuration("minEvictableIdle", TimeUnit.MILLISECONDS)); poolConfig.setMinIdle(config.getInt("minIdle")); poolConfig.setNumTestsPerEvictionRun(config.getInt("numTestsPerEvictionRun")); poolConfig.setSoftMinEvictableIdleTimeMillis( config.getDuration("softMinEvictableIdle", TimeUnit.MILLISECONDS)); poolConfig.setTestOnBorrow(config.getBoolean("testOnBorrow")); poolConfig.setTestOnReturn(config.getBoolean("testOnReturn")); poolConfig.setTestWhileIdle(config.getBoolean("testWhileIdle")); poolConfig.setTimeBetweenEvictionRunsMillis( config.getDuration("timeBetweenEvictionRuns", TimeUnit.MILLISECONDS)); return poolConfig; }
From source file:org.noerp.entity.connection.DBCPConnectionFactory.java
public Connection getConnection(GenericHelperInfo helperInfo, JdbcElement abstractJdbc) throws SQLException, GenericEntityException { String cacheKey = helperInfo.getHelperFullName(); ManagedDataSource mds = dsCache.get(cacheKey); if (mds != null) { return TransactionUtil.getCursorConnection(helperInfo, mds.getConnection()); }// ww w . j a v a 2s .c o m if (!(abstractJdbc instanceof InlineJdbc)) { throw new GenericEntityConfException( "DBCP requires an <inline-jdbc> child element in the <datasource> element"); } InlineJdbc jdbcElement = (InlineJdbc) abstractJdbc; // connection properties TransactionManager txMgr = TransactionFactoryLoader.getInstance().getTransactionManager(); String driverName = jdbcElement.getJdbcDriver(); String jdbcUri = helperInfo.getOverrideJdbcUri(jdbcElement.getJdbcUri()); String jdbcUsername = helperInfo.getOverrideUsername(jdbcElement.getJdbcUsername()); String jdbcPassword = helperInfo.getOverridePassword(EntityConfig.getJdbcPassword(jdbcElement)); // pool settings int maxSize = jdbcElement.getPoolMaxsize(); int minSize = jdbcElement.getPoolMinsize(); int maxIdle = jdbcElement.getIdleMaxsize(); // maxIdle must be greater than pool-minsize maxIdle = maxIdle > minSize ? maxIdle : minSize; // load the driver Driver jdbcDriver; synchronized (DBCPConnectionFactory.class) { // Sync needed for MS SQL JDBC driver. See OFBIZ-5216. try { jdbcDriver = (Driver) Class .forName(driverName, true, Thread.currentThread().getContextClassLoader()).newInstance(); } catch (Exception e) { Debug.logError(e, module); throw new GenericEntityException(e.getMessage(), e); } } // connection factory properties Properties cfProps = new Properties(); cfProps.put("user", jdbcUsername); cfProps.put("password", jdbcPassword); // create the connection factory org.apache.commons.dbcp2.ConnectionFactory cf = new DriverConnectionFactory(jdbcDriver, jdbcUri, cfProps); // wrap it with a LocalXAConnectionFactory XAConnectionFactory xacf = new LocalXAConnectionFactory(txMgr, cf); // create the pool object factory PoolableConnectionFactory factory = new PoolableManagedConnectionFactory(xacf, null); factory.setValidationQuery(jdbcElement.getPoolJdbcTestStmt()); factory.setDefaultReadOnly(false); String transIso = jdbcElement.getIsolationLevel(); if (!transIso.isEmpty()) { if ("Serializable".equals(transIso)) { factory.setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); } else if ("RepeatableRead".equals(transIso)) { factory.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); } else if ("ReadUncommitted".equals(transIso)) { factory.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); } else if ("ReadCommitted".equals(transIso)) { factory.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); } else if ("None".equals(transIso)) { factory.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE); } } // configure the pool settings GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMaxTotal(maxSize); // settings for idle connections poolConfig.setMaxIdle(maxIdle); poolConfig.setMinIdle(minSize); poolConfig.setTimeBetweenEvictionRunsMillis(jdbcElement.getTimeBetweenEvictionRunsMillis()); poolConfig.setMinEvictableIdleTimeMillis(-1); // disabled in favour of setSoftMinEvictableIdleTimeMillis(...) poolConfig.setSoftMinEvictableIdleTimeMillis(jdbcElement.getSoftMinEvictableIdleTimeMillis()); poolConfig.setNumTestsPerEvictionRun(maxSize); // test all the idle connections // settings for when the pool is exhausted poolConfig.setBlockWhenExhausted(true); // the thread requesting the connection waits if no connection is available poolConfig.setMaxWaitMillis(jdbcElement.getPoolSleeptime()); // throw an exception if, after getPoolSleeptime() ms, no connection is available for the requesting thread // settings for the execution of the validation query poolConfig.setTestOnCreate(jdbcElement.getTestOnCreate()); poolConfig.setTestOnBorrow(jdbcElement.getTestOnBorrow()); poolConfig.setTestOnReturn(jdbcElement.getTestOnReturn()); poolConfig.setTestWhileIdle(jdbcElement.getTestWhileIdle()); GenericObjectPool pool = new GenericObjectPool(factory, poolConfig); factory.setPool(pool); mds = new ManagedDataSource(pool, xacf.getTransactionRegistry()); //mds = new DebugManagedDataSource(pool, xacf.getTransactionRegistry()); // Useful to debug the usage of connections in the pool mds.setAccessToUnderlyingConnectionAllowed(true); // cache the pool dsCache.putIfAbsent(cacheKey, mds); mds = dsCache.get(cacheKey); return TransactionUtil.getCursorConnection(helperInfo, mds.getConnection()); }
From source file:org.power.commons.redis.RedisClient.java
private GenericObjectPoolConfig getPoolConfig() { GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); // maxIdle?pool size???????redis poolConfig.setMaxTotal(this.maxTotal); if (this.maxIdle > 0) { poolConfig.setMaxIdle(this.maxIdle); }//from w w w.j a v a 2s .c o m poolConfig.setMaxWaitMillis(this.maxWait); /* if (this.whenExhaustedAction >= 0 && this.whenExhaustedAction < 3) { poolConfig.whenExhaustedAction = this.whenExhaustedAction; }*/ poolConfig.setBlockWhenExhausted(this.blockWhenExhausted); poolConfig.setTestOnBorrow(this.testOnBorrow); poolConfig.setMinIdle(this.minIdle); poolConfig.setTestOnReturn(testOnReturn); poolConfig.setTestWhileIdle(testWhileIdle); poolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); poolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun); poolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); poolConfig.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis); poolConfig.setLifo(lifo); return poolConfig; }
From source file:redis.client.jedis.CustomShardedJedisPoolTest.java
@BeforeClass public void init() throws InterruptedException { List<JedisShardInfo> shards = RedisConfigUtils.parseRedisServerList(TestConfigUtils.getRedisServers(), TestConfigUtils.getTimeoutMillis()); GenericObjectPoolConfig poolConfig = new JedisPoolConfig(); // ?//from w ww . j a va 2 s . c o m poolConfig.setMaxTotal(TestConfigUtils.getMaxTotalNum()); poolConfig.setMaxIdle(TestConfigUtils.getMaxIdleNum()); // poolConfig.setMinIdle(TestConfigUtils.getMinIdleNum()); poolConfig.setMinIdle(3); // local test // ?"" boolean lifo = TestConfigUtils.getPoolBehaviour() == PoolBehaviour.LIFO ? true : false; poolConfig.setLifo(lifo); // ? poolConfig.setBlockWhenExhausted(false); // // poolConfig.setBlockWhenExhausted(true); // poolConfig.setMaxWaitMillis(TimeUnit.MILLISECONDS.toMillis(10L)); // ""? poolConfig.setTestOnBorrow(false); poolConfig.setTestOnReturn(false); /* * "Evictor?"??"" */ poolConfig.setTestWhileIdle(true); // ?5????? // poolConfig.setTimeBetweenEvictionRunsMillis(TimeUnit.SECONDS.toMillis(TestConfigUtils.getTimeBetweenEvictionRunsSeconds())); poolConfig.setTimeBetweenEvictionRunsMillis(TimeUnit.SECONDS.toMillis(2L)); // local test // ??Evictor // poolConfig.setTimeBetweenEvictionRunsMillis(-1L); // local test // ? // poolConfig.setNumTestsPerEvictionRun(TestConfigUtils.getNumTestsPerEvictionRun()); poolConfig.setNumTestsPerEvictionRun(3); // local test // ? poolConfig.setSoftMinEvictableIdleTimeMillis( TimeUnit.MINUTES.toMillis(TestConfigUtils.getMinEvictableIdleTimeMinutes())); // ??(?) // ? poolConfig.setMinEvictableIdleTimeMillis( TimeUnit.MINUTES.toMillis(TestConfigUtils.getMaxEvictableIdleTimeMinutes())); this.shardedJedisPool = new CustomShardedJedisPool(poolConfig, shards, (int) TimeUnit.SECONDS.toMillis(1), 2); }