本文整理汇总了Java中org.apache.commons.dbcp2.PoolingDataSource类的典型用法代码示例。如果您正苦于以下问题:Java PoolingDataSource类的具体用法?Java PoolingDataSource怎么用?Java PoolingDataSource使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PoolingDataSource类属于org.apache.commons.dbcp2包,在下文中一共展示了PoolingDataSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: SqlCache
import org.apache.commons.dbcp2.PoolingDataSource; //导入依赖的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 );
}
示例2: start
import org.apache.commons.dbcp2.PoolingDataSource; //导入依赖的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);
}
示例3: setup
import org.apache.commons.dbcp2.PoolingDataSource; //导入依赖的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");
}
示例4: createPool
import org.apache.commons.dbcp2.PoolingDataSource; //导入依赖的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;
}
示例5: createPool
import org.apache.commons.dbcp2.PoolingDataSource; //导入依赖的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;
}
示例6: createDataSource
import org.apache.commons.dbcp2.PoolingDataSource; //导入依赖的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);
}
示例7: getDataSource
import org.apache.commons.dbcp2.PoolingDataSource; //导入依赖的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;
}
示例8: setupDataSource
import org.apache.commons.dbcp2.PoolingDataSource; //导入依赖的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;
}
示例9: start
import org.apache.commons.dbcp2.PoolingDataSource; //导入依赖的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);
}
示例10: setupDataSource
import org.apache.commons.dbcp2.PoolingDataSource; //导入依赖的package包/类
private static void setupDataSource(String url, String user, String password) {
PoolableConnectionFactory factory = new PoolableConnectionFactory(
new DriverManagerConnectionFactory(url, user, password), null);
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(maxIdle);
config.setMaxIdle(maxIdle);
config.setMinIdle(minIdle);
ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(factory, config);
factory.setPool(connectionPool);
dataSource = new PoolingDataSource<>(connectionPool);
}
示例11: createDataSourceInstance
import org.apache.commons.dbcp2.PoolingDataSource; //导入依赖的package包/类
@Override
protected DataSource createDataSourceInstance() throws SQLException {
final TransactionRegistry transactionRegistry = getTransactionRegistry();
if (transactionRegistry == null) {
throw new IllegalStateException("TransactionRegistry has not been set");
}
if (getConnectionPool() == null) {
throw new IllegalStateException("Pool has not been set");
}
final PoolingDataSource<PoolableConnection> pds = new ManagedDataSource<PoolableConnection>(getConnectionPool(), transactionRegistry) {
@Override
public Connection getConnection() throws SQLException {
return new ManagedConnection<PoolableConnection>(getPool(), transactionRegistry, isAccessToUnderlyingConnectionAllowed()) {
@Override
public void close() throws SQLException {
if (!isClosedInternal()) {
try {
if (null != getDelegateInternal()) {
super.close();
}
} finally {
setClosedInternal(true);
}
}
}
@Override
public boolean isClosed() throws SQLException {
return isClosedInternal() || null != getDelegateInternal() && getDelegateInternal().isClosed();
}
};
}
};
pds.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
return pds;
}
示例12: createDataSourceInstance
import org.apache.commons.dbcp2.PoolingDataSource; //导入依赖的package包/类
@Override
protected DataSource createDataSourceInstance() throws SQLException {
final PoolingDataSource<PoolableConnection> pds =
new ManagedDataSource<>(getConnectionPool(), transactionRegistry);
pds.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
return pds;
}
示例13: createDbcp
import org.apache.commons.dbcp2.PoolingDataSource; //导入依赖的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.");
}
}
示例14: setDs
import org.apache.commons.dbcp2.PoolingDataSource; //导入依赖的package包/类
public static void setDs(BasicDataSource datasource) {
//
// 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.
//
try {
Class.forName(datasource.getDriverClassName());
} catch (ClassNotFoundException e) {
}
ConnectionFactory connectionFactory =
new DriverManagerConnectionFactory(datasource.getUrl(), datasource.getUsername(), datasource.getPassword());
//
// 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> poolingDS =
new PoolingDataSource<>(connectionPool);
ds = poolingDS;
}
示例15: deploy
import org.apache.commons.dbcp2.PoolingDataSource; //导入依赖的package包/类
@Override
public DataSourceDeploymentInfo deploy(DataSourceDef dataSourceDef) throws Exception {
DriverDef driverDef = null;
for (DriverDef _driverDef : driverProvider.getDeployments()) {
if (_driverDef.getUuid().equals(dataSourceDef.getDriverUuid())) {
driverDef = _driverDef;
break;
}
}
if (driverDef == null) {
throw new Exception("Required driver: " + dataSourceDef.getDriverUuid() + " is not deployed");
}
final URI uri = artifactResolver.resolve(driverDef.getGroupId(),
driverDef.getArtifactId(),
driverDef.getVersion());
if (uri == null) {
throw new Exception("Unable to get driver library artifact for driver: " + driverDef);
}
final Properties properties = new Properties();
properties.setProperty("user",
dataSourceDef.getUser());
properties.setProperty("password",
dataSourceDef.getPassword());
final URLConnectionFactory urlConnectionFactory = buildConnectionFactory(uri,
driverDef.getDriverClass(),
dataSourceDef.getConnectionURL(),
properties);
//Connection Factory that the pool will use for creating connections.
ConnectionFactory connectionFactory = new DBCPConnectionFactory(urlConnectionFactory);
//Poolable connection factory
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
null);
//The pool to be used by the ConnectionFactory
ObjectPool< PoolableConnection > connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
//Set the factory's pool property to the owning pool
poolableConnectionFactory.setPool(connectionPool);
//Finally create DataSource
PoolingDataSource< PoolableConnection > dataSource = new PoolingDataSource<>(connectionPool);
DataSourceDeploymentInfo deploymentInfo = new DataSourceDeploymentInfo(dataSourceDef.getUuid(),
true,
dataSourceDef.getUuid(),
false);
deploymentRegistry.put(deploymentInfo.getDeploymentId(),
new DBCPDataSource(dataSource));
deploymentInfos.put(deploymentInfo.getDeploymentId(),
deploymentInfo);
deployedDataSources.put(deploymentInfo.getDeploymentId(),
dataSourceDef);
return deploymentInfo;
}