Search in sources :

Example 16 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project ksan by infinistor.

the class MariaDB method initConnectionPool.

private void initConnectionPool(String dbUrl, String dbPort, String dbName, String userName, String passwd) throws Exception {
    try {
        Class.forName("org.mariadb.jdbc.Driver");
        String jdbcUrl = "jdbc:mariadb://" + dbUrl + ":" + dbPort + "/" + dbName + "?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf8";
        logger.debug(jdbcUrl);
        ConnectionFactory connFactory = new DriverManagerConnectionFactory(jdbcUrl, userName, passwd);
        PoolableConnectionFactory poolableConnFactory = new PoolableConnectionFactory(connFactory, null);
        poolableConnFactory.setValidationQuery("select 1");
        GenericObjectPoolConfig<PoolableConnection> poolConfig = new GenericObjectPoolConfig<PoolableConnection>();
        Duration timeBetweenEvictionRuns = Duration.ofSeconds(60);
        poolConfig.setTimeBetweenEvictionRuns(timeBetweenEvictionRuns);
        poolConfig.setTestWhileIdle(true);
        poolConfig.setMinIdle(4);
        poolConfig.setMaxTotal(16);
        poolConfig.setTestOnBorrow(true);
        poolConfig.setTestWhileIdle(true);
        GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnFactory, poolConfig);
        poolableConnFactory.setPool(connectionPool);
        Class.forName("org.apache.commons.dbcp2.PoolingDriver");
        PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
        driver.registerPool("cpMetering", connectionPool);
        jrpool = new JedisPool(dbUrl, 6379);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("fail to load JDBC Driver", e);
    } catch (SQLException e) {
        throw new RuntimeException("fail to load JDBC Driver", e);
    }
    initDBTable();
    loadbuckets();
}
Also used : DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) SQLException(java.sql.SQLException) Duration(java.time.Duration) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) PoolingDriver(org.apache.commons.dbcp2.PoolingDriver) JedisPool(redis.clients.jedis.JedisPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 17 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project springframework-source-5.1.x by wb02125055.

the class CommonsPool2TargetSource method createObjectPool.

/**
 * Subclasses can override this if they want to return a specific Commons pool.
 * They should apply any configuration properties to the pool here.
 * <p>Default is a GenericObjectPool instance with the given pool size.
 * @return an empty Commons {@code ObjectPool}.
 * @see GenericObjectPool
 * @see #setMaxSize
 */
protected ObjectPool createObjectPool() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(getMaxSize());
    config.setMaxIdle(getMaxIdle());
    config.setMinIdle(getMinIdle());
    config.setMaxWaitMillis(getMaxWait());
    config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
    config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
    config.setBlockWhenExhausted(isBlockWhenExhausted());
    return new GenericObjectPool(this, config);
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Example 18 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project anomaly-detection by opensearch-project.

the class CheckpointDaoTests method setup.

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    indexName = "testIndexName";
    // gson = PowerMockito.mock(Gson.class);
    gson = new GsonBuilder().serializeSpecialFloatingPointValues().create();
    thresholdingModelClass = HybridThresholdingModel.class;
    when(clock.instant()).thenReturn(Instant.now());
    mapper = new RandomCutForestMapper();
    mapper.setSaveExecutorContextEnabled(true);
    trcfMapper = new ThresholdedRandomCutForestMapper();
    trcfSchema = AccessController.doPrivileged((PrivilegedAction<Schema<ThresholdedRandomCutForestState>>) () -> RuntimeSchema.getSchema(ThresholdedRandomCutForestState.class));
    converter = new V1JsonToV2StateConverter();
    serializeRCFBufferPool = spy(AccessController.doPrivileged(new PrivilegedAction<GenericObjectPool<LinkedBuffer>>() {

        @Override
        public GenericObjectPool<LinkedBuffer> run() {
            return new GenericObjectPool<>(new BasePooledObjectFactory<LinkedBuffer>() {

                @Override
                public LinkedBuffer create() throws Exception {
                    return LinkedBuffer.allocate(AnomalyDetectorSettings.SERIALIZATION_BUFFER_BYTES);
                }

                @Override
                public PooledObject<LinkedBuffer> wrap(LinkedBuffer obj) {
                    return new DefaultPooledObject<>(obj);
                }
            });
        }
    }));
    serializeRCFBufferPool.setMaxTotal(AnomalyDetectorSettings.MAX_TOTAL_RCF_SERIALIZATION_BUFFERS);
    serializeRCFBufferPool.setMaxIdle(AnomalyDetectorSettings.MAX_TOTAL_RCF_SERIALIZATION_BUFFERS);
    serializeRCFBufferPool.setMinIdle(0);
    serializeRCFBufferPool.setBlockWhenExhausted(false);
    serializeRCFBufferPool.setTimeBetweenEvictionRuns(AnomalyDetectorSettings.HOURLY_MAINTENANCE);
    anomalyRate = 0.005;
    checkpointDao = new CheckpointDao(client, clientUtil, indexName, gson, mapper, converter, trcfMapper, trcfSchema, thresholdingModelClass, indexUtil, maxCheckpointBytes, serializeRCFBufferPool, AnomalyDetectorSettings.SERIALIZATION_BUFFER_BYTES, anomalyRate);
    when(indexUtil.doesCheckpointIndexExist()).thenReturn(true);
    modelId = "testModelId";
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) GsonBuilder(com.google.gson.GsonBuilder) ThresholdedRandomCutForestMapper(com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestMapper) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) PrivilegedAction(java.security.PrivilegedAction) ThresholdedRandomCutForestMapper(com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestMapper) RandomCutForestMapper(com.amazon.randomcutforest.state.RandomCutForestMapper) BasePooledObjectFactory(org.apache.commons.pool2.BasePooledObjectFactory) V1JsonToV2StateConverter(com.amazon.randomcutforest.serialize.json.v1.V1JsonToV2StateConverter) ThresholdedRandomCutForestState(com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestState) Before(org.junit.Before)

