本文整理汇总了Java中org.apache.commons.dbcp2.PoolableConnectionFactory.setValidationQuery方法的典型用法代码示例。如果您正苦于以下问题:Java PoolableConnectionFactory.setValidationQuery方法的具体用法?Java PoolableConnectionFactory.setValidationQuery怎么用?Java PoolableConnectionFactory.setValidationQuery使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.dbcp2.PoolableConnectionFactory
的用法示例。
在下文中一共展示了PoolableConnectionFactory.setValidationQuery方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createPool
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入方法依赖的package包/类
private ObjectPool<PoolableConnection> createPool(String connectionString) {
// A ConnectionFactory that the pool will use to create Connections.
ConnectionFactory connectionFactory =
new DriverManagerConnectionFactory(connectionString, null);
// PoolableConnectionFactory wraps the real Connections with the
// classes that implement the pooling functionality.
PoolableConnectionFactory poolableConnectionFactory =
new PoolableConnectionFactory(connectionFactory, null);
poolableConnectionFactory.setValidationQuery("SELECT 1");
// Actual pool of connections.
GenericObjectPool<PoolableConnection> connectionPool =
new GenericObjectPool<>(poolableConnectionFactory);
int connMax = getConfig()
.getInt(OctopusConfiguration.MASTER_CONNECTION_POOL_MAX, 8);
connectionPool.setMaxTotal(connMax);
connectionPool.setTestOnBorrow(true);
// Set the factory's pool property to the owning pool.
poolableConnectionFactory.setPool(connectionPool);
return connectionPool;
}
示例2: setUp
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入方法依赖的package包/类
@Before
public void setUp() throws Exception {
org.hsqldb.jdbc.JDBCDriver.class.newInstance();
Properties connectionProps=new Properties();
connectionProps.put("user", "SA");
connectionProps.put("password", "");
connectionProps.put("create", "true");
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
"jdbc:hsqldb:mem:my-sample",connectionProps);
ObjectName poolName= poolName=new ObjectName("org.moneta", "connectionPool", "TestPool");
poolableConnectionFactory=
new PoolableConnectionFactory(connectionFactory,poolName);
poolableConnectionFactory.setDefaultCatalog("PUBLIC");
poolableConnectionFactory.setValidationQuery(VALIDATION_SQL);
connectionPool =
new GenericObjectPool<PoolableConnection>(poolableConnectionFactory);
poolableConnectionFactory.setPool(connectionPool);
connectionPool.setMaxTotal(2);
connectionPool.setMaxWaitMillis(400);
healthCheck = new DbcpConnectionPoolHealthCheck(connectionPool, "mine");
}
示例3: start
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入方法依赖的package包/类
@Override
public void start() {
try {
Class.forName(driver);
} catch (final ClassNotFoundException e) {
throw new RuntimeException("Unable to find JDBC driver " + driver, e);
}
final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(serverURL, username, password);
final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
poolableConnectionFactory.setValidationQuery(testSQL);
poolableConnectionFactory.setValidationQueryTimeout(testTimeout);
poolableConnectionFactory.setMaxConnLifetimeMillis((long) (connectionTimeout * JiveConstants.DAY));
final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setTestOnBorrow(testBeforeUse);
poolConfig.setTestOnReturn(testAfterUse);
poolConfig.setMinIdle(minConnections);
poolConfig.setMaxTotal(maxConnections);
poolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRuns);
poolConfig.setSoftMinEvictableIdleTimeMillis(minIdleTime);
poolConfig.setMaxWaitMillis(maxWaitTime);
connectionPool = new GenericObjectPool<>(poolableConnectionFactory, poolConfig);
poolableConnectionFactory.setPool(connectionPool);
dataSource = new PoolingDataSource<>(connectionPool);
}
示例4: setUp
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入方法依赖的package包/类
@Before
public void setUp() throws Exception {
// create a GeronimoTransactionManager for testing
transactionManager = new TransactionManagerImpl();
// create a driver connection factory
final Properties properties = new Properties();
properties.setProperty("user", "username");
properties.setProperty("password", "password");
final ConnectionFactory connectionFactory = new DriverConnectionFactory(new TesterDriver(), "jdbc:apache:commons:testdriver", properties);
// wrap it with a LocalXAConnectionFactory
final XAConnectionFactory xaConnectionFactory = new LocalXAConnectionFactory(transactionManager, connectionFactory);
// create the pool object factory
final PoolableConnectionFactory factory =
new PoolableConnectionFactory(xaConnectionFactory, null);
factory.setValidationQuery("SELECT DUMMY FROM DUAL");
factory.setDefaultReadOnly(Boolean.TRUE);
factory.setDefaultAutoCommit(Boolean.TRUE);
// create the pool
pool = new GenericObjectPool<>(factory);
factory.setPool(pool);
pool.setMaxTotal(getMaxTotal());
pool.setMaxWaitMillis(getMaxWaitMillis());
// finally create the datasource
ds = new ManagedDataSource<>(pool, xaConnectionFactory.getTransactionRegistry());
ds.setAccessToUnderlyingConnectionAllowed(true);
}
示例5: setUp
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入方法依赖的package包/类
@Before
public void setUp()
throws Exception {
// create a GeronimoTransactionManager for testing
transactionManager = new TransactionManagerImpl();
// create a driver connection factory
final Properties properties = new Properties();
properties.setProperty("user", "username");
properties.setProperty("password", "password");
final ConnectionFactory connectionFactory = new DriverConnectionFactory(new TesterDriver(), "jdbc:apache:commons:testdriver", properties);
// wrap it with a LocalXAConnectionFactory
final XAConnectionFactory xaConnectionFactory = new UncooperativeLocalXAConnectionFactory(transactionManager, connectionFactory);
// create the pool object factory
final PoolableConnectionFactory factory = new PoolableConnectionFactory(xaConnectionFactory, null);
factory.setValidationQuery("SELECT DUMMY FROM DUAL");
factory.setDefaultReadOnly(Boolean.TRUE);
factory.setDefaultAutoCommit(Boolean.TRUE);
// create the pool
pool = new GenericObjectPool<>(factory);
factory.setPool(pool);
pool.setMaxTotal(10);
pool.setMaxWaitMillis(100);
// finally create the datasource
ds = new ManagedDataSource<>(pool, xaConnectionFactory.getTransactionRegistry());
ds.setAccessToUnderlyingConnectionAllowed(true);
}
示例6: createDbcp
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入方法依赖的package包/类
private void createDbcp(DbcpConfig conf)
{
if (!dataSources.containsKey(conf.name))
{
try
{
Class.forName(conf.driverClassName);
DriverManagerConnectionFactory cf = new DriverManagerConnectionFactory(conf.jdbc,conf.user,conf.password);
PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf,null);
pcf.setValidationQuery(conf.validationQuery);
//, pool, null, conf.validationQuery, false, true,abandondedConfig);
logger.info("Creating pool "+conf.toString());
// create a generic pool
GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(pcf);
pool.setMaxTotal(conf.maxTotal);
pool.setMaxIdle(conf.maxIdle);
pool.setMinIdle(conf.minIdle);
pool.setMaxWaitMillis(conf.maxWait);
pool.setTimeBetweenEvictionRunsMillis(conf.timeBetweenEvictionRunsMillis);
pool.setMinEvictableIdleTimeMillis(conf.minEvictableIdleTimeMillis);
pool.setTestWhileIdle(conf.testWhileIdle);
pool.setTestOnBorrow(conf.testOnBorrow);
AbandonedConfig abandonedConfig = new AbandonedConfig();
abandonedConfig.setRemoveAbandonedOnMaintenance(conf.removeAbanadoned);
abandonedConfig.setRemoveAbandonedTimeout(conf.removeAbandonedTimeout);
abandonedConfig.setLogAbandoned(conf.logAbandonded);
pool.setAbandonedConfig(abandonedConfig);
pcf.setPool(pool);
DataSource ds = new PoolingDataSource(pool);
dataSources.put(conf.name, ds);
} catch (ClassNotFoundException e) {
logger.error("Failed to create datasource for "+conf.name+ " with class "+conf.driverClassName);
}
}
else
{
logger.error("Pool "+conf.name+" already exists. Can't change existing datasource at present.");
}
}