Java Code Examples for com.alibaba.druid.pool.DruidDataSource#setTestOnReturn()
The following examples show how to use
com.alibaba.druid.pool.DruidDataSource#setTestOnReturn() .
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: DataSourceConfig.java From springboot-learning-experience with Apache License 2.0 | 6 votes |
/** * 手动创建DruidDataSource,通过DataSourceProperties 读取配置 * @return * @throws SQLException */ @Bean(name = "slaveDataSource") @Qualifier("slaveDataSource") @ConfigurationProperties(prefix = "spring.datasource.slave") public DataSource slaveDataSource() throws SQLException { DruidDataSource dataSource = new DruidDataSource(); dataSource.setFilters(filters); dataSource.setUrl(properties.getUrl()); dataSource.setDriverClassName(properties.getDriverClassName()); dataSource.setUsername(properties.getUsername()); dataSource.setPassword(properties.getPassword()); 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); return dataSource; }
Example 2
Source File: DruidDataSourceFactoryBean.java From cloud-config with MIT License | 6 votes |
private DruidDataSource createNewDataSource() throws Exception { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(config.getDriverClassName()); //loads the jdbc driver druidDataSource.setUrl(config.getJdbcUrl()); druidDataSource.setUsername(config.getUserName()); druidDataSource.setPassword(config.getPassword()); druidDataSource.setInitialSize(config.getInitialSize()); druidDataSource.setMaxActive(config.getMaxActive()); druidDataSource.setMaxIdle(config.getMaxIdle()); druidDataSource.setMaxWait(config.getMaxWait()); druidDataSource.setFilters(config.getFilters()); druidDataSource.setTimeBetweenEvictionRunsMillis(config.getTimeBetweenEvictionRunsMillis()); druidDataSource.setMinEvictableIdleTimeMillis(config.getMinEvictableIdleTimeMillis()); druidDataSource.setValidationQuery(config.getValidationQuery()); druidDataSource.setTestWhileIdle(config.isTestWhileIdle()); druidDataSource.setTestOnBorrow(config.isTestOnBorrow()); druidDataSource.setTestOnReturn(config.isTestOnReturn()); druidDataSource.setPoolPreparedStatements(config.isPoolPreparedStatements()); druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(config.getMaxPoolPreparedStatementPerConnectionSize()); return druidDataSource; }
Example 3
Source File: DruidDataSourceConfig.java From code with Apache License 2.0 | 6 votes |
@Bean public DataSource dataSource() throws SQLException { DruidDataSource ds = new DruidDataSource(); ds.setDriverClassName(druidSettings.getDriverClassName()); DRIVER_CLASSNAME = druidSettings.getDriverClassName(); ds.setUrl(druidSettings.getUrl()); ds.setUsername(druidSettings.getUsername()); ds.setPassword(druidSettings.getPassword()); ds.setInitialSize(druidSettings.getInitialSize()); ds.setMinIdle(druidSettings.getMinIdle()); ds.setMaxActive(druidSettings.getMaxActive()); ds.setTimeBetweenEvictionRunsMillis(druidSettings.getTimeBetweenEvictionRunsMillis()); ds.setMinEvictableIdleTimeMillis(druidSettings.getMinEvictableIdleTimeMillis()); ds.setValidationQuery(druidSettings.getValidationQuery()); ds.setTestWhileIdle(druidSettings.isTestWhileIdle()); ds.setTestOnBorrow(druidSettings.isTestOnBorrow()); ds.setTestOnReturn(druidSettings.isTestOnReturn()); ds.setPoolPreparedStatements(druidSettings.isPoolPreparedStatements()); ds.setMaxPoolPreparedStatementPerConnectionSize(druidSettings.getMaxPoolPreparedStatementPerConnectionSize()); ds.setFilters(druidSettings.getFilters()); ds.setConnectionProperties(druidSettings.getConnectionProperties()); logger.info(" druid datasource config : {} ", ds); return ds; }
Example 4
Source File: DynamicDataSourceFactory.java From spring-boot-study with MIT License | 5 votes |
/** * 通过自定义建立 Druid的数据源 * */ public static DruidDataSource buildDruidDataSource(DataSourceProperties properties) { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(properties.getDriverClassName()); druidDataSource.setUrl(properties.getUrl()); druidDataSource.setUsername(properties.getUsername()); druidDataSource.setPassword(properties.getPassword()); druidDataSource.setInitialSize(properties.getInitialSize()); druidDataSource.setMaxActive(properties.getMaxActive()); druidDataSource.setMinIdle(properties.getMinIdle()); druidDataSource.setMaxWait(properties.getMaxWait()); druidDataSource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis()); druidDataSource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis()); druidDataSource.setMaxEvictableIdleTimeMillis(properties.getMaxEvictableIdleTimeMillis()); druidDataSource.setValidationQuery(properties.getValidationQuery()); druidDataSource.setValidationQueryTimeout(properties.getValidationQueryTimeout()); druidDataSource.setTestOnBorrow(properties.isTestOnBorrow()); druidDataSource.setTestOnReturn(properties.isTestOnReturn()); druidDataSource.setPoolPreparedStatements(properties.isPoolPreparedStatements()); druidDataSource.setMaxOpenPreparedStatements(properties.getMaxOpenPreparedStatements()); druidDataSource.setSharePreparedStatements(properties.isSharePreparedStatements()); try { druidDataSource.setFilters(properties.getFilters()); druidDataSource.init(); } catch (SQLException e) { e.printStackTrace(); } return druidDataSource; }
Example 5
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 6
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 7
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 8
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 9
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 10
Source File: DataSourceAutoConfiguration.java From super-cloudops with Apache License 2.0 | 5 votes |
@Bean @RefreshScope @ConditionalOnMissingBean public DruidDataSource dataSource(DruidProperties prop) { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(prop.getUrl()); datasource.setUsername(prop.getUsername()); String plain = prop.getPassword(); if (valueOf(env.getProperty("spring.profiles.active")).startsWith("pro")) { try { // TODO using dynamic cipherKey?? byte[] cipherKey = AES128ECBPKCS5.getEnvCipherKey("DEVOPS_CIPHER_KEY"); plain = new AES128ECBPKCS5().decrypt(cipherKey, CodecSource.fromHex(prop.getPassword())).toString(); } catch (Throwable th) { throw new IllegalStateException(format("Unable to decryption database password for '%s'", prop.getPassword()), th); } } datasource.setPassword(plain); datasource.setDriverClassName(prop.getDriverClassName()); datasource.setInitialSize(prop.getInitialSize()); datasource.setMinIdle(prop.getMinIdle()); datasource.setMaxActive(prop.getMaxActive()); datasource.setMaxWait(prop.getMaxWait()); datasource.setTimeBetweenEvictionRunsMillis(prop.getTimeBetweenEvictionRunsMillis()); datasource.setMinEvictableIdleTimeMillis(prop.getMinEvictableIdleTimeMillis()); datasource.setValidationQuery(prop.getValidationQuery()); datasource.setTestWhileIdle(prop.isTestWhileIdle()); datasource.setTestOnBorrow(prop.isTestOnBorrow()); datasource.setTestOnReturn(prop.isTestOnReturn()); try { datasource.setFilters(prop.getFilters()); } catch (SQLException e) { log.error("druid configuration initialization filter", e); } return datasource; }
Example 11
Source File: DynamicDBUtil.java From jeecg-boot with Apache License 2.0 | 5 votes |
/** * 获取数据源【最底层方法,不要随便调用】 * * @param dbSource * @return */ private static DruidDataSource getJdbcDataSource(final DynamicDataSourceModel dbSource) { DruidDataSource dataSource = new DruidDataSource(); String driverClassName = dbSource.getDbDriver(); String url = dbSource.getDbUrl(); String dbUser = dbSource.getDbUsername(); String dbPassword = dbSource.getDbPassword(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(url); //dataSource.setValidationQuery("SELECT 1 FROM DUAL"); dataSource.setTestWhileIdle(true); dataSource.setTestOnBorrow(false); dataSource.setTestOnReturn(false); dataSource.setBreakAfterAcquireFailure(true); dataSource.setConnectionErrorRetryAttempts(0); dataSource.setUsername(dbUser); dataSource.setMaxWait(60000); dataSource.setPassword(dbPassword); log.info("******************************************"); log.info("* *"); log.info("*====【"+dbSource.getCode()+"】=====Druid连接池已启用 ====*"); log.info("* *"); log.info("******************************************"); return dataSource; }
Example 12
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 13
Source File: DruidProperties.java From MeetingFilm with Apache License 2.0 | 5 votes |
public void config(DruidDataSource dataSource) { 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); // 打开PSCache,并且指定每个连接上PSCache的大小 dataSource.setPoolPreparedStatements(poolPreparedStatements); dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize); try { dataSource.setFilters(filters); } catch (SQLException e) { e.printStackTrace(); } }
Example 14
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 15
Source File: DruidProperties.java From feiqu-opensource with Apache License 2.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 16
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 17
Source File: DruidDataSourceConfig.java From spring-boot-shiro with Apache License 2.0 | 5 votes |
@Bean @Primary //在同样的DataSource中,首先使用被标注的DataSource public DataSource getDataSource() { 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配置初始化失败", e); } datasource.setConnectionProperties(connectionProperties); return datasource; }
Example 18
Source File: DruidDataSourceFactory.java From jboot with Apache License 2.0 | 5 votes |
@Override public DataSource createDataSource(DataSourceConfig config) { DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setUrl(config.getUrl()); druidDataSource.setUsername(config.getUser()); druidDataSource.setPassword(config.getPassword()); druidDataSource.setDriverClassName(config.getDriverClassName()); druidDataSource.setMaxActive(config.getMaximumPoolSize()); druidDataSource.setMaxWait(config.getMaxWait()); druidDataSource.setTimeBetweenEvictionRunsMillis(config.getTimeBetweenEvictionRunsMillis()); druidDataSource.setMinEvictableIdleTimeMillis(config.getMinEvictableIdleTimeMillis()); druidDataSource.setTimeBetweenConnectErrorMillis(config.getTimeBetweenConnectErrorMillis()); druidDataSource.setValidationQuery(config.getValidationQuery()); druidDataSource.setTestWhileIdle(config.isTestWhileIdle()); druidDataSource.setTestOnBorrow(config.isTestOnBorrow()); druidDataSource.setTestOnReturn(config.isTestOnReturn()); if (config.getMinimumIdle() != null) { druidDataSource.setMinIdle(config.getMinimumIdle()); } if (config.getConnectionInitSql() != null) { druidDataSource.setConnectionInitSqls(Sets.newHashSet(config.getConnectionInitSql())); } try { druidDataSource.setFilters("stat"); } catch (SQLException e) { log.error("DruidDataSourceFactory is error", e); } return druidDataSource; }
Example 19
Source File: DataSourceUtil.java From framework with Apache License 2.0 | 4 votes |
private static DataSource regist(final String name, final DbParam dbParam) { String uid = new StringBuilder().append(dbParam.getUsername()).append(dbParam.getPassword()) .append(dbParam.getUrl()).toString(); dataSourceMap.put(name, uid); DataSource dataSource = dsMap.get(uid); if (dataSource == null) { DruidDataSource ds = new DruidDataSource(); ds.setDbType(dbParam.getDbType()); ds.setUrl(dbParam.getUrl()); if (StringUtils.isNotEmpty(dbParam.getUsername())) { ds.setUsername(dbParam.getUsername()); ds.setPassword(dbParam.getPassword()); } if (StringUtils.isNotEmpty(dbParam.getDriverClass())) { ds.setDriverClassName(dbParam.getDriverClass()); } else { ServiceLoader<Driver> drivers = ServiceLoader.load(Driver.class); if (drivers != null) { String dbType = new StringBuilder().append('.').append(dbParam.getDbType().toLowerCase()) .append('.').toString(); for (Driver driver : drivers) { if (driver.getClass().getName().indexOf(dbType) != -1) { ds.setDriverClassName(driver.getClass().getName()); break; } } } } ds.setInitialSize(dbParam.getInitialSize()); ds.setMaxActive(dbParam.getMaxActive()); ds.setMinIdle(dbParam.getMinIdle()); ds.setMaxWait(dbParam.getMaxWait()); ds.setValidationQuery(dbParam.getValidationQuery()); ds.setTestOnBorrow(dbParam.isTestOnBorrow()); ds.setTestOnReturn(dbParam.isTestOnReturn()); ds.setTestWhileIdle(dbParam.isTestWhileIdle()); ds.setTimeBetweenEvictionRunsMillis(dbParam.getTimeBetweenEvictionRunsMillis()); ds.setMinEvictableIdleTimeMillis(dbParam.getMinEvictableIdleTimeMillis()); ds.setRemoveAbandonedTimeout(dbParam.getRemoveAbandonedTimeout()); ds.setRemoveAbandoned(dbParam.isRemoveAbandoned()); ds.setProxyFilters(Arrays.asList(new SqlLogFilter())); try { ds.setFilters(dbParam.getFilters()); ds.init(); dsMap.put(uid, ds); dataSource = ds; } catch (SQLException e) { LoggerUtil.error(e); dataSourceMap.remove(name); if (ds != null) { ds.close(); } throw new InitializationException(ErrorCodeDef.DB_DATASOURCE_NOT_SET, e, name); } } return dataSource; }
Example 20
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; }