Java Code Examples for org.apache.commons.dbcp2.BasicDataSource#setTestWhileIdle()
The following examples show how to use
org.apache.commons.dbcp2.BasicDataSource#setTestWhileIdle() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResourceFactory.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
private static void external_dbcp2() throws Exception { for (ExternalDataSource ds : Config.externalDataSources()) { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(ds.getDriverClassName()); dataSource.setUrl(ds.getUrl()); dataSource.setInitialSize(0); dataSource.setMinIdle(0); dataSource.setMaxTotal(ds.getMaxTotal()); dataSource.setMaxIdle(ds.getMaxTotal()); dataSource.setTestOnCreate(false); dataSource.setTestWhileIdle(false); dataSource.setTestOnReturn(false); dataSource.setTestOnBorrow(false); dataSource.setUsername(ds.getUsername()); dataSource.setPassword(ds.getPassword()); String name = Config.externalDataSources().name(ds); new Resource(Config.RESOURCE_JDBC_PREFIX + name, dataSource); } }
Example 2
Source File: ResourceFactory.java From o2oa with GNU Affero General Public License v3.0 | 6 votes |
private static void internal_dbcp2() throws Exception { for (Entry<String, DataServer> entry : Config.nodes().dataServers().entrySet()) { BasicDataSource dataSource = new BasicDataSource(); String url = "jdbc:h2:tcp://" + entry.getKey() + ":" + entry.getValue().getTcpPort() + "/X;JMX=" + (entry.getValue().getJmxEnable() ? "TRUE" : "FALSE") + ";CACHE_SIZE=" + (entry.getValue().getCacheSize() * 1024); dataSource.setDriverClassName(SlicePropertiesBuilder.driver_h2); dataSource.setUrl(url); dataSource.setInitialSize(0); dataSource.setMinIdle(0); dataSource.setMaxTotal(50); dataSource.setMaxIdle(50); dataSource.setUsername("sa"); dataSource.setTestOnCreate(false); dataSource.setTestWhileIdle(false); dataSource.setTestOnReturn(false); dataSource.setTestOnBorrow(false); dataSource.setPassword(Config.token().getPassword()); String name = Config.nodes().dataServers().name(entry.getValue()); new Resource(Config.RESOURCE_JDBC_PREFIX + name, dataSource); } }
Example 3
Source File: DataSourceManager.java From Knowage-Server with GNU Affero General Public License v3.0 | 6 votes |
private static void createPoolIfAbsent(IDataSource dataSource) { Assert.assertNotNull(dataSource, "Missing input datasource"); Assert.assertNotNull(dataSource.getJdbcPoolConfiguration(), "Connection pool information is not provided"); logger.debug("Creating connection pool for datasource " + dataSource.getLabel()); BasicDataSource pool = new BasicDataSource(); pool.setDriverClassName(dataSource.getDriver()); pool.setUrl(dataSource.getUrlConnection()); pool.setUsername(dataSource.getUser()); pool.setPassword(dataSource.getPwd()); pool.setMaxTotal(dataSource.getJdbcPoolConfiguration().getMaxTotal()); pool.setMaxWaitMillis(dataSource.getJdbcPoolConfiguration().getMaxWait()); pool.setRemoveAbandonedOnBorrow(dataSource.getJdbcPoolConfiguration().getRemoveAbandonedOnBorrow()); pool.setRemoveAbandonedOnMaintenance(dataSource.getJdbcPoolConfiguration().getRemoveAbandonedOnMaintenance()); pool.setRemoveAbandonedTimeout(dataSource.getJdbcPoolConfiguration().getAbandonedTimeout()); pool.setLogAbandoned(dataSource.getJdbcPoolConfiguration().getLogAbandoned()); pool.setTestOnReturn(dataSource.getJdbcPoolConfiguration().getTestOnReturn()); pool.setTestWhileIdle(dataSource.getJdbcPoolConfiguration().getTestWhileIdle()); pool.setTimeBetweenEvictionRunsMillis(dataSource.getJdbcPoolConfiguration().getTimeBetweenEvictionRuns()); pool.setMinEvictableIdleTimeMillis(dataSource.getJdbcPoolConfiguration().getMinEvictableIdleTimeMillis()); pool.setValidationQuery(dataSource.getJdbcPoolConfiguration().getValidationQuery()); dataSources.put(dataSource, pool); }
Example 4
Source File: DBCPDataSourceBuilder.java From micro-server with Apache License 2.0 | 5 votes |
private void retrySetup(BasicDataSource ds) { if (!"org.hibernate.dialect.HSQLDialect".equals(mainEnv.getDialect())) { ds.setTestOnBorrow(dbcpEnv.isTestOnBorrow()); ds.setValidationQuery(dbcpEnv.getValidationQuery()); ds.setMaxTotal(dbcpEnv.getMaxTotal()); ds.setMinEvictableIdleTimeMillis(dbcpEnv.getMinEvictableIdleTime()); ds.setTimeBetweenEvictionRunsMillis(dbcpEnv.getTimeBetweenEvictionRuns()); ds.setNumTestsPerEvictionRun(dbcpEnv.getNumTestsPerEvictionRun()); ds.setTestWhileIdle(dbcpEnv.isTestWhileIdle()); ds.setTestOnReturn(dbcpEnv.isTestOnReturn()); } }
Example 5
Source File: DataSourceContainer.java From DBus with Apache License 2.0 | 4 votes |
public boolean register(JdbcVo conf) { boolean isOk = true; try { BasicDataSource bds = new BasicDataSource(); bds.setDriverClassName(conf.getDriverClass()); bds.setUrl(conf.getUrl()); bds.setUsername(conf.getUserName()); bds.setPassword(conf.getPassword()); bds.setInitialSize(conf.getInitialSize()); bds.setMaxTotal(conf.getMaxActive()); bds.setMaxIdle(conf.getMaxIdle()); bds.setMinIdle(conf.getMinIdle()); // 设置等待获取连接的最长时间 // bds.setMaxWaitMillis(20 * 1000); // validQuery 只给test idle 使用,不给 test Borrow使用, 为什么? // 发现 oracle版本 insert心跳被block, 下面的link 有人说 可以通过设置TestOnBorrow=false 解决。 // https://stackoverflow.com/questions/4853732/blocking-on-dbcp-connection-pool-open-and-close-connnection-is-database-conne bds.setTestOnBorrow(true); bds.setTestWhileIdle(false); if (StringUtils.equalsIgnoreCase(Constants.CONFIG_DB_TYPE_ORA, conf.getType())) { bds.setValidationQuery("select 1 from dual"); bds.setValidationQueryTimeout(5); } else if (StringUtils.equalsIgnoreCase(Constants.CONFIG_DB_TYPE_MYSQL, conf.getType())) { bds.setValidationQuery("select 1"); bds.setValidationQueryTimeout(5); } /*bds.setTimeBetweenEvictionRunsMillis(1000 * 300); if (org.apache.commons.lang.StringUtils.equals(Constants.CONFIG_DB_TYPE_ORA, conf.getType())) { bds.setValidationQuery("select 1 from dual"); bds.setValidationQueryTimeout(1); } else if (org.apache.commons.lang.StringUtils.equals(Constants.CONFIG_DB_TYPE_MYSQL, conf.getType())) { bds.setValidationQuery("select 1"); bds.setValidationQueryTimeout(1); }*/ LoggerFactory.getLogger().info("create datasource key:" + conf.getKey() + " url:" + conf.getUrl()); cmap.put(conf.getKey(), bds); // 为了支持查询主库和被库的延时,一个ds需要同时建立同主库和被库的连接 if (conf instanceof DsVo) { DsVo ds = (DsVo) conf; if (StringUtils.isNotBlank(ds.getSlvaeUrl())) { BasicDataSource slaveBds = new BasicDataSource(); slaveBds.setDriverClassName(ds.getDriverClass()); slaveBds.setUrl(ds.getSlvaeUrl()); slaveBds.setUsername(ds.getUserName()); slaveBds.setPassword(ds.getPassword()); slaveBds.setInitialSize(ds.getInitialSize()); slaveBds.setMaxTotal(ds.getMaxActive()); slaveBds.setMaxIdle(ds.getMaxIdle()); slaveBds.setMinIdle(ds.getMinIdle()); slaveBds.setTestOnBorrow(true); slaveBds.setTestWhileIdle(false); if (StringUtils.equalsIgnoreCase(Constants.CONFIG_DB_TYPE_ORA, conf.getType())) { slaveBds.setValidationQuery("select 1 from dual"); slaveBds.setValidationQueryTimeout(5); } else if (StringUtils.equalsIgnoreCase(Constants.CONFIG_DB_TYPE_MYSQL, conf.getType())) { slaveBds.setValidationQuery("select 1"); slaveBds.setValidationQueryTimeout(5); } // 设置等待获取连接的最长时间 // slaveBds.setMaxWaitMillis(20 * 1000); String key = StringUtils.join(new String[]{ds.getKey(), "slave"}, "_"); LoggerFactory.getLogger().info("create datasource key:" + key + " url:" + ds.getSlvaeUrl()); cmap.put(key, slaveBds); } else { LoggerFactory.getLogger().warn("db container initDsPool key " + ds.getKey() + " of slave url is empty."); } } } catch (Exception e) { LoggerFactory.getLogger().error("[db container initDsPool key " + conf.getKey() + " datasource error!]", e); isOk = false; } return isOk; }
Example 6
Source File: Dbcp2DataSourcePool.java From Zebra with Apache License 2.0 | 4 votes |
@Override public DataSource build(DataSourceConfig config, boolean withDefaultValue) { BasicDataSource dbcp2DataSource = new BasicDataSource(); dbcp2DataSource.setUrl(config.getJdbcUrl()); dbcp2DataSource.setUsername(config.getUsername()); dbcp2DataSource.setPassword(config.getPassword()); dbcp2DataSource.setDriverClassName(StringUtils.isNotBlank(config.getDriverClass()) ? config.getDriverClass() : JdbcDriverClassHelper.getDriverClassNameByJdbcUrl(config.getJdbcUrl())); if (withDefaultValue) { dbcp2DataSource.setInitialSize(getIntProperty(config, "initialPoolSize", 5)); dbcp2DataSource.setMaxTotal(getIntProperty(config, "maxPoolSize", 30)); dbcp2DataSource.setMinIdle(getIntProperty(config, "minPoolSize", 5)); dbcp2DataSource.setMaxIdle(getIntProperty(config, "maxPoolSize", 20)); dbcp2DataSource.setMaxWaitMillis(getIntProperty(config, "checkoutTimeout", 1000)); dbcp2DataSource.setValidationQuery(getStringProperty(config, "preferredTestQuery", "SELECT 1")); dbcp2DataSource.setMinEvictableIdleTimeMillis(getIntProperty(config, "minEvictableIdleTimeMillis", 1800000));// 30min dbcp2DataSource .setTimeBetweenEvictionRunsMillis(getIntProperty(config, "timeBetweenEvictionRunsMillis", 30000)); // 30s dbcp2DataSource.setRemoveAbandonedTimeout(getIntProperty(config, "removeAbandonedTimeout", 300)); // 30s dbcp2DataSource.setNumTestsPerEvictionRun(getIntProperty(config, "numTestsPerEvictionRun", 6)); // 30s dbcp2DataSource.setValidationQueryTimeout(getIntProperty(config, "validationQueryTimeout", 0)); if (StringUtils.isNotBlank(getStringProperty(config, "connectionInitSql", null))) { List<String> initSqls = new ArrayList<String>(); initSqls.add(getStringProperty(config, "connectionInitSql", null)); dbcp2DataSource.setConnectionInitSqls(initSqls); } dbcp2DataSource.setTestWhileIdle(true); dbcp2DataSource.setTestOnBorrow(false); dbcp2DataSource.setTestOnReturn(false); dbcp2DataSource.setRemoveAbandonedOnBorrow(true); dbcp2DataSource.setRemoveAbandonedOnMaintenance(true); } else { try { PropertiesInit<BasicDataSource> propertiesInit = new PropertiesInit<BasicDataSource>(dbcp2DataSource); propertiesInit.initPoolProperties(config); } catch (Exception e) { throw new ZebraConfigException(String.format("dbcp2 dataSource [%s] created error : ", config.getId()), e); } } this.pool = dbcp2DataSource; LOGGER.info(String.format("New dataSource [%s] created.", config.getId())); return this.pool; }