Search in sources :

Example 56 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project ofbiz-framework by apache.

the class DBCPConnectionFactory method getConnection.

@SuppressWarnings("deprecation")
@Override
public Connection getConnection(GenericHelperInfo helperInfo, JdbcElement abstractJdbc) throws SQLException, GenericEntityException {
    String cacheKey = helperInfo.getHelperFullName();
    DebugManagedDataSource<? extends Connection> mds = DS_CACHE.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()).getDeclaredConstructor().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.setAutoCommitOnReturn(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<PoolableConnection> 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,
    poolConfig.setMaxWaitMillis(jdbcElement.getPoolSleeptime());
    // 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<PoolableConnection> pool = new GenericObjectPool<>(factory, poolConfig);
    factory.setPool(pool);
    mds = new DebugManagedDataSource<>(pool, xacf.getTransactionRegistry());
    mds.setAccessToUnderlyingConnectionAllowed(true);
    // cache the pool
    DS_CACHE.putIfAbsent(cacheKey, mds);
    mds = DS_CACHE.get(cacheKey);
    return TransactionUtil.getCursorConnection(helperInfo, mds.getConnection());
}
Also used : GenericEntityConfException(org.apache.ofbiz.entity.GenericEntityConfException) Driver(java.sql.Driver) Properties(java.util.Properties) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) DriverConnectionFactory(org.apache.commons.dbcp2.DriverConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) 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) TransactionManager(javax.transaction.TransactionManager) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) InlineJdbc(org.apache.ofbiz.entity.config.model.InlineJdbc) XAConnectionFactory(org.apache.commons.dbcp2.managed.XAConnectionFactory) LocalXAConnectionFactory(org.apache.commons.dbcp2.managed.LocalXAConnectionFactory) LocalXAConnectionFactory(org.apache.commons.dbcp2.managed.LocalXAConnectionFactory)

Example 57 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project chao-cloud by chaojunzi.

the class FtpConfig method fileOperation.

@Bean
public IFileOperation fileOperation(FtpConfig ftpConfig) {
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setBlockWhenExhausted(ftpConfig.blockWhenExhausted);
    poolConfig.setMaxWaitMillis(ftpConfig.maxWaitMillis);
    poolConfig.setMinIdle(ftpConfig.minIdle);
    poolConfig.setMaxIdle(ftpConfig.maxIdle);
    poolConfig.setMaxTotal(ftpConfig.maxTotal);
    poolConfig.setTestOnBorrow(ftpConfig.testOnBorrow);
    poolConfig.setTestOnReturn(ftpConfig.testOnReturn);
    poolConfig.setTestOnCreate(ftpConfig.testOnCreate);
    poolConfig.setTestWhileIdle(ftpConfig.testWhileIdle);
    poolConfig.setLifo(ftpConfig.lifo);
    // 注入对象
    FtpClientFactory factory = new FtpClientFactory(ftpConfig);
    FtpClientPool ftpClientPool = new FtpClientPool(new GenericObjectPool(factory, poolConfig));
    FtpClientProxy ftpClientProxy = new FtpClientProxy(ftpClientPool);
    FileOperationImpl operation = new FileOperationImpl();
    operation.setFtpConfig(ftpConfig);
    operation.setFtpClientProxy(ftpClientProxy);
    return operation;
}
Also used : FtpClientFactory(com.chao.cloud.common.extra.ftp.pool.FtpClientFactory) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) FtpClientProxy(com.chao.cloud.common.extra.ftp.pool.FtpClientProxy) FileOperationImpl(com.chao.cloud.common.extra.ftp.impl.FileOperationImpl) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) FtpClientPool(com.chao.cloud.common.extra.ftp.pool.FtpClientPool) InitializingBean(org.springframework.beans.factory.InitializingBean) Bean(org.springframework.context.annotation.Bean)

Example 58 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project businessautomation-cop by redhat-cop.

the class JPAAuditBean method auditDatasource.

@Bean
public DataSource auditDatasource() {
    DataSourceXAConnectionFactory dataSourceXAConnectionFactory = new DataSourceXAConnectionFactory(tm, h2DataSource());
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(dataSourceXAConnectionFactory, null);
    GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    return new ManagedDataSource<>(connectionPool, dataSourceXAConnectionFactory.getTransactionRegistry());
}
Also used : PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) DataSourceXAConnectionFactory(org.apache.commons.dbcp2.managed.DataSourceXAConnectionFactory) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) ManagedDataSource(org.apache.commons.dbcp2.managed.ManagedDataSource) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Example 59 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project today-framework by TAKETODAY.

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 60 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project spring-integration by spring-projects.

the class JdbcMessageStoreTests method testMessageGroupStreamNoConnectionPoolLeak.

@Test
@Transactional(propagation = Propagation.NEVER)
public void testMessageGroupStreamNoConnectionPoolLeak() throws NoSuchMethodException {
    DataSourceConnectionFactory connFactory = new DataSourceConnectionFactory(this.dataSource);
    PoolableConnectionFactory poolFactory = new PoolableConnectionFactory(connFactory, null);
    GenericObjectPoolConfig<PoolableConnection> config = new GenericObjectPoolConfig<>();
    config.setMaxTotal(2);
    config.setMaxWaitMillis(500);
    ObjectPool<PoolableConnection> connPool = new GenericObjectPool<>(poolFactory, config);
    poolFactory.setPool(connPool);
    PoolingDataSource<PoolableConnection> poolingDataSource = new PoolingDataSource<>(connPool);
    JdbcMessageStore pooledMessageStore = new JdbcMessageStore(poolingDataSource);
    CollectionArgumentResolver collectionArgumentResolver = new CollectionArgumentResolver(true);
    collectionArgumentResolver.setBeanFactory(new DefaultListableBeanFactory());
    Method methodForCollectionOfPayloads = getClass().getMethod("methodForCollectionOfPayloads", Collection.class);
    MethodParameter methodParameter = SynthesizingMethodParameter.forExecutable(methodForCollectionOfPayloads, 0);
    String groupId = "X";
    Message<String> message = MessageBuilder.withPayload("test data").build();
    pooledMessageStore.addMessagesToGroup(groupId, message);
    // it failed with "Cannot get a connection, pool error Timeout waiting for idle object"
    for (int i = 0; i < 3; i++) {
        Object result = collectionArgumentResolver.resolveArgument(methodParameter, new GenericMessage<>(pooledMessageStore.getMessageGroup(groupId).getMessages()));
        assertThat(result).isInstanceOf(Collection.class).asList().hasSize(1).contains("test data");
    }
    pooledMessageStore.removeMessageGroup(groupId);
}
Also used : PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Method(java.lang.reflect.Method) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) DataSourceConnectionFactory(org.apache.commons.dbcp2.DataSourceConnectionFactory) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) CollectionArgumentResolver(org.springframework.integration.handler.support.CollectionArgumentResolver) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) Collection(java.util.Collection) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) Test(org.junit.jupiter.api.Test) Transactional(org.springframework.transaction.annotation.Transactional)

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