Example 19 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project proctor by indeedeng.

the class SvnObjectPools method createObjectPool.

private static <T> ObjectPool<T> createObjectPool(final PooledObjectFactory<T> factory) {
    final GenericObjectPoolConfig objectPoolConfig = new GenericObjectPoolConfig();
    // arbitrary, but positive so objects do get evicted
    objectPoolConfig.setMinEvictableIdleTimeMillis(TimeUnit.HOURS.toMillis(1));
    // arbitrary, but positive so objects do get evicted
    objectPoolConfig.setTimeBetweenEvictionRunsMillis(TimeUnit.MINUTES.toMillis(10));
    objectPoolConfig.setJmxEnabled(false);
    objectPoolConfig.setBlockWhenExhausted(false);
    // uncapped number of objects in the pool
    objectPoolConfig.setMaxTotal(-1);
    final AbandonedConfig abandonedConfig = new AbandonedConfig();
    abandonedConfig.setRemoveAbandonedOnBorrow(true);
    abandonedConfig.setRemoveAbandonedTimeout((int) TimeUnit.MINUTES.toSeconds(30));
    return new GenericObjectPool<T>(factory, objectPoolConfig, abandonedConfig);
}
Also used : AbandonedConfig(org.apache.commons.pool2.impl.AbandonedConfig) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Example 20 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project bboss by bbossgroups.

the class BasicDataSource method createConnectionPool.

/**
 * Creates a connection pool for this datasource.  This method only exists
 * so subclasses can replace the implementation class.
 *
 * This implementation configures all pool properties other than
 * timeBetweenEvictionRunsMillis.  Setting that property is deferred to
 * {@link #startPoolMaintenance()}, since setting timeBetweenEvictionRunsMillis
 * to a positive value causes {@link GenericObjectPool}'s eviction timer
 * to be started.
 */
protected void createConnectionPool(PoolableConnectionFactory factory) {
    // Create an object pool to contain our active connections
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    updateJmxName(config);
    // Disable JMX on the underlying pool if the DS is not registered.
    config.setJmxEnabled(registeredJmxName != null);
    GenericObjectPool<PoolableConnection> gop;
    if (abandonedConfig != null && (abandonedConfig.getRemoveAbandonedOnBorrow() || abandonedConfig.getRemoveAbandonedOnMaintenance() || abandonedConfig.getLogAbandoned())) {
        // 改造实现跟踪功能 biaoping.yin
        gop = new GenericObjectPool<PoolableConnection>(factory, config, abandonedConfig);
    } else {
        gop = new GenericObjectPool<PoolableConnection>(factory, config);
    }
    gop.setMaxTotal(maxTotal);
    gop.setMaxIdle(maxIdle);
    gop.setMinIdle(minIdle);
    gop.setMaxWaitMillis(maxWaitMillis);
    gop.setTestOnCreate(testOnCreate);
    gop.setTestOnBorrow(testOnBorrow);
    gop.setTestOnReturn(testOnReturn);
    gop.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
    gop.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    gop.setTestWhileIdle(testWhileIdle);
    gop.setLifo(lifo);
    gop.setSwallowedExceptionListener(new SwallowedExceptionLogger(log, logExpiredConnections));
    gop.setEvictionPolicyClassName(evictionPolicyClassName);
    factory.setPool(gop);
    connectionPool = gop;
}
Also used : GenericObjectPoolConfig(com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig)

Aggregations

GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)79 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)32 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)27 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)23 ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)19 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)16 Test (org.junit.jupiter.api.Test)13 Properties (java.util.Properties)11 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)9 SQLException (java.sql.SQLException)8 PoolingDriver (org.apache.commons.dbcp2.PoolingDriver)8 Connection (java.sql.Connection)7 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)5 Bean (org.springframework.context.annotation.Bean)5 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)4 IOException (java.io.IOException)3 PooledObject (org.apache.commons.pool2.PooledObject)3 Test (org.junit.Test)3 TimeInterval (com.adaptris.util.TimeInterval)2 ThresholdedRandomCutForestMapper (com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestMapper)2