Search in sources :

Example 81 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig in project ofbiz-framework by apache.

the class DBCPConnectionFactory method getConnection.

public Connection getConnection(GenericHelperInfo helperInfo, JdbcElement abstractJdbc) throws SQLException, GenericEntityException {
    String cacheKey = helperInfo.getHelperFullName();
    DebugManagedDataSource mds = dsCache.get(cacheKey);
    if (mds != null) {
        return TransactionUtil.getCursorConnection(helperInfo, mds.getConnection());
    }
    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);
    factory.setRollbackOnReturn(false);
    factory.setEnableAutoCommitOnReturn(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());
    // disabled in favour of setSoftMinEvictableIdleTimeMillis(...)
    poolConfig.setMinEvictableIdleTimeMillis(-1);
    poolConfig.setSoftMinEvictableIdleTimeMillis(jdbcElement.getSoftMinEvictableIdleTimeMillis());
    // test all the idle connections
    poolConfig.setNumTestsPerEvictionRun(maxSize);
    // settings for when the pool is exhausted
    // the thread requesting the connection waits if no connection is available
    poolConfig.setBlockWhenExhausted(true);
    // throw an exception if, after getPoolSleeptime() ms, no connection is available for the requesting thread
    poolConfig.setMaxWaitMillis(jdbcElement.getPoolSleeptime());
    // 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<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(factory, poolConfig);
    factory.setPool(pool);
    mds = new DebugManagedDataSource(pool, xacf.getTransactionRegistry());
    mds.setAccessToUnderlyingConnectionAllowed(true);
    // cache the pool
    dsCache.putIfAbsent(cacheKey, mds);
    mds = dsCache.get(cacheKey);
    return TransactionUtil.getCursorConnection(helperInfo, mds.getConnection());
}
Also used : GenericEntityConfException(org.apache.ofbiz.entity.GenericEntityConfException) Driver(java.sql.Driver) Properties(java.util.Properties) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableManagedConnectionFactory(org.apache.commons.dbcp2.managed.PoolableManagedConnectionFactory) GenericEntityConfException(org.apache.ofbiz.entity.GenericEntityConfException) SQLException(java.sql.SQLException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) TransactionManager(javax.transaction.TransactionManager) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) InlineJdbc(org.apache.ofbiz.entity.config.model.InlineJdbc) DriverConnectionFactory(org.apache.commons.dbcp2.DriverConnectionFactory) XAConnectionFactory(org.apache.commons.dbcp2.managed.XAConnectionFactory) LocalXAConnectionFactory(org.apache.commons.dbcp2.managed.LocalXAConnectionFactory) LocalXAConnectionFactory(org.apache.commons.dbcp2.managed.LocalXAConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Example 82 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig in project kork by spinnaker.

the class JedisPoolFactory method build.

public Pool<Jedis> build(String name, JedisDriverProperties properties) {
    if (properties.connection == null || "".equals(properties.connection)) {
        throw new MissingRequiredConfiguration("Jedis client must have a connection defined");
    }
    URI redisConnection = URI.create(properties.connection);
    String host = redisConnection.getHost();
    int port = redisConnection.getPort() == -1 ? Protocol.DEFAULT_PORT : redisConnection.getPort();
    int database = parseDatabase(redisConnection.getPath());
    String password = parsePassword(redisConnection.getUserInfo());
    GenericObjectPoolConfig objectPoolConfig = properties.poolConfig;
    return new JedisPool(objectPoolConfig, host, port, properties.timeoutMs, password, database, name);
}
Also used : MissingRequiredConfiguration(com.netflix.spinnaker.kork.jedis.exception.MissingRequiredConfiguration) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) URI(java.net.URI)

Example 83 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig in project cuba by cuba-platform.

the class KryoSerialization method initPool.

