Java Code Examples for org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig#setTimeBetweenEvictionRunsMillis()

The following examples show how to use org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig#setTimeBetweenEvictionRunsMillis() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryService.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
private GenericKeyedObjectPool<PreparedContextKey, PreparedContext> createPreparedContextPool() {
    PreparedContextFactory factory = new PreparedContextFactory();
    KylinConfig kylinConfig = getConfig();
    GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
    config.setMaxTotalPerKey(kylinConfig.getQueryMaxCacheStatementInstancePerKey());
    config.setMaxTotal(kylinConfig.getQueryMaxCacheStatementNum());
    config.setBlockWhenExhausted(false);
    config.setMinEvictableIdleTimeMillis(10 * 60 * 1000L); // cached statement will be evict if idle for 10 minutes
    config.setTimeBetweenEvictionRunsMillis(60 * 1000L); 
    GenericKeyedObjectPool<PreparedContextKey, PreparedContext> pool = new GenericKeyedObjectPool<>(factory,
            config);
    return pool;
}
 
Example 2
Source File: QueryService.java    From kylin with Apache License 2.0 5 votes vote down vote up
private GenericKeyedObjectPool<PreparedContextKey, PreparedContext> createPreparedContextPool() {
    PreparedContextFactory factory = new PreparedContextFactory();
    KylinConfig kylinConfig = getConfig();
    GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
    config.setMaxTotalPerKey(kylinConfig.getQueryMaxCacheStatementInstancePerKey());
    config.setMaxTotal(kylinConfig.getQueryMaxCacheStatementNum());
    config.setBlockWhenExhausted(false);
    config.setMinEvictableIdleTimeMillis(10 * 60 * 1000L); // cached statement will be evict if idle for 10 minutes
    config.setTimeBetweenEvictionRunsMillis(60 * 1000L); 
    GenericKeyedObjectPool<PreparedContextKey, PreparedContext> pool = new GenericKeyedObjectPool<>(factory,
            config);
    return pool;
}
 
Example 3
Source File: SharedPoolDataSource.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
private void registerPool(final String userName, final String password) throws NamingException, SQLException {

        final ConnectionPoolDataSource cpds = testCPDS(userName, password);

        // Create an object pool to contain our PooledConnections
        factory = new KeyedCPDSConnectionFactory(cpds, getValidationQuery(), getValidationQueryTimeout(),
                isRollbackAfterValidation());
        factory.setMaxConnLifetimeMillis(getMaxConnLifetimeMillis());

        final GenericKeyedObjectPoolConfig<PooledConnectionAndInfo> config = new GenericKeyedObjectPoolConfig<>();
        config.setBlockWhenExhausted(getDefaultBlockWhenExhausted());
        config.setEvictionPolicyClassName(getDefaultEvictionPolicyClassName());
        config.setLifo(getDefaultLifo());
        config.setMaxIdlePerKey(getDefaultMaxIdle());
        config.setMaxTotal(getMaxTotal());
        config.setMaxTotalPerKey(getDefaultMaxTotal());
        config.setMaxWaitMillis(getDefaultMaxWaitMillis());
        config.setMinEvictableIdleTimeMillis(getDefaultMinEvictableIdleTimeMillis());
        config.setMinIdlePerKey(getDefaultMinIdle());
        config.setNumTestsPerEvictionRun(getDefaultNumTestsPerEvictionRun());
        config.setSoftMinEvictableIdleTimeMillis(getDefaultSoftMinEvictableIdleTimeMillis());
        config.setTestOnCreate(getDefaultTestOnCreate());
        config.setTestOnBorrow(getDefaultTestOnBorrow());
        config.setTestOnReturn(getDefaultTestOnReturn());
        config.setTestWhileIdle(getDefaultTestWhileIdle());
        config.setTimeBetweenEvictionRunsMillis(getDefaultTimeBetweenEvictionRunsMillis());

        final KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> tmpPool = new GenericKeyedObjectPool<>(factory,
                config);
        factory.setPool(tmpPool);
        pool = tmpPool;
    }
 
