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());
}
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;
}
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());
}
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);
}
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);
}
Aggregations