本文整理汇总了Java中org.apache.commons.dbcp2.PoolableConnectionFactory类的典型用法代码示例。如果您正苦于以下问题:Java PoolableConnectionFactory类的具体用法?Java PoolableConnectionFactory怎么用?Java PoolableConnectionFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PoolableConnectionFactory类属于org.apache.commons.dbcp2包,在下文中一共展示了PoolableConnectionFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: check
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入依赖的package包/类
@Override
protected Result check() throws Exception {
GenericObjectPool<PoolableConnection> pool = (GenericObjectPool<PoolableConnection>) connectionPool;
if (pool.getNumWaiters() > maxWaitingConnections) {
return Result.unhealthy("Overloaded connection pool. name="
+ poolName + " nbrWaiters=" + pool.getNumWaiters());
}
PoolableConnectionFactory poolFactory = (PoolableConnectionFactory) pool
.getFactory();
PoolableConnection conn = null;
try {
conn = pool.borrowObject();
poolFactory.validateConnection(conn);
} catch (Exception e) {
return Result
.unhealthy("Database connection validation error. error="
+ ExceptionUtils.getStackTrace(e));
} finally {
DbUtils.closeQuietly(conn);
}
return Result.healthy();
}
示例3: 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");
}
示例4: SqlCache
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入依赖的package包/类
/**
* Constructor.
*
* @param dataSource
* The data source
* @param maxSize
* The max entry count
* @param poolSize
* The number of connections in the pool
* @param lockSource
* The lock source
*/
public SqlCache( DataSource dataSource, int maxSize, int poolSize, LockSource lockSource )
{
this.maxSize = maxSize;
this.lockSource = lockSource;
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal( poolSize );
config.setMaxIdle( poolSize );
config.setMinIdle( poolSize );
DataSourceConnectionFactory connectionFactory = new DataSourceConnectionFactory( dataSource );
PoolableConnectionFactory pooledObjectFactory = new PoolableConnectionFactory( connectionFactory, null );
GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>( pooledObjectFactory, config );
pooledObjectFactory.setPool( pool );
this.dataSource = new PoolingDataSource<PoolableConnection>( pool );
}
示例5: start
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入依赖的package包/类
@Override
public void start() {
File databaseDir = new File(JiveGlobals.getHomeDirectory(), File.separator + "embedded-db");
// If the database doesn't exist, create it.
if (!databaseDir.exists()) {
databaseDir.mkdirs();
}
try {
serverURL = "jdbc:hsqldb:" + databaseDir.getCanonicalPath() + File.separator + "openfire";
}
catch (IOException ioe) {
Log.error("EmbeddedConnectionProvider: Error starting connection pool: ", ioe);
}
final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(serverURL, "sa", "");
final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
poolableConnectionFactory.setMaxConnLifetimeMillis((long) (0.5 * JiveConstants.DAY));
final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMinIdle(3);
poolConfig.setMaxTotal(25);
final GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, poolConfig);
poolableConnectionFactory.setPool(connectionPool);
dataSource = new PoolingDataSource<>(connectionPool);
}
示例6: setup
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入依赖的package包/类
private void setup() {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
url, username, password);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
connectionFactory, null);
ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(
poolableConnectionFactory);
poolableConnectionFactory.setPool(connectionPool);
PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(
connectionPool);
this.dataSource = dataSource;
logger.debug("get DataSource");
}
示例7: createPool
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入依赖的package包/类
public static DataSource createPool(ConnectionFactory connectionFactory, int isolation) {
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
connectionFactory, null);
poolableConnectionFactory.setMaxConnLifetimeMillis(3000);
poolableConnectionFactory.setDefaultAutoCommit(false);
poolableConnectionFactory.setDefaultReadOnly(true);
poolableConnectionFactory
.setDefaultTransactionIsolation(isolation);
ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(
poolableConnectionFactory);
poolableConnectionFactory.setPool(connectionPool);
PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(
connectionPool);
return dataSource;
}
示例8: createPool
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入依赖的package包/类
public static DataSource createPool(String aName)
{
ResourceBundle bundle = ResourceBundle.getBundle(aName);
String password = bundle.getString("PWD");
String principal = bundle.getString("PRINCIPAL");
String conn = bundle.getString("CONNECTION");
DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
conn, principal, password);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
connectionFactory, null);
ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(
poolableConnectionFactory);
poolableConnectionFactory.setPool(connectionPool);
PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(
connectionPool);
return dataSource;
}
示例9: create
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入依赖的package包/类
static PoolableDataSource create(ConnectionFactory connectionFactory) {
// setup our pool config object
GenericObjectPoolConfig config = setupPoolConfig();
PoolableConnectionFactory poolableConnectionFactory =
new PoolableConnectionFactory(connectionFactory, null);
// Set max lifetime of a connection in milli-secs, after which it will
// always fail activation, passivation, and validation.
// Value of -1 means infinite life time. The default value
// defined in this class is 10 minutes.
long connTtlMillis = retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_TTL, MAX_TTL_CONN_MS);
poolableConnectionFactory.setMaxConnLifetimeMillis(connTtlMillis);
if (LOG.isInfoEnabled()) {
LOG.info("Setting Time-To-Live interval for live connections (" +
connTtlMillis + ") milli-secs");
}
ObjectPool<PoolableConnection> connectionPool =
new GenericObjectPool<>(poolableConnectionFactory, config);
poolableConnectionFactory.setPool(connectionPool);
AthenzDataSource dataSource = new AthenzDataSource(connectionPool);
return dataSource;
}
示例10: DbcpConnectionPoolHealthCheck
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入依赖的package包/类
public DbcpConnectionPoolHealthCheck(
GenericObjectPool<PoolableConnection> pool, String poolName) {
Validate.notNull(pool, "Null connection pool not allowed.");
Validate.notEmpty(poolName, "Null or blank poolName not allowed.");
Validate.isTrue(pool.getFactory() instanceof PoolableConnectionFactory,
"this check only handles connection pools using PoolableConnectionFactory");
connectionPool = pool;
this.poolName = poolName;
this.setMaxWaitingConnections(3);
}
示例11: createDataSource
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入依赖的package包/类
/**
* Create an automatically managed pool of database connections to the supplied JDBC database URL.
*
* Creating new database connections is usually an expensive operation,
* taking up to seconds to prepare for a transaction that only lasts milliseconds.
* The JDBC in Java 7 and 8 support connection pooling, which is critical for performance.
* We are using the standard JDBC DataSource interface, with the Apache commons-dbcp implementation.
* This should be roughly the same connection pooling used in the Tomcat application server.
*
* Here are some sample database URLs to serve as patterns:
* H2_FILE_URL = "jdbc:h2:file:~/test-db"; // H2 memory does not seem faster than H2 file
* SQLITE_FILE_URL = "jdbc:sqlite:/Users/abyrd/test-db";
* POSTGRES_LOCAL_URL = "jdbc:postgresql://localhost/catalogue";
*
* For local Postgres connections, you can supply a null username and password to use host-based authentication.
*/
public static DataSource createDataSource (String url, String username, String password) {
String characterEncoding = Charset.defaultCharset().toString();
LOG.debug("Default character encoding: {}", characterEncoding);
if (!Charset.defaultCharset().equals(StandardCharsets.UTF_8)) {
// Character encoding must be set to UTF-8 in order for the database connection to work without error.
// To override default encoding at runtime, run application jar with encoding environment variable set to
// UTF-8 (or update IDE settings). TODO we should also check that JDBC and the database know to use UTF-8.
throw new RuntimeException("Your system's default encoding (" + characterEncoding + ") is not supported. Please set it to UTF-8. Example: java -Dfile.encoding=UTF-8 application.jar");
}
// ConnectionFactory can handle null username and password (for local host-based authentication)
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, username, password);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
GenericObjectPool connectionPool = new GenericObjectPool(poolableConnectionFactory);
// TODO: set other options on connectionPool?
connectionPool.setMaxTotal(300);
connectionPool.setMaxIdle(4);
connectionPool.setMinIdle(2);
poolableConnectionFactory.setPool(connectionPool);
// We also want auto-commit switched off for bulk inserts, and also because fetches are super-slow with
// auto-commit turned on. Apparently it interferes with result cursors.
poolableConnectionFactory.setDefaultAutoCommit(false);
return new PoolingDataSource(connectionPool);
// We might want already-loaded feeds to be treated as read-only.
// But we need to call this on the connection, not the connectionSource.
// connection.setReadOnly();
// Not sure we need to close cursors - closing the pool-wrapped connection when we're done with it should also close cursors.
// will this help? https://stackoverflow.com/a/18300252
// connection.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
}
示例12: getDataSource
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入依赖的package包/类
private DataSource getDataSource() {
if (dataSource == null) {
String host = getConfigManager().get().getString(Keys.Db.SERVER);
int port = getConfigManager().get().getInt(Keys.Db.PORT);
String dbname = getConfigManager().get().getString(Keys.Db.DB_NAME);
String user = getConfigManager().get().getString(Keys.Db.USER);
String password = getConfigManager().get().getString(Keys.Db.PASSWORD);
Properties props = new Properties();
props.setProperty("user", user);
props.setProperty("password", password);
props.setProperty("defaultTransactionIsolation", "NONE");
// props.setProperty("validationQuery", "SELECT 1");
// props.setProperty("testOnBorrow", "true");
String url = "jdbc:mysql://" + host + ":" + port + "/" + dbname + "?tcpKeepAlive=true";
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMinIdle(getDbCnxPoolMin());
poolConfig.setMaxTotal(getDbCnxPoolMax());
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, props);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
null);
GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory,
poolConfig);
poolableConnectionFactory.setPool(connectionPool);
dataSource = new PoolingDataSource<>(connectionPool);
LOG.info("MYSQL[default] connection to {}", url);
}
return dataSource;
}
示例13: setupDataSource
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入依赖的package包/类
public static DataSource setupDataSource(String connectURI, Properties properties) {
//
// First, we'll create a ConnectionFactory that the
// pool will use to create Connections.
// We'll use the DriverManagerConnectionFactory,
// using the connect string passed in the command line
// arguments.
//
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, properties);
//
// Next we'll create the PoolableConnectionFactory, which wraps
// the "real" Connections created by the ConnectionFactory with
// the classes that implement the pooling functionality.
//
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
//
// Now we'll need a ObjectPool that serves as the
// actual pool of connections.
//
// We'll use a GenericObjectPool instance, although
// any ObjectPool implementation will suffice.
//
ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
// Set the factory's pool property to the owning pool
poolableConnectionFactory.setPool(connectionPool);
//
// Finally, we create the PoolingDriver itself,
// passing in the object pool we created.
//
PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);
return dataSource;
}
示例14: createConnectionPool
import org.apache.commons.dbcp2.PoolableConnectionFactory; //导入依赖的package包/类
private void createConnectionPool(String url, String user, String propertyKey,
Properties properties) throws SQLException, ClassNotFoundException {
ConnectionFactory connectionFactory =
new DriverManagerConnectionFactory(url, properties);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
connectionFactory, null);
ObjectPool connectionPool = new GenericObjectPool(poolableConnectionFactory);
poolableConnectionFactory.setPool(connectionPool);
Class.forName(properties.getProperty(DRIVER_KEY));
PoolingDriver driver = new PoolingDriver();
driver.registerPool(propertyKey + user, connectionPool);
getJDBCConfiguration(user).saveDBDriverPool(propertyKey, driver);
}
示例15: 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);
}