Java Code Examples for com.alibaba.druid.pool.DruidDataSource#setTestWhileIdle()
The following examples show how to use
com.alibaba.druid.pool.DruidDataSource#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: DynamicDataSource.java From tcc-transaction with Apache License 2.0 | 6 votes |
private DataSource getDruidDataSource(DataSourceInfoDto dto) { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setUrl(dto.getDbUrl()); druidDataSource.setUsername(dto.getUsername()); druidDataSource.setPassword(getPassword(dto.getUsername(), dto.getPassword())); druidDataSource.setMaxActive(dto.getMaxActive()); druidDataSource.setInitialSize(dto.getInitialSize()); druidDataSource.setMaxWait(60000); druidDataSource.setTimeBetweenEvictionRunsMillis(300000); druidDataSource.setMinEvictableIdleTimeMillis(300000); druidDataSource.setValidationQuery("select 1"); druidDataSource.setTestWhileIdle(true); druidDataSource.setTestOnBorrow(false); druidDataSource.setTestOnReturn(false); druidDataSource.setPoolPreparedStatements(true); druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(50); try { druidDataSource.init(); } catch (SQLException e) { log.error("Can't init druidDataSource from DataSourceInfo:" + dto, e); } return druidDataSource; }
Example 2
Source File: MybatisConfig.java From java_server with MIT License | 6 votes |
private void druidSettings(DruidDataSource druidDataSource) throws Exception { druidDataSource.setMaxActive(1000); druidDataSource.setInitialSize(0); druidDataSource.setMinIdle(0); druidDataSource.setMaxWait(60000); druidDataSource.setPoolPreparedStatements(true); druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(100); druidDataSource.setTestOnBorrow(false); druidDataSource.setTestOnReturn(false); druidDataSource.setTestWhileIdle(true); druidDataSource.setTimeBetweenEvictionRunsMillis(6000); druidDataSource.setMinEvictableIdleTimeMillis(2520000); druidDataSource.setRemoveAbandoned(true); druidDataSource.setRemoveAbandonedTimeout(18000); druidDataSource.setLogAbandoned(true); druidDataSource.setFilters("mergeStat"); druidDataSource.setDriver(new Driver()); }
Example 3
Source File: DbConfig.java From sanshanblog with Apache License 2.0 | 5 votes |
@Bean(initMethod = "init", destroyMethod = "close",name = "datasource") public DruidDataSource dataSource() throws SQLException { DruidDataSource ds = new DruidDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(username); ds.setPassword(passowrd); // 最大并发连接数 ds.setMaxActive(maxActive); // 初始化连接数量 ds.setInitialSize(initialSize); // 配置获取连接等待超时的时间 ds.setMaxWait(maxWait); // 最小空闲连接数 ds.setMinIdle(minIdle); // 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 ds.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // 配置一个连接在池中最小生存的时间,单位是毫秒 ds.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); ds.setValidationQuery("SELECT 1"); ds.setTestWhileIdle(testWhileIdle); //开启mysql的自动事务 ds.setDefaultAutoCommit(true); ds.setTestOnBorrow(testOnBorrow); ds.setTestOnReturn(testOnReturn); ds.setMaxOpenPreparedStatements(maxOpenPreparedStatements); ds.setFilters(filters); // 打开removeAbandoned功能 ds.setRemoveAbandoned(removeAbandoned); // 1800秒,也就是30分钟 ds.setRemoveAbandonedTimeout(removeAbandonedTimeout); // 关闭abanded连接时输出错误日志 ds.setLogAbandoned(logAbandoned); return ds; }
Example 4
Source File: DruidDataSourceFactory.java From tsharding with MIT License | 5 votes |
@Override public DruidDataSource getDataSource(DataSourceConfig config) throws SQLException { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(config.getUrl()); dataSource.setUsername(config.getUsername()); dataSource.setPassword(config.getPassword()); // pool config dataSource.setInitialSize(config.getInitialPoolSize()); dataSource.setMinIdle(config.getMinPoolSize()); dataSource.setMaxActive(config.getMaxPoolSize()); // common config dataSource.setFilters("stat"); dataSource.setMaxWait(1000); dataSource.setValidationQuery("SELECT 'x'"); dataSource.setTestWhileIdle(true); dataSource.setTestOnBorrow(false); dataSource.setTestOnReturn(false); dataSource.setTimeBetweenEvictionRunsMillis(60000); dataSource.setMinEvictableIdleTimeMillis(120000); dataSource.setTimeBetweenLogStatsMillis(0); dataSource.setProxyFilters(filters); dataSource.init(); return dataSource; }
Example 5
Source File: DruidProperties.java From SpringBootBucket with MIT License | 5 votes |
public void config(DruidDataSource dataSource) { dataSource.setDbType(JdbcConstants.MYSQL); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setDriverClassName(driverClassName); dataSource.setInitialSize(initialSize); // 定义初始连接数 dataSource.setMinIdle(minIdle); // 最小空闲 dataSource.setMaxActive(maxActive); // 定义最大连接数 dataSource.setMaxWait(maxWait); // 获取连接等待超时的时间 dataSource.setRemoveAbandoned(removeAbandoned); // 超过时间限制是否回收 dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout); // 超过时间限制多长 // 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // 配置一个连接在池中最小生存的时间,单位是毫秒 dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); // 用来检测连接是否有效的sql,要求是一个查询语句 dataSource.setValidationQuery(validationQuery); // 申请连接的时候检测 dataSource.setTestWhileIdle(testWhileIdle); // 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 dataSource.setTestOnBorrow(testOnBorrow); // 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能 dataSource.setTestOnReturn(testOnReturn); // 打开PSCache,并且指定每个连接上PSCache的大小 dataSource.setPoolPreparedStatements(poolPreparedStatements); dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); // 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有: // 监控统计用的filter:stat // 日志用的filter:log4j // 防御SQL注入的filter:wall try { dataSource.setFilters(filters); } catch (SQLException e) { e.printStackTrace(); } }
Example 6
Source File: DruidConfiguration.java From elephant with Apache License 2.0 | 5 votes |
@Bean //声明其为Bean实例 @Primary //在同样的DataSource中,首先使用被标注的DataSource public DataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(url); datasource.setUsername(username); datasource.setPassword(password); datasource.setDriverClassName(driverClassName); //configuration datasource.setInitialSize(initialSize); datasource.setMinIdle(minIdle); datasource.setMaxActive(maxActive); datasource.setMaxWait(maxWait); datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); datasource.setValidationQuery(validationQuery); datasource.setTestWhileIdle(testWhileIdle); datasource.setTestOnBorrow(testOnBorrow); datasource.setTestOnReturn(testOnReturn); datasource.setPoolPreparedStatements(poolPreparedStatements); datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); try { datasource.setFilters(filters); } catch (SQLException e) { logger.error("druid configuration initialization filter: {}",e); } datasource.setConnectionProperties(connectionProperties); return datasource; }
Example 7
Source File: DruidConfig.java From sophia_scaffolding with Apache License 2.0 | 5 votes |
@Bean //声明其为Bean实例 @Primary //在同样的DataSource中,首先使用被标注的DataSource public DataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(url); datasource.setUsername(username); datasource.setPassword(password); datasource.setDriverClassName(driverClassName); //configuration datasource.setInitialSize(initialSize); datasource.setMinIdle(minIdle); datasource.setMaxActive(maxActive); datasource.setMaxWait(maxWait); datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); datasource.setValidationQuery(validationQuery); datasource.setTestWhileIdle(testWhileIdle); datasource.setTestOnBorrow(testOnBorrow); datasource.setTestOnReturn(testOnReturn); datasource.setPoolPreparedStatements(poolPreparedStatements); datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); try { datasource.setFilters(filters); } catch (SQLException e) { logger.error("druid configuration initialization filter: " + e); } datasource.setConnectionProperties(connectionProperties); logger.debug("druid configuration datasource 成功" + datasource); logger.debug("druid configuration datasource 成功" + name); return datasource; }
Example 8
Source File: DruidProperties.java From LuckyFrameWeb with GNU Affero General Public License v3.0 | 5 votes |
public DruidDataSource dataSource(DruidDataSource datasource) { // 配置初始化大小、最小、最大 datasource.setInitialSize(initialSize); datasource.setMaxActive(maxActive); datasource.setMinIdle(minIdle); //配置获取连接等待超时的时间 datasource.setMaxWait(maxWait); //配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // 配置一个连接在池中最小、最大生存的时间,单位是毫秒 datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); datasource.setMaxEvictableIdleTimeMillis(maxEvictableIdleTimeMillis); //用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。 datasource.setValidationQuery(validationQuery); // 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 datasource.setTestWhileIdle(testWhileIdle); // 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 datasource.setTestOnBorrow(testOnBorrow); //归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 datasource.setTestOnReturn(testOnReturn); return datasource; }
Example 9
Source File: DruidProperties.java From SpringBootBucket with MIT License | 5 votes |
public void config(DruidDataSource dataSource) { dataSource.setDbType(JdbcConstants.ORACLE); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setDriverClassName(driverClassName); dataSource.setInitialSize(initialSize); // 定义初始连接数 dataSource.setMinIdle(minIdle); // 最小空闲 dataSource.setMaxActive(maxActive); // 定义最大连接数 dataSource.setMaxWait(maxWait); // 获取连接等待超时的时间 dataSource.setRemoveAbandoned(removeAbandoned); // 超过时间限制是否回收 dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout); // 超过时间限制多长 // 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // 配置一个连接在池中最小生存的时间,单位是毫秒 dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); // 用来检测连接是否有效的sql,要求是一个查询语句 dataSource.setValidationQuery(validationQuery); // 申请连接的时候检测 dataSource.setTestWhileIdle(testWhileIdle); // 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 dataSource.setTestOnBorrow(testOnBorrow); // 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能 dataSource.setTestOnReturn(testOnReturn); // 打开PSCache,并且指定每个连接上PSCache的大小 dataSource.setPoolPreparedStatements(poolPreparedStatements); dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); // 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有: // 监控统计用的filter:stat // 日志用的filter:log4j // 防御SQL注入的filter:wall try { dataSource.setFilters(filters); } catch (SQLException e) { e.printStackTrace(); } }
Example 10
Source File: DruidProperties.java From ruoyiplus with MIT License | 5 votes |
public DruidDataSource dataSource(DruidDataSource datasource) { /** 配置初始化大小、最小、最大 */ datasource.setInitialSize(initialSize); datasource.setMaxActive(maxActive); datasource.setMinIdle(minIdle); /** 配置获取连接等待超时的时间 */ datasource.setMaxWait(maxWait); /** 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 */ datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); /** 配置一个连接在池中最小、最大生存的时间,单位是毫秒 */ datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); datasource.setMaxEvictableIdleTimeMillis(maxEvictableIdleTimeMillis); /** * 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。 */ datasource.setValidationQuery(validationQuery); /** 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 */ datasource.setTestWhileIdle(testWhileIdle); /** 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 */ datasource.setTestOnBorrow(testOnBorrow); /** 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 */ datasource.setTestOnReturn(testOnReturn); return datasource; }
Example 11
Source File: DruidProperties.java From RuoYi-Vue with MIT License | 5 votes |
public DruidDataSource dataSource(DruidDataSource datasource) { /** 配置初始化大小、最小、最大 */ datasource.setInitialSize(initialSize); datasource.setMaxActive(maxActive); datasource.setMinIdle(minIdle); /** 配置获取连接等待超时的时间 */ datasource.setMaxWait(maxWait); /** 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 */ datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); /** 配置一个连接在池中最小、最大生存的时间,单位是毫秒 */ datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); datasource.setMaxEvictableIdleTimeMillis(maxEvictableIdleTimeMillis); /** * 用来检测连接是否有效的sql,要求是一个查询语句,常用select 'x'。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用。 */ datasource.setValidationQuery(validationQuery); /** 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 */ datasource.setTestWhileIdle(testWhileIdle); /** 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 */ datasource.setTestOnBorrow(testOnBorrow); /** 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 */ datasource.setTestOnReturn(testOnReturn); return datasource; }
Example 12
Source File: ClusterDataSourceConfig.java From springBoot-study with Apache License 2.0 | 5 votes |
@Bean(name = "clusterDataSource") public DataSource clusterDataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setDriverClassName(driverClass); //具体配置 dataSource.setInitialSize(initialSize); dataSource.setMinIdle(minIdle); dataSource.setMaxActive(maxActive); dataSource.setMaxWait(maxWait); dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); dataSource.setValidationQuery(validationQuery); dataSource.setTestWhileIdle(testWhileIdle); dataSource.setTestOnBorrow(testOnBorrow); dataSource.setTestOnReturn(testOnReturn); dataSource.setPoolPreparedStatements(poolPreparedStatements); dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); try { dataSource.setFilters(filters); } catch (SQLException e) { e.printStackTrace(); } dataSource.setConnectionProperties(connectionProperties); return dataSource; }
Example 13
Source File: DruidProperties.java From SpringBootBucket with MIT License | 5 votes |
public void config(DruidDataSource dataSource) { dataSource.setDbType(JdbcConstants.MYSQL); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setDriverClassName(driverClassName); dataSource.setInitialSize(initialSize); // 定义初始连接数 dataSource.setMinIdle(minIdle); // 最小空闲 dataSource.setMaxActive(maxActive); // 定义最大连接数 dataSource.setMaxWait(maxWait); // 获取连接等待超时的时间 dataSource.setRemoveAbandoned(removeAbandoned); // 超过时间限制是否回收 dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout); // 超过时间限制多长 // 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // 配置一个连接在池中最小生存的时间,单位是毫秒 dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); // 用来检测连接是否有效的sql,要求是一个查询语句 dataSource.setValidationQuery(validationQuery); // 申请连接的时候检测 dataSource.setTestWhileIdle(testWhileIdle); // 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 dataSource.setTestOnBorrow(testOnBorrow); // 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能 dataSource.setTestOnReturn(testOnReturn); // 打开PSCache,并且指定每个连接上PSCache的大小 dataSource.setPoolPreparedStatements(poolPreparedStatements); dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); // 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有: // 监控统计用的filter:stat // 日志用的filter:log4j // 防御SQL注入的filter:wall try { dataSource.setFilters(filters); } catch (SQLException e) { e.printStackTrace(); } }
Example 14
Source File: MasterDataSourceConfig.java From springBoot-study with Apache License 2.0 | 5 votes |
@Bean(name = "masterDataSource") @Primary //标志这个 Bean 如果在多个同类 Bean 候选时,该 Bean 优先被考虑。 public DataSource masterDataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setDriverClassName(driverClassName); //具体配置 dataSource.setInitialSize(initialSize); dataSource.setMinIdle(minIdle); dataSource.setMaxActive(maxActive); dataSource.setMaxWait(maxWait); dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); dataSource.setValidationQuery(validationQuery); dataSource.setTestWhileIdle(testWhileIdle); dataSource.setTestOnBorrow(testOnBorrow); dataSource.setTestOnReturn(testOnReturn); dataSource.setPoolPreparedStatements(poolPreparedStatements); dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); try { dataSource.setFilters(filters); } catch (SQLException e) { e.printStackTrace(); } dataSource.setConnectionProperties(connectionProperties); return dataSource; }
Example 15
Source File: DruidDataSourceConfig.java From ssm-demo with MIT License | 5 votes |
@Bean //声明其为Bean实例 public DataSource dataSource(){ LOG.info("Initialize the data source..."); DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(this.dbUrl); datasource.setUsername(username); datasource.setPassword(password); datasource.setDriverClassName(driverClassName); //configuration datasource.setInitialSize(initialSize); datasource.setMinIdle(minIdle); datasource.setMaxActive(maxActive); datasource.setMaxWait(maxWait); datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); datasource.setValidationQuery(validationQuery); datasource.setTestWhileIdle(testWhileIdle); datasource.setTestOnBorrow(testOnBorrow); datasource.setTestOnReturn(testOnReturn); datasource.setPoolPreparedStatements(poolPreparedStatements); datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); try { datasource.setFilters(filters); } catch (SQLException e) { LOG.error("druid configuration initialization filter", e); } datasource.setConnectionProperties(connectionProperties); return datasource; }
Example 16
Source File: DruidConfig.java From Photo with GNU General Public License v3.0 | 5 votes |
@Bean // 声明其为Bean实例 @Primary // 在同样的DataSource中,首先使用被标注的DataSource public DataSource dataSource() throws SQLException { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(this.url); datasource.setUsername(username); datasource.setPassword(password); datasource.setDriverClassName(driverClassName); // configuration datasource.setInitialSize(initialSize); datasource.setMinIdle(minIdle); datasource.setMaxActive(maxActive); datasource.setMaxWait(maxWait); datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); datasource.setValidationQuery(validationQuery); datasource.setTestWhileIdle(testWhileIdle); datasource.setTestOnBorrow(testOnBorrow); datasource.setTestOnReturn(testOnReturn); datasource.setPoolPreparedStatements(poolPreparedStatements); datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); datasource.setFilters(filters); return datasource; }
Example 17
Source File: DruidProperties.java From SpringBootBucket with MIT License | 5 votes |
public void config(DruidDataSource dataSource) { dataSource.setDbType(JdbcConstants.MYSQL); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setDriverClassName(driverClassName); dataSource.setInitialSize(initialSize); // 定义初始连接数 dataSource.setMinIdle(minIdle); // 最小空闲 dataSource.setMaxActive(maxActive); // 定义最大连接数 dataSource.setMaxWait(maxWait); // 获取连接等待超时的时间 dataSource.setRemoveAbandoned(removeAbandoned); // 超过时间限制是否回收 dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout); // 超过时间限制多长 // 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // 配置一个连接在池中最小生存的时间,单位是毫秒 dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); // 用来检测连接是否有效的sql,要求是一个查询语句 dataSource.setValidationQuery(validationQuery); // 申请连接的时候检测 dataSource.setTestWhileIdle(testWhileIdle); // 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 dataSource.setTestOnBorrow(testOnBorrow); // 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能 dataSource.setTestOnReturn(testOnReturn); // 打开PSCache,并且指定每个连接上PSCache的大小 dataSource.setPoolPreparedStatements(poolPreparedStatements); dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); // 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有: // 监控统计用的filter:stat // 日志用的filter:log4j // 防御SQL注入的filter:wall try { dataSource.setFilters(filters); } catch (SQLException e) { e.printStackTrace(); } }
Example 18
Source File: ConnectionManager.java From es_data_export with Apache License 2.0 | 5 votes |
/** * 在构造函数初始化的时候获取数据库连接 * @throws Exception */ private ConnectionManager() throws Exception{ /** 获取属性文件中的值 **/ String jdbc_driver_library = Config.JDBC_CONFIG.getJdbc_driver_library(); String driverName = Config.JDBC_CONFIG.getJdbc_driver_class(); String url = Config.JDBC_CONFIG.getJdbc_connection_string(); String username = Config.JDBC_CONFIG.getJdbc_user(); String password = Config.JDBC_CONFIG.getJdbc_password(); /** 数据库连接池对象 **/ cpds = new DruidDataSource(); /** 设置数据库连接驱动 **/ //cpds.setDriverClassName(driverName); cpds.setDriver(DriverLoader.getDriverLoaderByName(jdbc_driver_library, driverName)); /** 设置数据库连接地址 **/ cpds.setUrl(url); /** 设置数据库连接用户名 **/ cpds.setUsername(username); /** 设置数据库连接密码 **/ cpds.setPassword(password); /** 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 **/ cpds.setTestWhileIdle(true); /*******最大连接池数量********/ cpds.setMaxActive(30); /*******初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时********/ cpds.setInitialSize(3); /** 超过30分钟开始关闭空闲连接 **/ cpds.setRemoveAbandonedTimeout(1800); /** 最大等待时间为60S 单位是毫秒**/ cpds.setMaxWait(60000); /**配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒**/ cpds.setTimeBetweenEvictionRunsMillis(30000); }
Example 19
Source File: DruidProperties.java From spring-boot-starter-dao with Apache License 2.0 | 4 votes |
public DruidDataSource createDataSource() throws SQLException { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setDriverClassName(driverClassName); dataSource.setConnectProperties(connectProperties); dataSource.setInitialSize(initialSize); dataSource.setMinIdle(minIdle); dataSource.setMaxActive(maxActive); dataSource.setMaxWait(maxWait); dataSource.setFilters(filters); dataSource.setDefaultAutoCommit(defaultAutoCommit); dataSource.setTimeBetweenConnectErrorMillis(timeBetweenConnectErrorMillis); dataSource.setValidationQuery(validationQuery); dataSource.setValidationQueryTimeout(validationQueryTimeout); dataSource.setTestWhileIdle(testWhileIdle); dataSource.setTestOnBorrow(testOnBorrow); dataSource.setTestOnReturn(testOnReturn); dataSource.setPoolPreparedStatements(poolPreparedStatements); dataSource.setClearFiltersEnable(clearFiltersEnable); dataSource.setDefaultReadOnly(defaultReadOnly); dataSource.setAsyncCloseConnectionEnable(asyncCloseConnectionEnable); dataSource.setConnectionErrorRetryAttempts(connectionErrorRetryAttempts); dataSource.setBreakAfterAcquireFailure(breakAfterAcquireFailure); dataSource.setDupCloseLogEnable(dupCloseLogEnable); dataSource.setEnable(enable); dataSource.setLogAbandoned(logAbandoned); dataSource.setLogDifferentThread(logDifferentThread); dataSource.setLoginTimeout(loginTimeout); dataSource.setAccessToUnderlyingConnectionAllowed(accessToUnderlyingConnectionAllowed); dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); dataSource.setQueryTimeout(queryTimeout); dataSource.setFailFast(failFast); dataSource.setMaxCreateTaskCount(maxCreateTaskCount); dataSource.setRemoveAbandoned(removeAbandoned); dataSource.setRemoveAbandonedTimeoutMillis(removeAbandonedTimeoutMillis); dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation); dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); dataSource.setMaxEvictableIdleTimeMillis(maxEvictableIdleTimeMillis); dataSource.setMaxOpenPreparedStatements(maxOpenPreparedStatements); dataSource.setNotFullTimeoutRetryCount(notFullTimeoutRetryCount); dataSource.setTimeBetweenLogStatsMillis(timeBetweenLogStatsMillis); return dataSource; }
Example 20
Source File: DataSourceIT.java From apm-agent-java with Apache License 2.0 | 4 votes |
private static DataSource getDruidDataSource() { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setUrl(URL); druidDataSource.setTestWhileIdle(false); return druidDataSource; }