List of usage examples for org.apache.commons.dbcp BasicDataSource setLogAbandoned
public void setLogAbandoned(boolean logAbandoned)
From source file:edu.tamu.tcat.db.core.AbstractDataSourceFactory.java
/** * Create a new {@link BasicDataSource} from the specified {@link DSProperties} *///from w ww.java 2s . co m protected synchronized BasicDataSource createDataSource(final Properties parameters) throws DataSourceException { BasicDataSource dataSource; final Driver driver = getDriver(); final String connectionUrl = getConnectionUrl(parameters); final Properties connectionProps = getConnectionProperties(parameters); dataSource = new BasicDataSource() { @Override protected ConnectionFactory createConnectionFactory() throws SQLException { //The loading of the driver via class-loader does not work properly in OSGI. if (driver.acceptsURL(getUrl())) { if (getValidationQuery() == null) { setTestOnBorrow(false); setTestOnReturn(false); setTestWhileIdle(false); } ConnectionFactory driverConnectionFactory = new DriverConnectionFactory(driver, connectionUrl, connectionProps); return driverConnectionFactory; } return super.createConnectionFactory(); } }; // dataSource.setDriverClassLoader(Driver.class.getClassLoader()); // should be included in the connection properties and not needed // dataSource.setUsername(key.getUsername()); // dataSource.setPassword(key.getPassword()); dataSource.setDriverClassName(driver.getClass().getName()); dataSource.setUrl(connectionUrl); dataSource.setMaxActive(getMaxActiveConnections(parameters)); dataSource.setMaxIdle(getMaxIdleConnections(parameters)); dataSource.setMinIdle(0); dataSource.setMinEvictableIdleTimeMillis(10000); dataSource.setTimeBetweenEvictionRunsMillis(1000); dataSource.setLogAbandoned(true); dataSource.setRemoveAbandoned(true);//seconds dataSource.setRemoveAbandonedTimeout(60); return dataSource; }
From source file:jetsennet.orm.datasource.DbcpDataSourceCreator.java
@Override public DataSource createDatasource(ConnectionInfo conn, DbPoolInfo props) throws SQLException { org.apache.commons.dbcp.BasicDataSource dataSource = new org.apache.commons.dbcp.BasicDataSource(); dataSource.setDriverClassName(conn.driver); dataSource.setUrl(conn.url);//from w w w. j av a2 s. c om dataSource.setUsername(conn.user); dataSource.setPassword(conn.pwd); String connectionProperties = props.get("connectionProperties"); if (connectionProperties != null && connectionProperties.trim().length() > 0) { dataSource.setConnectionProperties(connectionProperties); } Boolean defaultAutoCommit = Utils.str2Boolean(props.get("defaultAutoCommit")); if (defaultAutoCommit != null) { dataSource.setDefaultAutoCommit(defaultAutoCommit); } Boolean defaultReadOnly = Utils.str2Boolean(props.get("defaultReadOnly")); if (defaultReadOnly != null) { dataSource.setDefaultReadOnly(defaultReadOnly); } Integer defaultTransactionIsolation = Utils.strToInteger(props.get("defaultTransactionIsolation")); if (defaultTransactionIsolation != null) { dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation); } String defaultCatalog = props.get("defaultCatalog"); if (defaultCatalog != null && defaultCatalog.trim().length() > 0) { dataSource.setDefaultCatalog(defaultCatalog); } int initialSize = Utils.strToInt(props.get("initialSize")); if (initialSize > 0) { dataSource.setInitialSize(initialSize); } int maxActive = Utils.strToInt(props.get("maxActive")); if (maxActive > 0) { dataSource.setMaxActive(maxActive); } int maxIdle = Utils.strToInt(props.get("maxIdle")); if (maxIdle > 0) { dataSource.setMaxIdle(maxIdle); } int minIdle = Utils.strToInt(props.get("minIdle")); if (minIdle > 0) { dataSource.setMinIdle(minIdle); } int maxWait = Utils.strToInt(props.get("maxWait")); if (maxWait > 0) { dataSource.setMaxWait(maxWait); } String validationQuery = props.get("validationQuery"); if (validationQuery != null && validationQuery.trim().length() > 0) { dataSource.setValidationQuery(validationQuery); } Integer validationQueryTimeout = Utils.strToInteger(props.get("validationQueryTimeout")); if (validationQueryTimeout != null) { dataSource.setValidationQueryTimeout(validationQueryTimeout); } Boolean testOnBorrow = Utils.str2Boolean(props.get("testOnBorrow")); if (testOnBorrow != null) { dataSource.setTestOnBorrow(testOnBorrow); } Boolean testOnReturn = Utils.str2Boolean(props.get("testOnReturn")); if (testOnReturn != null) { dataSource.setTestOnReturn(testOnReturn); } Boolean testWhileIdle = Utils.str2Boolean(props.get("testWhileIdle")); if (testWhileIdle != null) { dataSource.setTestWhileIdle(testWhileIdle); } Integer timeBetweenEvictionRunsMillis = Utils.strToInteger(props.get("timeBetweenEvictionRunsMillis")); if (timeBetweenEvictionRunsMillis != null) { dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); } Integer numTestsPerEvictionRun = Utils.strToInteger(props.get("numTestsPerEvictionRun")); if (numTestsPerEvictionRun != null) { dataSource.setNumTestsPerEvictionRun(numTestsPerEvictionRun); } int minEvictableIdleTimeMillis = Utils.strToInt(props.get("minEvictableIdleTimeMillis")); if (minEvictableIdleTimeMillis > 0) { dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); } Boolean removeAbandoned = Utils.str2Boolean(props.get("removeAbandoned")); if (removeAbandoned != null) { dataSource.setRemoveAbandoned(removeAbandoned); } int removeAbandonedTimeout = Utils.strToInt(props.get("removeAbandonedTimeout")); if (removeAbandonedTimeout > 0) { dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout); } Boolean logAbandoned = Utils.str2Boolean(props.get("logAbandoned")); if (logAbandoned != null) { dataSource.setLogAbandoned(logAbandoned); } return dataSource; }
From source file:org.geosde.core.jdbc.data.datasource.DataSourceUtil.java
/** * Builds up a default DBCP DataSource that easy to use connection factories * can use to setup a connection pool./*from w w w .j a v a 2 s . c o m*/ * * @param url * the jdbc url * @param driverName * the jdbc driver full qualified class name * @param username * @param password * @param maxActive maximum number of concurrent connections in the pool * @param minIdle minimum number of concurrent connections in the pool * @param validationQuery * the validation query to be used for connection liveliness on * borrow, or null, if no check is to be performed * @param cachePreparedStatements * wheter to cache prepared statements or not * @return * @throws DataSourceException */ public static ManageableDataSource buildDefaultDataSource(String url, String driverName, String username, String password, int maxActive, int minIdle, String validationQuery, boolean cachePreparedStatements, int removeAbandonedTimeout) throws DataSourceException { // basics BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(driverName); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setAccessToUnderlyingConnectionAllowed(true); // pool size dataSource.setMaxActive(maxActive); dataSource.setMinIdle(minIdle); // pool eviction settings dataSource.setMinEvictableIdleTimeMillis(1000 * 20); dataSource.setTimeBetweenEvictionRunsMillis(1000 * 10); // connection validation if (validationQuery != null) { dataSource.setTestOnBorrow(true); dataSource.setValidationQuery(validationQuery); } // prepared statement cache if (cachePreparedStatements) { dataSource.setPoolPreparedStatements(true); dataSource.setMaxOpenPreparedStatements(10); } // remove abandoned connections (I know it's deprecated, but we do want // something shaving off lost connections. Let's give them 5 minutes of // continuous usage if (removeAbandonedTimeout > 0) { dataSource.setRemoveAbandoned(true); dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout); dataSource.setLogAbandoned(true); } Connection conn = null; try { conn = dataSource.getConnection(); } catch (Exception e) { throw new DataSourceException("Connection test failed ", e); } finally { if (conn != null) try { conn.close(); } catch (SQLException e) { } } return new DBCPDataSource(dataSource); }
From source file:se.unlogic.hierarchy.core.utils.DBCPUtils.java
public static BasicDataSource createConnectionPool(DataSourceDescriptor dataSourceDescriptor) { BasicDataSource basicDataSource = new BasicDataSource(); basicDataSource.setDriverClassName(dataSourceDescriptor.getDriver()); basicDataSource.setUsername(dataSourceDescriptor.getUsername()); basicDataSource.setPassword(dataSourceDescriptor.getPassword()); basicDataSource.setUrl(dataSourceDescriptor.getUrl()); basicDataSource.setDefaultCatalog(dataSourceDescriptor.getDefaultCatalog()); basicDataSource.setLogAbandoned(dataSourceDescriptor.logAbandoned()); basicDataSource.setRemoveAbandoned(dataSourceDescriptor.removeAbandoned()); if (dataSourceDescriptor.getRemoveTimeout() != null) { basicDataSource.setRemoveAbandonedTimeout(dataSourceDescriptor.getRemoveTimeout()); }/*from w w w .j ava2s . c om*/ basicDataSource.setTestOnBorrow(dataSourceDescriptor.testOnBorrow()); basicDataSource.setValidationQuery(dataSourceDescriptor.getValidationQuery()); basicDataSource.setMaxWait(dataSourceDescriptor.getMaxWait()); basicDataSource.setMaxActive(dataSourceDescriptor.getMaxActive()); basicDataSource.setMaxIdle(dataSourceDescriptor.getMaxIdle()); basicDataSource.setMinIdle(dataSourceDescriptor.getMinIdle()); return basicDataSource; }