List of usage examples for org.apache.commons.dbcp BasicDataSource setMaxIdle
public synchronized void setMaxIdle(int maxIdle)
From source file:com.alfaariss.oa.util.database.jdbc.DataSourceFactory.java
private static DataSource addOptionalSettings(IConfigurationManager configurationManager, Element eConfig, BasicDataSource dataSource) throws DatabaseException { try {//from w w w. ja v a 2s . co m String sMaxActive = configurationManager.getParam(eConfig, "maxactive"); int iMaxActive = -1; if (sMaxActive != null) { try { iMaxActive = Integer.parseInt(sMaxActive); } catch (NumberFormatException e) { _logger.error("Wrong 'maxactive' item found in configuration: " + sMaxActive, e); throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ); } } dataSource.setMaxActive(iMaxActive); String sMaxIdle = configurationManager.getParam(eConfig, "maxidle"); if (sMaxIdle != null) { int iMaxIdle = -1; try { iMaxIdle = Integer.parseInt(sMaxIdle); } catch (NumberFormatException e) { _logger.error("Wrong 'maxidle' item found in configuration: " + sMaxIdle, e); throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ); } dataSource.setMaxIdle(iMaxIdle); } String sMaxWait = configurationManager.getParam(eConfig, "maxwait"); if (sMaxWait != null) { int iMaxWait = -1; try { iMaxWait = Integer.parseInt(sMaxWait); } catch (NumberFormatException e) { _logger.error("Wrong 'maxwait' item found in configuration: " + sMaxWait, e); throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ); } dataSource.setMaxWait(iMaxWait); } String sTestOnBorrow = configurationManager.getParam(eConfig, "testonborrow"); if (sTestOnBorrow != null) { boolean bTestOnBorrow = false; if (sTestOnBorrow.equalsIgnoreCase("true")) bTestOnBorrow = true; else if (!sTestOnBorrow.equalsIgnoreCase("false")) { _logger.error("Wrong 'testonborrow' item found in configuration: " + sTestOnBorrow); throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ); } dataSource.setTestOnBorrow(bTestOnBorrow); } String sTestOnReturn = configurationManager.getParam(eConfig, "testonreturn"); if (sTestOnReturn != null) { boolean bTestOnReturn = false; if (sTestOnReturn.equalsIgnoreCase("true")) bTestOnReturn = true; else if (!sTestOnReturn.equalsIgnoreCase("false")) { _logger.error("Wrong 'testonreturn' item found in configuration: " + sTestOnReturn); throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ); } dataSource.setTestOnReturn(bTestOnReturn); } String sTimeBetweenEvictionRunsMillis = configurationManager.getParam(eConfig, "timebetweenevictionrunsmillis"); if (sTimeBetweenEvictionRunsMillis != null) { try { long lTimeBetweenEvictionRunsMillis = Long.parseLong(sTimeBetweenEvictionRunsMillis); dataSource.setTimeBetweenEvictionRunsMillis(lTimeBetweenEvictionRunsMillis); } catch (NumberFormatException e) { _logger.error("Wrong 'timebetweenevictionrunsmillis' item found in configuration: " + sTimeBetweenEvictionRunsMillis, e); throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ); } } String sNumTestsPerEvictionRun = configurationManager.getParam(eConfig, "numtestsperevictionrun"); if (sNumTestsPerEvictionRun != null) { int iNumTestsPerEvictionRun = -1; try { iNumTestsPerEvictionRun = Integer.parseInt(sNumTestsPerEvictionRun); } catch (NumberFormatException e) { _logger.error("Wrong 'numtestsperevictionrun' item found in configuration: " + sNumTestsPerEvictionRun, e); throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ); } dataSource.setNumTestsPerEvictionRun(iNumTestsPerEvictionRun); } String sMinEvictableIdleTimeMillis = configurationManager.getParam(eConfig, "minevictableidletimemillis"); if (sMinEvictableIdleTimeMillis != null) { try { long lMinEvictableIdleTimeMillis = Long.parseLong(sMinEvictableIdleTimeMillis); dataSource.setMinEvictableIdleTimeMillis(lMinEvictableIdleTimeMillis); } catch (NumberFormatException e) { _logger.error("Wrong 'minevictableidletimemillis' item found in configuration: " + sMinEvictableIdleTimeMillis, e); throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ); } } String sTestWhileIdle = configurationManager.getParam(eConfig, "testwhileidle"); if (sTestWhileIdle != null) { boolean bTestWhileIdle = false; if (sTestWhileIdle.equalsIgnoreCase("true")) bTestWhileIdle = true; else if (!sTestWhileIdle.equalsIgnoreCase("false")) { _logger.error("Wrong 'testwhileidle' item found in configuration: " + sTestWhileIdle); throw new DatabaseException(SystemErrors.ERROR_CONFIG_READ); } dataSource.setTestWhileIdle(bTestWhileIdle); } String sValidationQuery = configurationManager.getParam(eConfig, "validationquery"); if (sValidationQuery != null) { dataSource.setValidationQuery(sValidationQuery); } } catch (DatabaseException e) { throw e; } catch (Exception e) { _logger.fatal("Could not create datasource", e); throw new DatabaseException(SystemErrors.ERROR_INTERNAL); } return dataSource; }
From source file:com.alibaba.otter.common.push.datasource.media.MediaPushDataSource.java
protected DataSource doCreateDataSource(String url) { BasicDataSource dbcpDs = new BasicDataSource(); dbcpDs.setInitialSize(initialSize);// ? dbcpDs.setMaxActive(maxActive);// ????? dbcpDs.setMaxIdle(maxIdle);// ?? dbcpDs.setMinIdle(minIdle);// ?0? dbcpDs.setMaxWait(maxWait);// ??-1? dbcpDs.setRemoveAbandoned(true);// ??removeAbandonedTimeout dbcpDs.setLogAbandoned(true);// ?? dbcpDs.setRemoveAbandonedTimeout(removeAbandonedTimeout); // ? dbcpDs.setNumTestsPerEvictionRun(numTestsPerEvictionRun);// ?? dbcpDs.setTestOnBorrow(false);// ?? dbcpDs.setTestOnReturn(false);// ?? dbcpDs.setTestWhileIdle(true);// ???? dbcpDs.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // ???????? dbcpDs.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); // ??????? // ??/* w w w . j a va2 s .c o m*/ dbcpDs.setDriverClassName(driverClassName); dbcpDs.setUrl(url); dbcpDs.setUsername(userName); dbcpDs.setPassword(password); if (dataMediaType.isOracle()) { dbcpDs.addConnectionProperty("restrictGetTables", "true"); dbcpDs.setValidationQuery("select 1 from dual"); } else if (dataMediaType.isMysql()) { // open the batch mode for mysql since 5.1.8 dbcpDs.addConnectionProperty("useServerPrepStmts", "false"); dbcpDs.addConnectionProperty("rewriteBatchedStatements", "true"); dbcpDs.addConnectionProperty("zeroDateTimeBehavior", "convertToNull");// 0000-00-00null dbcpDs.addConnectionProperty("yearIsDateType", "false");// ??year?date? if (StringUtils.isNotEmpty(encoding)) { dbcpDs.addConnectionProperty("characterEncoding", encoding); } dbcpDs.setValidationQuery("select 1"); } else { logger.error("ERROR ## Unknow database type"); } return dbcpDs; }
From source file:com.google.gerrit.server.schema.DataSourceProvider.java
private DataSource open(final SitePaths site, final Config cfg, final Context context, final DataSourceType dst) { ConfigSection dbs = new ConfigSection(cfg, "database"); String driver = dbs.optional("driver"); if (Strings.isNullOrEmpty(driver)) { driver = dst.getDriver();//from ww w.ja v a2 s .co m } String url = dbs.optional("url"); if (Strings.isNullOrEmpty(url)) { url = dst.getUrl(); } String username = dbs.optional("username"); String password = dbs.optional("password"); boolean usePool; if (context == Context.SINGLE_USER) { usePool = false; } else { usePool = cfg.getBoolean("database", "connectionpool", dst.usePool()); } if (usePool) { final BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); if (username != null && !username.isEmpty()) { ds.setUsername(username); } if (password != null && !password.isEmpty()) { ds.setPassword(password); } ds.setMaxActive(cfg.getInt("database", "poollimit", 8)); ds.setMinIdle(cfg.getInt("database", "poolminidle", 4)); ds.setMaxIdle(cfg.getInt("database", "poolmaxidle", 4)); ds.setMaxWait(ConfigUtil.getTimeUnit(cfg, "database", null, "poolmaxwait", MILLISECONDS.convert(30, SECONDS), MILLISECONDS)); ds.setInitialSize(ds.getMinIdle()); return ds; } else { // Don't use the connection pool. // try { final Properties p = new Properties(); p.setProperty("driver", driver); p.setProperty("url", url); if (username != null) { p.setProperty("user", username); } if (password != null) { p.setProperty("password", password); } return new SimpleDataSource(p); } catch (SQLException se) { throw new ProvisionException("Database unavailable", se); } } }
From source file:cn.cuizuoli.gotour.config.DataSourceConfig.java
@Bean public DataSource userDataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(env.getRequiredProperty("user.jdbc.driverClassName")); dataSource.setUrl(env.getRequiredProperty("user.jdbc.url")); dataSource.setUsername(env.getRequiredProperty("user.jdbc.username")); dataSource.setPassword(env.getRequiredProperty("user.jdbc.password")); dataSource.setInitialSize(env.getRequiredProperty("jdbc.initialSize", Integer.class)); dataSource.setMaxActive(env.getRequiredProperty("jdbc.maxActive", Integer.class)); dataSource.setMaxIdle(env.getRequiredProperty("jdbc.maxIdle", Integer.class)); dataSource.setMinIdle(env.getRequiredProperty("jdbc.minIdle", Integer.class)); dataSource.setDefaultAutoCommit(env.getRequiredProperty("jdbc.defaultAutoCommit", Boolean.class)); dataSource.setPoolPreparedStatements(env.getRequiredProperty("jdbc.poolPreparedStatements", Boolean.class)); dataSource.setValidationQuery(env.getRequiredProperty("jdbc.validationQuery")); dataSource.setTestOnBorrow(env.getRequiredProperty("jdbc.testOnBorrow", Boolean.class)); dataSource.setTestOnReturn(env.getRequiredProperty("jdbc.testOnReturn", Boolean.class)); dataSource.setTestWhileIdle(env.getRequiredProperty("jdbc.testWhileIdle", Boolean.class)); dataSource.setTimeBetweenEvictionRunsMillis( env.getRequiredProperty("jdbc.timeBetweenEvictionRunsMillis", Long.class)); dataSource.setNumTestsPerEvictionRun(env.getRequiredProperty("jdbc.numTestsPerEvictionRun", Integer.class)); dataSource.setMinEvictableIdleTimeMillis( env.getRequiredProperty("jdbc.minEvictableIdleTimeMillis", Long.class)); return dataSource; }
From source file:cn.cuizuoli.gotour.config.DataSourceConfig.java
@Bean public DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(env.getRequiredProperty("gotour.jdbc.driverClassName")); dataSource.setUrl(env.getRequiredProperty("gotour.jdbc.url")); dataSource.setUsername(env.getRequiredProperty("gotour.jdbc.username")); dataSource.setPassword(env.getRequiredProperty("gotour.jdbc.password")); dataSource.setInitialSize(env.getRequiredProperty("jdbc.initialSize", Integer.class)); dataSource.setMaxActive(env.getRequiredProperty("jdbc.maxActive", Integer.class)); dataSource.setMaxIdle(env.getRequiredProperty("jdbc.maxIdle", Integer.class)); dataSource.setMinIdle(env.getRequiredProperty("jdbc.minIdle", Integer.class)); dataSource.setDefaultAutoCommit(env.getRequiredProperty("jdbc.defaultAutoCommit", Boolean.class)); dataSource.setPoolPreparedStatements(env.getRequiredProperty("jdbc.poolPreparedStatements", Boolean.class)); dataSource.setValidationQuery(env.getRequiredProperty("jdbc.validationQuery")); dataSource.setTestOnBorrow(env.getRequiredProperty("jdbc.testOnBorrow", Boolean.class)); dataSource.setTestOnReturn(env.getRequiredProperty("jdbc.testOnReturn", Boolean.class)); dataSource.setTestWhileIdle(env.getRequiredProperty("jdbc.testWhileIdle", Boolean.class)); dataSource.setTimeBetweenEvictionRunsMillis( env.getRequiredProperty("jdbc.timeBetweenEvictionRunsMillis", Long.class)); dataSource.setNumTestsPerEvictionRun(env.getRequiredProperty("jdbc.numTestsPerEvictionRun", Integer.class)); dataSource.setMinEvictableIdleTimeMillis( env.getRequiredProperty("jdbc.minEvictableIdleTimeMillis", Long.class)); return dataSource; }
From source file:cn.cuizuoli.gotour.config.DataSourceConfig.java
@Bean public DataSource slaveDataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(env.getRequiredProperty("gotour.slave.jdbc.driverClassName")); dataSource.setUrl(env.getRequiredProperty("gotour.slave.jdbc.url")); dataSource.setUsername(env.getRequiredProperty("gotour.slave.jdbc.username")); dataSource.setPassword(env.getRequiredProperty("gotour.slave.jdbc.password")); dataSource.setInitialSize(env.getRequiredProperty("jdbc.initialSize", Integer.class)); dataSource.setMaxActive(env.getRequiredProperty("jdbc.maxActive", Integer.class)); dataSource.setMaxIdle(env.getRequiredProperty("jdbc.maxIdle", Integer.class)); dataSource.setMinIdle(env.getRequiredProperty("jdbc.minIdle", Integer.class)); dataSource.setDefaultAutoCommit(env.getRequiredProperty("jdbc.defaultAutoCommit", Boolean.class)); dataSource.setPoolPreparedStatements(env.getRequiredProperty("jdbc.poolPreparedStatements", Boolean.class)); dataSource.setValidationQuery(env.getRequiredProperty("jdbc.validationQuery")); dataSource.setTestOnBorrow(env.getRequiredProperty("jdbc.testOnBorrow", Boolean.class)); dataSource.setTestOnReturn(env.getRequiredProperty("jdbc.testOnReturn", Boolean.class)); dataSource.setTestWhileIdle(env.getRequiredProperty("jdbc.testWhileIdle", Boolean.class)); dataSource.setTimeBetweenEvictionRunsMillis( env.getRequiredProperty("jdbc.timeBetweenEvictionRunsMillis", Long.class)); dataSource.setNumTestsPerEvictionRun(env.getRequiredProperty("jdbc.numTestsPerEvictionRun", Integer.class)); dataSource.setMinEvictableIdleTimeMillis( env.getRequiredProperty("jdbc.minEvictableIdleTimeMillis", Long.class)); return dataSource; }
From source file:com.googlesource.gerrit.plugins.verifystatus.server.schema.CiDataSourceProvider.java
private DataSource open(Context context, CiDataSourceType dst) { // ConfigSection dbs = new ConfigSection(cfg, "database"); String driver = config.getString("driver"); if (Strings.isNullOrEmpty(driver)) { driver = dst.getDriver();/* ww w.j a va 2 s . com*/ } String url = config.getString("dbUrl"); if (Strings.isNullOrEmpty(url)) { url = dst.getUrl(); } String username = config.getString("username"); String password = config.getString("password"); String interceptor = config.getString("dataSourceInterceptorClass"); boolean usePool; if (context == Context.SINGLE_USER) { usePool = false; } else { usePool = config.getBoolean("connectionpool", dst.usePool()); } if (usePool) { final BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); if (username != null && !username.isEmpty()) { ds.setUsername(username); } if (password != null && !password.isEmpty()) { ds.setPassword(password); } ds.setMaxActive(config.getInt("poollimit", DEFAULT_POOL_LIMIT)); ds.setMinIdle(config.getInt("poolminidle", 4)); ds.setMaxIdle(config.getInt("poolmaxidle", 4)); String valueString = config.getString("poolmaxwait"); if (Strings.isNullOrEmpty(valueString)) { ds.setMaxWait(MILLISECONDS.convert(30, SECONDS)); } else { ds.setMaxWait(ConfigUtil.getTimeUnit(valueString, MILLISECONDS.convert(30, SECONDS), MILLISECONDS)); } ds.setInitialSize(ds.getMinIdle()); exportPoolMetrics(ds); return intercept(interceptor, ds); } // Don't use the connection pool. try { Properties p = new Properties(); p.setProperty("driver", driver); p.setProperty("url", url); if (username != null) { p.setProperty("user", username); } if (password != null) { p.setProperty("password", password); } return intercept(interceptor, new SimpleDataSource(p)); } catch (SQLException se) { throw new ProvisionException("Database unavailable", se); } }
From source file:com.googlesource.gerrit.plugins.ci.server.schema.CiDataSourceProvider.java
private DataSource open(Context context, CiDataSourceType dst) { //ConfigSection dbs = new ConfigSection(cfg, "database"); String driver = config.getString("driver"); if (Strings.isNullOrEmpty(driver)) { driver = dst.getDriver();/*from w w w. j a v a2 s . co m*/ } String url = config.getString("dbUrl"); if (Strings.isNullOrEmpty(url)) { url = dst.getUrl(); } String username = config.getString("username"); String password = config.getString("password"); String interceptor = config.getString("dataSourceInterceptorClass"); boolean usePool; if (context == Context.SINGLE_USER) { usePool = false; } else { usePool = config.getBoolean("connectionpool", dst.usePool()); } if (usePool) { final BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); if (username != null && !username.isEmpty()) { ds.setUsername(username); } if (password != null && !password.isEmpty()) { ds.setPassword(password); } ds.setMaxActive(config.getInt("poollimit", DEFAULT_POOL_LIMIT)); ds.setMinIdle(config.getInt("poolminidle", 4)); ds.setMaxIdle(config.getInt("poolmaxidle", 4)); String valueString = config.getString("poolmaxwait"); if (Strings.isNullOrEmpty(valueString)) { ds.setMaxWait(MILLISECONDS.convert(30, SECONDS)); } else { ds.setMaxWait(ConfigUtil.getTimeUnit(valueString, MILLISECONDS.convert(30, SECONDS), MILLISECONDS)); } ds.setInitialSize(ds.getMinIdle()); exportPoolMetrics(ds); return intercept(interceptor, ds); } else { // Don't use the connection pool. // try { Properties p = new Properties(); p.setProperty("driver", driver); p.setProperty("url", url); if (username != null) { p.setProperty("user", username); } if (password != null) { p.setProperty("password", password); } return intercept(interceptor, new SimpleDataSource(p)); } catch (SQLException se) { throw new ProvisionException("Database unavailable", se); } } }
From source file:net.comze.framework.orm.datasource.DbcpDataSourceFactory.java
private BasicDataSource initialize(Properties properties) { ObjectUtils.notNull(properties, "properties"); BasicDataSource basicDataSource = new BasicDataSource(); basicDataSource.setDriverClassName(properties.getProperty(JDBC_DRIVER)); basicDataSource.setUrl(properties.getProperty(JDBC_URL)); basicDataSource.setUsername(properties.getProperty(JDBC_USERNAME)); basicDataSource.setPassword(properties.getProperty(JDBC_PASSWORD)); if (properties.containsKey(DBCP_INITIALSIZE)) { basicDataSource.setInitialSize(Integer.parseInt(properties.getProperty(DBCP_INITIALSIZE))); }/*from ww w . j av a 2s .c om*/ if (properties.containsKey(DBCP_MAXACTIVE)) { basicDataSource.setMaxActive(Integer.parseInt(properties.getProperty(DBCP_MAXACTIVE))); } if (properties.containsKey(DBCP_MAXIDLE)) { basicDataSource.setMaxIdle(Integer.parseInt(properties.getProperty(DBCP_MAXIDLE))); } if (properties.containsKey(DBCP_MAXWAIT)) { basicDataSource.setMaxWait(Long.parseLong(properties.getProperty(DBCP_MAXWAIT))); } if (properties.containsKey(DBCP_MINEVICTABLEIDLETIMEMILLIS)) { basicDataSource.setMinEvictableIdleTimeMillis( Long.parseLong(properties.getProperty(DBCP_MINEVICTABLEIDLETIMEMILLIS))); } if (properties.containsKey(DBCP_MINIDLE)) { basicDataSource.setMinIdle(Integer.parseInt(properties.getProperty(DBCP_MINIDLE))); } if (properties.containsKey(DBCP_NUMTESTSPEREVICTIONRUN)) { basicDataSource.setNumTestsPerEvictionRun( Integer.parseInt(properties.getProperty(DBCP_NUMTESTSPEREVICTIONRUN))); } if (properties.containsKey(DBCP_REMOVEABANDONED)) { basicDataSource.setRemoveAbandoned(Boolean.parseBoolean(properties.getProperty(DBCP_REMOVEABANDONED))); } if (properties.containsKey(DBCP_REMOVEABANDONEDTIMEOUT)) { basicDataSource.setRemoveAbandonedTimeout( Integer.parseInt(properties.getProperty(DBCP_REMOVEABANDONEDTIMEOUT))); } if (properties.containsKey(DBCP_TESTONBORROW)) { basicDataSource.setTestOnBorrow(Boolean.parseBoolean(properties.getProperty(DBCP_TESTONBORROW))); } if (properties.containsKey(DBCP_TESTONCREATE)) { basicDataSource.setTestOnReturn(Boolean.parseBoolean(properties.getProperty(DBCP_TESTONCREATE))); } if (properties.containsKey(DBCP_TESTONRETURN)) { basicDataSource.setTestOnReturn(Boolean.parseBoolean(properties.getProperty(DBCP_TESTONRETURN))); } if (properties.containsKey(DBCP_TESTWHILEIDLE)) { basicDataSource.setTestWhileIdle(Boolean.parseBoolean(properties.getProperty(DBCP_TESTWHILEIDLE))); } if (properties.containsKey(DBCP_TIMEBETWEENEVICTIONRUNSMILLIS)) { basicDataSource.setTimeBetweenEvictionRunsMillis( Long.parseLong(properties.getProperty(DBCP_TIMEBETWEENEVICTIONRUNSMILLIS))); } if (properties.containsKey(DBCP_VALIDATIONQUERY)) { basicDataSource.setValidationQuery(properties.getProperty(DBCP_VALIDATIONQUERY)); } if (properties.containsKey(DBCP_VALIDATIONQUERYTIMEOUT)) { basicDataSource.setValidationQueryTimeout( Integer.parseInt(properties.getProperty(DBCP_VALIDATIONQUERYTIMEOUT))); } return basicDataSource; }
From source file:com.jolbox.benchmark.BenchmarkTests.java
/** * // ww w .j a va 2 s. com * * @param doPreparedStatement * @return time taken * @throws PropertyVetoException * @throws InterruptedException * @throws SQLException */ private DataSource multiThreadedDBCP(boolean doPreparedStatement) throws PropertyVetoException, InterruptedException, SQLException { BasicDataSource cpds = new BasicDataSource(); cpds.setDriverClassName("com.jolbox.bonecp.MockJDBCDriver"); cpds.setUrl(url); cpds.setUsername(username); cpds.setPassword(password); cpds.setMaxIdle(-1); cpds.setMinIdle(-1); if (doPreparedStatement) { cpds.setPoolPreparedStatements(true); cpds.setMaxOpenPreparedStatements(max_statement); } cpds.setInitialSize(pool_size); cpds.setMaxActive(pool_size); return cpds; }