protected void initPool() {
    // assume that application context is already initialized
    Configuration configuration = AppBeans.get(Configuration.NAME);
    config = configuration.getConfig(KryoSerializationConfig.class);
    int poolSize = config.getMaxPoolSize();
    GenericObjectPoolConfig<Kryo> poolConfig = new GenericObjectPoolConfig<>();
    poolConfig.setMaxIdle(poolSize);
    poolConfig.setMaxTotal(poolSize);
    poolConfig.setMaxWaitMillis(config.getMaxBorrowWaitMillis());
    String jmxName = "kryo-" + AppContext.getProperty("cuba.webContextName");
    poolConfig.setJmxNamePrefix(jmxName);
    PooledObjectFactory<Kryo> factory = new KryoObjectFactory(this);
    pool = new GenericObjectPool<>(factory, poolConfig);
    log.debug("Kryo context pool created");
}
Also used : Configuration(com.haulmont.cuba.core.global.Configuration) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) Kryo(com.esotericsoftware.kryo.Kryo)

Example 84 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig in project Anserini by castorini.

the class SolrEndToEndTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    final File solrHome = createTempDir().toFile();
    final File configSetBaseDir = new File(solrHome.toPath() + File.separator + "configsets");
    FileUtils.copyDirectory(getFile("solr/anserini"), new File(configSetBaseDir + File.separator + "anserini"));
    SolrResourceLoader loader = new SolrResourceLoader(solrHome.toPath());
    NodeConfig config = new NodeConfig.NodeConfigBuilder("embeddedSolrServerNode", loader.getInstancePath()).setConfigSetBaseDirectory(configSetBaseDir.getAbsolutePath()).build();
    client = new EmbeddedSolrServer(config, getCollectionName());
    LOG.info("Created Embedded Solr Server");
    CoreAdminRequest.Create createRequest = new CoreAdminRequest.Create();
    createRequest.setCoreName(getCollectionName());
    createRequest.setConfigSet("anserini");
    createRequest.process(client);
    client.commit();
    LOG.info("Created Solr Core: " + getCollectionName());
    GenericObjectPoolConfig<SolrClient> poolConfig = new GenericObjectPoolConfig<>();
    // only 1 EmbeddedSolrServer instance will be created by getSolrClient
    poolConfig.setMaxTotal(1);
    poolConfig.setMinIdle(1);
    stubSolrPool = new GenericObjectPool<>(new StubSolrClientFactory(client), poolConfig);
}
Also used : CoreAdminRequest(org.apache.solr.client.solrj.request.CoreAdminRequest) SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) SolrClient(org.apache.solr.client.solrj.SolrClient) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) File(java.io.File) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) NodeConfig(org.apache.solr.core.NodeConfig) Before(org.junit.Before)

Example 85 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig in project spring-cloud-connectors by spring-cloud.

the class RedisConnectionFactoryCloudConfigTestHelper method assertPoolProperties.

public static void assertPoolProperties(RedisConnectionFactory connector, int maxActive, int minIdle, long maxWait) {
    GenericObjectPoolConfig poolConfig = null;
    if (connector instanceof JedisConnectionFactory) {
        poolConfig = ((JedisConnectionFactory) connector).getPoolConfig();
    } else if (connector instanceof LettuceConnectionFactory) {
        LettuceClientConfiguration config = ((LettuceConnectionFactory) connector).getClientConfiguration();
        assertThat(config, instanceOf(LettucePoolingClientConfiguration.class));
        poolConfig = ((LettucePoolingClientConfiguration) config).getPoolConfig();
    }
    assertCommonsPoolProperties(poolConfig, maxActive, minIdle, maxWait);
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisConnectionFactory(org.springframework.data.redis.connection.jedis.JedisConnectionFactory) LettuceClientConfiguration(org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration) LettuceConnectionFactory(org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory) LettucePoolingClientConfiguration(org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration)

Aggregations

GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)127 Test (org.junit.Test)77 Jedis (redis.clients.jedis.Jedis)41 ShardedJedis (redis.clients.jedis.ShardedJedis)40 ShardedJedisPool (redis.clients.jedis.ShardedJedisPool)40 JedisPool (redis.clients.jedis.JedisPool)31 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)19 ArrayList (java.util.ArrayList)15 JedisShardInfo (redis.clients.jedis.JedisShardInfo)15 URI (java.net.URI)11 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)9 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 BaseTest (com.sohu.tv.test.base.BaseTest)6 IOException (java.io.IOException)6 Transaction (redis.clients.jedis.Transaction)6 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)5 PooledObject (org.apache.commons.pool2.PooledObject)5 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)5 Test (org.testng.annotations.Test)5