Search in sources :

Example 6 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project demo by changlongmao.

the class RedisTemplateConfig method genericObjectPoolConfig.

/**
 * GenericObjectPoolConfig 连接池配置
 */
@Bean
public GenericObjectPoolConfig genericObjectPoolConfig() {
    GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
    genericObjectPoolConfig.setMaxIdle(maxIdle);
    genericObjectPoolConfig.setMinIdle(minIdle);
    genericObjectPoolConfig.setMaxTotal(maxActive);
    genericObjectPoolConfig.setMaxWaitMillis(maxWait);
    return genericObjectPoolConfig;
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) Bean(org.springframework.context.annotation.Bean)

Example 7 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project spring-parent by mingyang66.

the class PoolMain method main.

public static void main(String[] args) throws Exception {
    ObjectFactory orderFactory = new ObjectFactory();
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(5000);
    // 设置获取连接超时时间
    config.setMaxWaitMillis(1000);
    GenericObjectPool<User> connectionPool = new GenericObjectPool<User>(orderFactory, config);
    long start = System.currentTimeMillis();
    for (int i = 0; i < 100000; i++) {
        User o = connectionPool.borrowObject();
        // User o = new User();
        // System.out.println("brrow a connection: " + o +" active connection:"+connectionPool.getNumActive());
        connectionPool.returnObject(o);
    }
    System.out.println(System.currentTimeMillis() - start);
}
Also used : User(com.emily.cloud.test.api.po.User) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Example 8 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project spring-framework-debug by Joker-5.

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 9 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

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 10 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig 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)

Aggregations

GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)222 Test (org.junit.Test)82 Jedis (redis.clients.jedis.Jedis)44 ShardedJedisPool (redis.clients.jedis.ShardedJedisPool)43 ShardedJedis (redis.clients.jedis.ShardedJedis)41 JedisPool (redis.clients.jedis.JedisPool)37 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)32 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)21 Bean (org.springframework.context.annotation.Bean)20 ArrayList (java.util.ArrayList)17 JedisShardInfo (redis.clients.jedis.JedisShardInfo)17 Test (org.junit.jupiter.api.Test)15 URI (java.net.URI)12 LettuceConnectionFactory (org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory)12 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)12 RedisStandaloneConfiguration (org.springframework.data.redis.connection.RedisStandaloneConfiguration)11 IOException (java.io.IOException)10 Test (org.testng.annotations.Test)10 PooledObject (org.apache.commons.pool2.PooledObject)9 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)9