List of usage examples for org.apache.commons.dbcp2 PoolableConnectionFactory PoolableConnectionFactory
public PoolableConnectionFactory(ConnectionFactory connFactory, ObjectName dataSourceJmxName)
From source file:eu.peppol.persistence.jdbc.OxalisDataSourceFactoryDbcpImpl.java
/** * Creates a DataSource with connection pooling as provided by Apache DBCP * * @return a DataSource//from w ww . ja va 2s . c o m */ DataSource configureAndCreateDataSource(RepositoryConfiguration configuration) { log.debug("Configuring DataSource wrapped in a Database Connection Pool, using custom loader"); String jdbcDriverClassPath = configuration.getJdbcDriverClassPath(); log.debug("Loading JDBC Driver with custom class path: " + jdbcDriverClassPath); // Creates a new class loader, which will be used for loading our JDBC driver URLClassLoader urlClassLoader = getOxalisClassLoaderForJdbc(jdbcDriverClassPath); String className = configuration.getJdbcDriverClassName(); String connectURI = configuration.getJdbcConnectionUri().toString(); String userName = configuration.getJdbcUsername(); String password = configuration.getJdbcPassword(); log.debug("className=" + className); log.debug("connectURI=" + connectURI); log.debug("userName=" + userName); log.debug("password=" + password); // Loads the JDBC Driver in a separate class loader Driver driver = getJdbcDriver(jdbcDriverClassPath, urlClassLoader, className); Properties properties = new Properties(); properties.put("user", userName); properties.put("password", password); // DBCP factory which will produce JDBC Driver instances ConnectionFactory driverConnectionFactory = new DriverConnectionFactory(driver, connectURI, properties); // DBCP Factory holding the pooled connection, which are created by the driver connection factory and held in the supplied pool ObjectName dataSourceJmxName = null; try { dataSourceJmxName = new ObjectName("no.difi.oxalis", "connectionPool", "OxalisDB"); } catch (MalformedObjectNameException e) { throw new IllegalStateException(e.getMessage(), e); } PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(driverConnectionFactory, dataSourceJmxName); String validationQuery = configuration.getValidationQuery(); if (validationQuery != null) { poolableConnectionFactory.setValidationQuery(validationQuery); } // DBCP object pool holding our driver connections GenericObjectPool<PoolableConnection> genericObjectPool = new GenericObjectPool<PoolableConnection>( poolableConnectionFactory); poolableConnectionFactory.setPool(genericObjectPool); genericObjectPool.setMaxTotal(100); genericObjectPool.setMaxIdle(30); genericObjectPool.setMaxWaitMillis(10000); genericObjectPool.setTestOnBorrow(true); // Test the connection returned from the pool genericObjectPool.setTestWhileIdle(true); // Test idle instances visited by the pool maintenance thread and destroy any that fail validation genericObjectPool.setTimeBetweenEvictionRunsMillis(60 * 60 * 1000); // Test every hour // Creates the actual DataSource instance PoolingDataSource poolingDataSource = new PoolingDataSource(genericObjectPool); return poolingDataSource; }
From source file:JDBCPool.dbcp.demo.offical.PoolingDriverExample.java
/** * ?connectURI?PoolingDriver//from w w w .jav a 2s . c o m * @param connectURI * @throws Exception */ public static void setupDriver(String connectURI) throws Exception { // 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, null); // 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... // Class.forName("org.apache.commons.dbcp2.PoolingDriver"); PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); // // ...and register our pool with it. // driver.registerPool("example", connectionPool); // // Now we can just use the connect string "jdbc:apache:commons:dbcp:example" to access our pool of Connections. // }
From source file:PoolingDriverExample.java
public static void setupDriver(String connectURI) throws Exception { ////from ww w . j av a 2s . co m // 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, null); // // 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... // Class.forName("org.apache.commons.dbcp2.PoolingDriver"); PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); // // ...and register our pool with it. // driver.registerPool("example", connectionPool); // // Now we can just use the connect string "jdbc:apache:commons:dbcp:example" // to access our pool of Connections. // }
From source file:PoolingDriverExample.java
public static void setupDriver(String connectURI) throws Exception { ///*ww w.ja v a 2 s . c o m*/ // 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, null); // // 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); // // Finally, we create the PoolingDriver itself... // Class.forName("org.apache.commons.dbcp2.PoolingDriver"); PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); // // ...and register our pool with it. // driver.registerPool("example", connectionPool); // // Now we can just use the connect string "jdbc:apache:commons:dbcp:example" // to access our pool of Connections. // }
From source file:com.github.brandtg.switchboard.TestMysqlReplicationApplier.java
private PoolingDataSource<PoolableConnection> getDataSource() throws Exception { // DBCP2 pool ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbc, "root", ""); ObjectName poolName = new ObjectName(JdbcBasedLogIndex.class.getCanonicalName(), URLEncoder.encode(jdbc, "UTF-8"), "replicatorConnectionPool"); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, poolName);/*from w w w .ja v a 2 s. c o m*/ ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>( poolableConnectionFactory); poolableConnectionFactory.setPool(connectionPool); return new PoolingDataSource<PoolableConnection>(connectionPool); }
From source file:com.yahoo.athenz.common.server.db.DataSourceFactory.java
static PoolableDataSource create(ConnectionFactory connectionFactory) { // setup our pool config object GenericObjectPoolConfig config = setupPoolConfig(); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);/*from www .j a v a 2s .com*/ // 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; }
From source file:de.alexandria.cms.config.SpringConfigBackendDatabase.java
private PoolableConnectionFactory getPoolableConnectionFactory() { DataSourceConnectionFactory dataSourceConnectionFactory = getDataSourceConnectionFactory(); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory( dataSourceConnectionFactory, null); poolableConnectionFactory.setValidationQuery("SELECT 1"); long maxConnLifetimeMillis = 600000; // ten minutes poolableConnectionFactory.setMaxConnLifetimeMillis(maxConnLifetimeMillis); return poolableConnectionFactory; }
From source file:eu.peppol.persistence.jdbc.OxalisDataSourceFactoryDbcpImplTest.java
private PoolingDataSource createPoolingDataSource(ConnectionFactory driverConnectionFactory) { PoolableConnectionFactory poolableConnectionFactory = null; try {// www . j av a 2 s. com poolableConnectionFactory = new PoolableConnectionFactory(driverConnectionFactory, new ObjectName("no.difi.oxalis", "connectionPool", "TestPool")); GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>( poolableConnectionFactory); poolableConnectionFactory.setPool(pool); poolableConnectionFactory.setValidationQuery("select 1"); return new PoolingDataSource(pool); } catch (MalformedObjectNameException e) { throw new IllegalStateException("Unable to create poolable conneciton factory: " + e.getMessage(), e); } }
From source file:com.github.brandtg.switchboard.JdbcBasedLogIndex.java
@Override public void start() throws Exception { Class.forName(driverClass);// w w w . jav a 2 s .c o m LOG.info("Loaded driver {}", driverClass); // DBCP2 pool ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionString, user, password); ObjectName poolName = new ObjectName(JdbcBasedLogIndex.class.getCanonicalName(), URLEncoder.encode(connectionString, ENCODING), "indexConnectionPool"); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, poolName); ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>( poolableConnectionFactory); poolableConnectionFactory.setPool(connectionPool); this.dataSource = new PoolingDataSource<PoolableConnection>(connectionPool); LOG.info("Opened connection pool to {} as {}", connectionString, user); createTable(); }
From source file:no.sr.ringo.persistence.jdbc.RingoDataSourceFactoryDbcpImpl.java
private PoolingDataSource getPoolingDataSource(JdbcConfiguration configuration, String connectURI, String userName, String password, Driver driver) { Properties properties = new Properties(); properties.put("user", userName); properties.put("password", password); // DBCP factory which will produce JDBC Driver instances ConnectionFactory driverConnectionFactory = new DriverConnectionFactory(driver, connectURI, properties); // DBCP Factory holding the pooled connection, which are created by the driver connection factory and held in the supplied pool ObjectName dataSourceJmxName; try {//w w w. j ava2 s. c o m dataSourceJmxName = new ObjectName("no.difi.oxalis", "connectionPool", "OxalisDB"); } catch (MalformedObjectNameException e) { throw new IllegalStateException(e.getMessage(), e); } PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(driverConnectionFactory, dataSourceJmxName); if (configuration.getValidationQuery().isPresent()) { poolableConnectionFactory.setValidationQuery(configuration.getValidationQuery().get()); } // DBCP object pool holding our driver connections GenericObjectPool<PoolableConnection> genericObjectPool = new GenericObjectPool<PoolableConnection>( poolableConnectionFactory); poolableConnectionFactory.setPool(genericObjectPool); genericObjectPool.setMaxTotal(100); genericObjectPool.setMaxIdle(30); genericObjectPool.setMaxWaitMillis(10000); genericObjectPool.setTestOnBorrow(true); // Test the connection returned from the pool genericObjectPool.setTestWhileIdle(true); // Test idle instances visited by the pool maintenance thread and destroy any that fail validation genericObjectPool.setTimeBetweenEvictionRunsMillis(60 * 60 * 1000); // Test every hour // Creates the actual DataSource instance return new PoolingDataSource(genericObjectPool); }