List of usage examples for org.apache.commons.dbcp PoolableConnectionFactory PoolableConnectionFactory
public PoolableConnectionFactory(ConnectionFactory connFactory, ObjectPool pool, KeyedObjectPoolFactory stmtPoolFactory, String validationQuery, boolean defaultReadOnly, boolean defaultAutoCommit)
From source file:uk.ac.sanger.cgp.dbcon.pooling.Pool.java
/** * Creates the pool and the data source which provides the pooling ability */// ww w .j av a 2 s . com private void createPool() { PoolableObjectFactory pof = null; // Creation of the generic pool and linking a factory to it GenericObjectPool underlyingConnectionPool = new GenericObjectPool(pof, config.getMaxActive(), config.getExhausted(), config.getMaxWait(), config.getMaxIdle(), config.isTestOnBorrow(), config.isTestOnReturn(), config.getTimeBetweenEvictRun(), config.getNumTestsPerEvictionRun(), config.getMinEvictTime(), config.isTestWhileIdle()); setUnderlyingConnectionPool(underlyingConnectionPool); // This section allows for PreparedStatements to be used GenericKeyedObjectPoolFactory kopf = null; if (config.getCachedPreparedStatements() != 0) { kopf = new GenericKeyedObjectPoolFactory(null, -1, // unlimited maxActive // (per key) GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL, 0, // maxWait 1, // maxIdle (per key) config.getCachedPreparedStatements()); } // Creating the correct connection factory ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(config.getWorkingUrl(), config.getUsername(), config.getPassword()); // Final construction of the poolable connection factory from: // PoolableConnectionFactory(ConnectionFactory connFactory, ObjectPool pool, // KeyedObjectPoolFactory stmtPoolFactory, String validationQuery, // boolean defaultReadOnly, boolean defaultAutoCommit) PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, getUnderlyingConnectionPool(), kopf, null, false, false); // Setting the validation query poolableConnectionFactory.setValidationQuery(config.getValidationQuery()); // Creating the pooled datasource setDataSource(new PoolingDataSource(getUnderlyingConnectionPool())); }
From source file:us.daveread.basicquery.BasicQuery.java
/** * Configures the database connection pool and sets the pool configuration. * //from w ww . j a va 2 s. co m * @param connectionPool * The ObjectPool * @param connectURI * The url specifying the database to which it * connects using JDBC * @param pUserId * The user id attribute * @param pPassword * The password attribute * * @throws java.lang.Exception * Throws an SQL exception that provides * information on a database access error * or other errors * * @todo Make the pool access dynamic (use the dynamic class loader?) * so that the application will compile/run without the Apache * commons library. */ private void configurePool(GenericObjectPool connectionPool, String connectURI, String pUserId, String pPassword) throws Exception { String lowerCaseConnectURI; String validationQuery; lowerCaseConnectURI = connectURI.toLowerCase(); validationQuery = null; if (lowerCaseConnectURI.startsWith("jdbc:sybase")) { validationQuery = "select getdate()"; } else if (lowerCaseConnectURI.startsWith("jdbc:mysql")) { validationQuery = "select 1"; } else if (lowerCaseConnectURI.startsWith("jdbc:oracle")) { validationQuery = "select sysdate from dual"; } else if (lowerCaseConnectURI.startsWith("jdbc:mssql")) { validationQuery = "select 1"; } // Pool settings - someday a dialog and persistence should be // added to put these under user control connectionPool.setMaxActive(1); connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK); connectionPool.setMaxWait(CONN_POOL_MAX_WAIT_MS); connectionPool.setMaxIdle(CONN_POOL_MAX_IDLE_CONNECTIONS); connectionPool.setTimeBetweenEvictionRunsMillis(CONN_POOL_TIME_BETWEEN_EVICT_RUNS_MS); connectionPool.setNumTestsPerEvictionRun(CONN_POOL_NUM_TESTS_PER_EVICT_RUN); connectionPool.setMinEvictableIdleTimeMillis(CONN_POOL_EVICT_IDLE_TIME_MS); final DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, pUserId, pPassword); final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); if (validationQuery != null) { connectionPool.setTestOnBorrow(true); connectionPool.setTestWhileIdle(true); poolableConnectionFactory.setValidationQuery(validationQuery); } }
From source file:utility.ConnectionPool.java
/** * * @return @throws Exception/*from w w w . j a v a2 s. c om*/ */ public DataSource setUp() throws Exception { /** * Load JDBC Driver class. */ Class.forName(ConnectionPool.DRIVER).newInstance(); /** * Creates an instance of GenericObjectPool that holds our pool of * connections object. */ connectionPool = new GenericObjectPool(); // set the max number of connections connectionPool.setMaxActive(connections); // if the pool is exhausted (i.e., the maximum number of active objects has been reached), the borrowObject() method should simply create a new object anyway connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW); /** * Creates a connection factory object which will be use by the pool to * create the connection object. We passes the JDBC url info, username and * password. */ ConnectionFactory cf = new DriverManagerConnectionFactory(URL, USERNAME, PASSWORD); /** * Creates a PoolableConnectionFactory that will wraps the connection object * created by the ConnectionFactory to add object pooling functionality. */ PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, connectionPool, null, null, false, true); return new PoolingDataSource(connectionPool); }