Example 4
Source File: PooledContextSource.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
private GenericKeyedObjectPoolConfig getConfig(PoolConfig poolConfig) {
	GenericKeyedObjectPoolConfig objectPoolConfig = new GenericKeyedObjectPoolConfig();

	objectPoolConfig.setMaxTotalPerKey(poolConfig.getMaxTotalPerKey());
	objectPoolConfig.setMaxTotal(poolConfig.getMaxTotal());

	objectPoolConfig.setMaxIdlePerKey(poolConfig.getMaxIdlePerKey());
	objectPoolConfig.setMinIdlePerKey(poolConfig.getMinIdlePerKey());

	objectPoolConfig.setTestWhileIdle(poolConfig.isTestWhileIdle());
	objectPoolConfig.setTestOnReturn(poolConfig.isTestOnReturn());
	objectPoolConfig.setTestOnCreate(poolConfig.isTestOnCreate());
	objectPoolConfig.setTestOnBorrow(poolConfig.isTestOnBorrow());

	objectPoolConfig.setTimeBetweenEvictionRunsMillis(poolConfig.getTimeBetweenEvictionRunsMillis());
	objectPoolConfig.setEvictionPolicyClassName(poolConfig.getEvictionPolicyClassName());
	objectPoolConfig.setMinEvictableIdleTimeMillis(poolConfig.getMinEvictableIdleTimeMillis());
	objectPoolConfig.setNumTestsPerEvictionRun(poolConfig.getNumTestsPerEvictionRun());
	objectPoolConfig.setSoftMinEvictableIdleTimeMillis(poolConfig.getSoftMinEvictableIdleTimeMillis());

	objectPoolConfig.setJmxEnabled(poolConfig.isJmxEnabled());
	objectPoolConfig.setJmxNameBase(poolConfig.getJmxNameBase());
	objectPoolConfig.setJmxNamePrefix(poolConfig.getJmxNamePrefix());

	objectPoolConfig.setMaxWaitMillis(poolConfig.getMaxWaitMillis());

	objectPoolConfig.setFairness(poolConfig.isFairness());
	objectPoolConfig.setBlockWhenExhausted(poolConfig.isBlockWhenExhausted());
	objectPoolConfig.setLifo(poolConfig.isLifo());

	return objectPoolConfig;
}
 
Example 5
Source File: CasStorageServiceImpl.java    From webanno with Apache License 2.0 4 votes vote down vote up
/**
 * @param aCasDoctor
 *            (optional) if present, CAS validation can take place
 * @param aSchemaService
 *            (optional) if present, CAS upgrades can be performed
 */
public CasStorageServiceImpl(
        @Autowired(required = false) CasDoctor aCasDoctor,
        @Autowired(required = false) AnnotationSchemaService aSchemaService,
        @Autowired RepositoryProperties aRepositoryProperties,
        @Autowired BackupProperties aBackupProperties)
{
    casDoctor = aCasDoctor;
    schemaService = aSchemaService;
    repositoryProperties = aRepositoryProperties;
    backupProperties = aBackupProperties;
    
    GenericKeyedObjectPoolConfig<CasHolder> config = new GenericKeyedObjectPoolConfig<>();
    // Since we want the pool to control exclusive access to a particular CAS, we only ever 
    // must have one instance per key (the key uniquely identifies the CAS)
    config.setMaxTotalPerKey(1);
    // Setting this to 0 because we do not want any CAS to stick around in memory indefinitely
    config.setMinIdlePerKey(0);
    // Run an evictor thread every 5 minutes
    config.setTimeBetweenEvictionRunsMillis(MINUTES.toMillis(5));
    // Allow the evictor to drop idle CASes from the pool after 5 minutes (i.e. on each run)
    config.setMinEvictableIdleTimeMillis(MINUTES.toMillis(EVICT_IDLE_CASES_AFTER_MINUTES));
    // Allow the evictor to drop all idle CASes on every eviction run
    config.setNumTestsPerEvictionRun(-1);
    // Allow viewing the pool in JMX
    config.setJmxEnabled(true);
    config.setJmxNameBase(getClass().getPackage().getName() + ":type="
            + getClass().getSimpleName() + ",name=");
    config.setJmxNamePrefix("exclusiveCasAccessPool");
    // Check if the CAS is still valid or needs to be replaced when it is borrowed and when it
    // is returned
    config.setTestOnReturn(true);
    config.setTestOnBorrow(true);
    // We do not have to set maxTotal because the default is already to have no limit (-1)
    exclusiveAccessPool = new GenericKeyedObjectPool<>(new PooledCasHolderFactory(), config);
    
    sharedAccessCache = Caffeine.newBuilder()
            .expireAfterAccess(EVICT_IDLE_CASES_AFTER_MINUTES, MINUTES)
            .maximumSize(SHARED_CAS_CACHE_SIZE)
            .recordStats()
            .build();

    if (casDoctor == null) {
        log.info("CAS doctor not available - unable to check/repair CASes");
    }

    if (backupProperties.getInterval() > 0) {
        log.info("CAS backups enabled - interval: {}sec  max-backups: {}  max-age: {}sec",
                backupProperties.getInterval(), backupProperties.getKeep().getNumber(),
                backupProperties.getKeep().getTime());
    }
    else {
        log.info("CAS backups disabled");
    }
}
 
