List of usage examples for org.apache.commons.dbcp PoolableConnectionFactory setValidationQuery
public void setValidationQuery(String validationQuery)
From source file:eu.peppol.jdbc.OxalisDataSourceFactoryDbcpImpl.java
/** * Creates a DataSource with connection pooling as provided by Apache DBCP * * @return a DataSource/*from www. ja va2s . c om*/ */ public static DataSource configureAndCreateDataSource() { log.debug("Configuring DataSource wrapped in a Database Connection Pool, using custom loader"); GlobalConfiguration globalConfiguration = GlobalConfigurationImpl.getInstance(); String jdbcDriverClassPath = globalConfiguration.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 = globalConfiguration.getJdbcDriverClassName(); String connectURI = globalConfiguration.getJdbcConnectionURI(); String userName = globalConfiguration.getJdbcUsername(); String password = globalConfiguration.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 object pool holding our driver connections GenericObjectPool genericObjectPool = new GenericObjectPool(null); genericObjectPool.setMaxActive(100); genericObjectPool.setMaxIdle(30); genericObjectPool.setMaxWait(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 // DBCP Factory holding the pooled connection, which are created by the driver connection factory and held in the supplied pool PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(driverConnectionFactory, genericObjectPool, null, null, false, true); String validationQuery = globalConfiguration.getValidationQuery(); poolableConnectionFactory.setValidationQuery(validationQuery); // Creates the actual DataSource instance PoolingDataSource poolingDataSource = new PoolingDataSource(genericObjectPool); return poolingDataSource; }
From source file:eu.peppol.jdbc.OxalisDataSourceFactoryDbcpImplTest.java
private PoolingDataSource createPoolingDataSource(ConnectionFactory driverConnectionFactory, GenericObjectPool genericObjectPool) { PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(driverConnectionFactory, genericObjectPool, null, null, false, true); poolableConnectionFactory.setValidationQuery("select 1"); return new PoolingDataSource(genericObjectPool); }
From source file:org.onexus.collection.store.mysql.internal.MysqlCollectionStore.java
protected DataSource newDataSource() { try {/*w w w.j a v a 2 s. c om*/ Class.forName(Driver.class.getName()); } catch (Exception e) { LOGGER.error("Exception: " + e.getMessage()); } // Config parameters int maxActive = 8; try { maxActive = Integer.valueOf(getPoolMaxActive().trim()).intValue(); } catch (Exception e) { LOGGER.error("Malformed config parameter 'poolMaxActive'"); } byte whenExhausted = GenericObjectPool.WHEN_EXHAUSTED_BLOCK; try { if (getPoolWhenExhausted().trim().equalsIgnoreCase("FAIL")) { whenExhausted = GenericObjectPool.WHEN_EXHAUSTED_FAIL; } if (getPoolWhenExhausted().trim().equalsIgnoreCase("GROW")) { whenExhausted = GenericObjectPool.WHEN_EXHAUSTED_GROW; } } catch (Exception e) { LOGGER.error("Malformed config parameter 'poolWhenExhausted'"); } long maxWait = GenericObjectPool.DEFAULT_MAX_WAIT; try { maxWait = Long.valueOf(getPoolMaxWait().trim()).longValue(); } catch (Exception e) { LOGGER.error("Malformed config parameter 'poolMaxWait'"); } // Initialize the DataSource with a connection pool ConnectionFactory connectionFactory = new MysqlConnectionFactory(); GenericObjectPool connectionPool = new GenericObjectPool(null, maxActive, whenExhausted, maxWait); connectionPool.setTestWhileIdle(true); connectionPool.setTimeBetweenEvictionRunsMillis(3600); connectionPool.setMinEvictableIdleTimeMillis(3600); @SuppressWarnings("unused") PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); poolableConnectionFactory.setValidationQuery("SELECT 1"); return new PoolingDataSource(connectionPool); }
From source file:org.plasma.sdo.jdbc.connect.RDBConnectionManager.java
/** * // w w w. ja v a 2 s. c o m * @param connectURI * - JDBC Connection URI * @param username * - JDBC Connection username * @param password * - JDBC Connection password * @param minIdle * - Minimum number of idel connection in the connection pool * @param maxActive * - Connection Pool Maximum Capacity (Size) * @throws Exception */ private static DataSource setup(String connectURI, String username, String password, int minIdle, int maxActive, Map<String, String> props) throws Exception { // // Create an ObjectPool that serves as the // actual pool of connections. // // Use a GenericObjectPool instance, although // any ObjectPool implementation will suffice. // GenericObjectPool.Config config = createGenericObjectPoolConfig(props); GenericObjectPool connectionPool = new GenericObjectPool(null, config); connectionPool.setMinIdle(minIdle); connectionPool.setMaxActive(maxActive); RDBConnectionManager._pool = connectionPool; // We keep it for two reasons // #1 We need it for statistics/debugging // #2 PoolingDataSource does not have getPool() // method, for some obscure, weird reason. // // Create a ConnectionFactory that the // pool will use to create Connections. // Use the DriverManagerConnectionFactory, // using the connect string from configuration // ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, username, password); // // 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, connectionPool, null, null, false, true); String value = getValue(DBCP_VALIDATIONQUERY, props); if (value != null) { try { poolableConnectionFactory.setValidationQuery(value); } catch (Exception e) { throw new JDBCServiceException(e); } } PoolingDataSource dataSource = new PoolingDataSource(connectionPool); return dataSource; }
From source file:uk.ac.sanger.cgp.dbcon.pooling.Pool.java
/** * Creates the pool and the data source which provides the pooling ability *///from ww w .j av a 2s. 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. * /* ww w . j ava 2 s. c o 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); } }