Example 6
Source File: ObjectPoolIssue326.java    From commons-pool with Apache License 2.0 4 votes vote down vote up
private void run() throws Exception {
    final GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
    poolConfig.setMaxTotal(10);
    poolConfig.setMaxTotalPerKey(5);
    poolConfig.setMinIdlePerKey(-1);
    poolConfig.setMaxIdlePerKey(-1);
    poolConfig.setLifo(true);
    poolConfig.setFairness(true);
    poolConfig.setMaxWaitMillis(30 * 1000);
    poolConfig.setMinEvictableIdleTimeMillis(-1);
    poolConfig.setSoftMinEvictableIdleTimeMillis(-1);
    poolConfig.setNumTestsPerEvictionRun(1);
    poolConfig.setTestOnCreate(false);
    poolConfig.setTestOnBorrow(false);
    poolConfig.setTestOnReturn(false);
    poolConfig.setTestWhileIdle(false);
    poolConfig.setTimeBetweenEvictionRunsMillis(5 * 1000);
    poolConfig.setEvictionPolicyClassName(BaseObjectPoolConfig.DEFAULT_EVICTION_POLICY_CLASS_NAME);
    poolConfig.setBlockWhenExhausted(false);
    poolConfig.setJmxEnabled(false);
    poolConfig.setJmxNameBase(null);
    poolConfig.setJmxNamePrefix(null);

    final GenericKeyedObjectPool<Integer, Object> pool = new GenericKeyedObjectPool<>(new ObjectFactory(), poolConfig);

    // number of threads to reproduce is finicky. this count seems to be best for my
    // 4 core box.
    // too many doesn't reproduce it ever, too few doesn't either.
    final ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
    final long startTime = System.currentTimeMillis();
    long testIter = 0;
    try {
        while (true) {
            testIter++;
            if (testIter % 1000 == 0) {
                System.out.println(testIter);
            }
            final List<Task> tasks = createTasks(pool);
            final List<Future<Object>> futures = service.invokeAll(tasks);
            for (final Future<Object> future : futures) {
                future.get();
            }
        }
    } finally {
        System.out.println("Time: " + (System.currentTimeMillis() - startTime) / 1000.0);
        service.shutdown();
    }
}
 
Example 7
Source File: DriverAdapterCPDS.java    From commons-dbcp with Apache License 2.0 4 votes vote down vote up
/**
 * Attempts to establish a database connection.
 *
 * @param pooledUserName
 *            name to be used for the connection
 * @param pooledUserPassword
 *            password to be used fur the connection
 */
@Override
public PooledConnection getPooledConnection(final String pooledUserName, final String pooledUserPassword)
        throws SQLException {
    getConnectionCalled = true;
    PooledConnectionImpl pooledConnection = null;
    // Workaround for buggy WebLogic 5.1 classloader - ignore the exception upon first invocation.
    try {
        if (connectionProperties != null) {
            update(connectionProperties, KEY_USER, pooledUserName);
            update(connectionProperties, KEY_PASSWORD, pooledUserPassword);
            pooledConnection = new PooledConnectionImpl(
                    DriverManager.getConnection(getUrl(), connectionProperties));
        } else {
            pooledConnection = new PooledConnectionImpl(
                    DriverManager.getConnection(getUrl(), pooledUserName, pooledUserPassword));
        }
        pooledConnection.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    } catch (final ClassCircularityError e) {
        if (connectionProperties != null) {
            pooledConnection = new PooledConnectionImpl(
                    DriverManager.getConnection(getUrl(), connectionProperties));
        } else {
            pooledConnection = new PooledConnectionImpl(
                    DriverManager.getConnection(getUrl(), pooledUserName, pooledUserPassword));
        }
        pooledConnection.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    }
    KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> stmtPool = null;
    if (isPoolPreparedStatements()) {
        final GenericKeyedObjectPoolConfig<DelegatingPreparedStatement> config = new GenericKeyedObjectPoolConfig<>();
        config.setMaxTotalPerKey(Integer.MAX_VALUE);
        config.setBlockWhenExhausted(false);
        config.setMaxWaitMillis(0);
        config.setMaxIdlePerKey(getMaxIdle());
        if (getMaxPreparedStatements() <= 0) {
            // since there is no limit, create a prepared statement pool with an eviction thread;
            // evictor settings are the same as the connection pool settings.
            config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
            config.setNumTestsPerEvictionRun(getNumTestsPerEvictionRun());
            config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
        } else {
            // since there is a limit, create a prepared statement pool without an eviction thread;
            // pool has LRU functionality so when the limit is reached, 15% of the pool is cleared.
            // see org.apache.commons.pool2.impl.GenericKeyedObjectPool.clearOldest method
            config.setMaxTotal(getMaxPreparedStatements());
            config.setTimeBetweenEvictionRunsMillis(-1);
            config.setNumTestsPerEvictionRun(0);
            config.setMinEvictableIdleTimeMillis(0);
        }
        stmtPool = new GenericKeyedObjectPool<>(pooledConnection, config);
        pooledConnection.setStatementPool(stmtPool);
    }
    return pooledConnection;
}