Java Code Examples for com.alibaba.druid.pool.DruidDataSource#setRemoveAbandoned()

The following examples show how to use com.alibaba.druid.pool.DruidDataSource#setRemoveAbandoned() . 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: DataSourceConfiguration.java    From mywx with Apache License 2.0 6 votes vote down vote up
/**
 * 初始化数据源
 *
 * @return
 * @throws SQLException
 */

@Bean(destroyMethod = "close")
public DruidDataSource druidDataSource(DataSourceProperties dataSourceProperties) throws SQLException {
    DruidDataSource source = new DruidDataSource();
    source.setUsername(dataSourceProperties.getUserName());
    source.setPassword(dataSourceProperties.getPassWord());
    source.setUrl(dataSourceProperties.getUrl());
    source.setDriverClassName(dataSourceProperties.getDriverClassName());
    source.setFilters(dataSourceProperties.getFilters());
    source.setMaxActive(dataSourceProperties.getMaxActive());
    source.setInitialSize(dataSourceProperties.getInitialSize());
    source.setMinIdle(dataSourceProperties.getMinIdle());
    source.setTimeBetweenEvictionRunsMillis(dataSourceProperties.getTimeBetweenEvictionRunsMillis());
    source.setMinEvictableIdleTimeMillis(dataSourceProperties.getMinEvictableIdleTimeMillis());
    source.setValidationQuery(dataSourceProperties.getValidationQuery());
    source.setTestWhileIdle(dataSourceProperties.isTestWhileIdle());
    source.setTestOnReturn(dataSourceProperties.isTestOnReturn());
    source.setTestOnBorrow(dataSourceProperties.isTestOnBorrow());
    source.setMaxOpenPreparedStatements(dataSourceProperties.getMaxOpenPreparedStatements());
    source.setRemoveAbandoned(dataSourceProperties.isRemoveAbandoned());
    source.setRemoveAbandonedTimeout(dataSourceProperties.getRemoveAbandonedTimeout());
    source.setLogAbandoned(dataSourceProperties.isLogAbandoned());
    return source;
}
 
Example 2
Source File: MybatisConfig.java    From java_server with MIT License 6 votes vote down vote up
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: MySQLDruidDataSourceConfiguration.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
@Bean
    @Primary
    public DataSource dataSource() {
        // 数据库驱动配置信息
        DruidDataSource datasource = new DruidDataSource();
        datasource.setDbType(properties.getType());
        datasource.setUrl(properties.getUrl());
        datasource.setUsername(properties.getUsername());
        datasource.setPassword(properties.getPassword());
        datasource.setDriverClassName(properties.getDriver());

        // 数据库连接池配置信息
        datasource.setMaxWait(properties.getMaxWait());
//        datasource.setInitialSize(properties.getInitialSize());  //配置后, junit将不能执行
        datasource.setMinIdle(properties.getMinIdle());
        datasource.setMaxActive(properties.getMaxActive());
        datasource.setMaxWait(properties.getMaxWait());
        datasource.setTimeBetweenEvictionRunsMillis(properties.getTimeBetweenEvictionRunsMillis());
        datasource.setMinEvictableIdleTimeMillis(properties.getMinEvictableIdleTimeMillis());
        datasource.setMaxEvictableIdleTimeMillis(properties.getMaxEvictableIdleTimeMillis());
        datasource.setRemoveAbandoned(properties.isRemoveAbandoned());
        datasource.setRemoveAbandonedTimeoutMillis(properties.getRemoveAbandonedTimeoutMillis());
        datasource.setTimeBetweenConnectErrorMillis(properties.getTimeBetweenConnectErrorMillis());
        datasource.setValidationQuery(properties.getValidationQuery());
        datasource.setTestWhileIdle(properties.isTestWhileIdle());
        datasource.setTestOnBorrow(properties.isTestOnBorrow());
        datasource.setTestOnReturn(properties.isTestOnReturn());
        datasource.setPoolPreparedStatements(properties.isPoolPreparedStatements());
        datasource.setMaxOpenPreparedStatements(properties.getMaxOpenPreparedStatements());
        datasource.setMaxPoolPreparedStatementPerConnectionSize(properties.getMaxPoolPreparedStatementPerConnectionSize());
        datasource.setLogAbandoned(properties.isLogAbandoned());
        try {
            datasource.setFilters(properties.getFilters());
        } catch (SQLException e) {
            log.error("Druid setting filters exception: " + e.getMessage(), e);
        }
        datasource.setConnectionProperties(properties.getConnectionProperties());
        log.info("\n*** Initialize MySQL Druid datasource successful." + properties.getUrl());
        return datasource;
    }
 
Example 4
Source File: DruidDataSourcePool.java    From EasyReport with Apache License 2.0 5 votes vote down vote up
@Override
public DataSource wrap(final ReportDataSource rptDs) {
    try {
        final DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(rptDs.getDriverClass());
        dataSource.setUrl(rptDs.getJdbcUrl());
        dataSource.setUsername(rptDs.getUser());
        dataSource.setPassword(rptDs.getPassword());
        dataSource.setInitialSize(MapUtils.getInteger(rptDs.getOptions(), "initialSize", 3));
        dataSource.setMaxActive(MapUtils.getInteger(rptDs.getOptions(), "maxActive", 20));
        dataSource.setMinIdle(MapUtils.getInteger(rptDs.getOptions(), "minIdle", 1));
        dataSource.setMaxWait(MapUtils.getInteger(rptDs.getOptions(), "maxWait", 60000));
        dataSource.setTimeBetweenEvictionRunsMillis(
            MapUtils.getInteger(rptDs.getOptions(), "timeBetweenEvictionRunsMillis", 60000));
        dataSource.setMinEvictableIdleTimeMillis(
            MapUtils.getInteger(rptDs.getOptions(), "minEvictableIdleTimeMillis", 300000));
        dataSource.setValidationQuery("select 1");
        dataSource.setTestWhileIdle(MapUtils.getBoolean(rptDs.getOptions(), "testWhileIdle", true));
        dataSource.setTestOnBorrow(MapUtils.getBoolean(rptDs.getOptions(), "testOnBorrow", false));
        dataSource.setTestOnReturn(MapUtils.getBoolean(rptDs.getOptions(), "testOnReturn", false));
        dataSource.setMaxOpenPreparedStatements(
            MapUtils.getInteger(rptDs.getOptions(), "maxOpenPreparedStatements", 20));
        dataSource.setRemoveAbandoned(MapUtils.getBoolean(rptDs.getOptions(), "removeAbandoned", true));
        dataSource.setRemoveAbandonedTimeout(
            MapUtils.getInteger(rptDs.getOptions(), "removeAbandonedTimeout", 1800));
        dataSource.setLogAbandoned(MapUtils.getBoolean(rptDs.getOptions(), "logAbandoned", true));
        return dataSource;
    } catch (final Exception ex) {
        throw new RuntimeException("C3p0DataSourcePool Create Error", ex);
    }
}
 
Example 5
Source File: DatabaseConfiguration.java    From huanhuan-blog with Apache License 2.0 5 votes vote down vote up
/**
 * 数据库连接配置
 */
@Bean(initMethod = "init", destroyMethod = "close")
public DataSource dataSource() {
    logger.debug("Configuring Datasource");

    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setDriverClassName(driverClassName);
    druidDataSource.setUrl(url);
    druidDataSource.setUsername(username);
    druidDataSource.setPassword(password);
    druidDataSource.setMaxActive(maximumPoolSize);
    druidDataSource.setMaxWait(60000);
    druidDataSource.setMinIdle(1);
    druidDataSource.setValidationQuery("SELECT 1");
    druidDataSource.setTestWhileIdle(true);
    druidDataSource.setTestOnBorrow(false);
    druidDataSource.setTestOnReturn(false);
    /**
     * configuration exception handling
     * */
    druidDataSource.setQueryTimeout(15); //查询超时时间15s
    //通过datasource.getConnontion() 取得的连接必须在removeAbandonedTimeout这么多秒内调用close(),要不强制关闭.(就是conn不能超过指定的租期)
    druidDataSource.setRemoveAbandoned(true);
    druidDataSource.setRemoveAbandonedTimeout(600); //一个连接的租期10分钟,超时会被强制关闭
    druidDataSource.setLogAbandoned(true);
    return druidDataSource;
}
 
Example 6
Source File: DbConfig.java    From sanshanblog with Apache License 2.0 5 votes vote down vote up
@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 7
Source File: DataSourceConfiguration.java    From dk-foundation with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static DruidDataSource initDataSource(String url, String username, String password) {
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setUrl(url);
    druidDataSource.setUsername(username);
    druidDataSource.setPassword(password);

    try {
        druidDataSource.setFilters("stat");
    } catch (SQLException e) {
        e.printStackTrace();
        logger.error("druid filter init failed",e);
    }

    druidDataSource.setMaxActive(20);
    druidDataSource.setInitialSize(1);
    druidDataSource.setMaxWait(60000);
    druidDataSource.setMinIdle(1);

    druidDataSource.setTimeBetweenEvictionRunsMillis(60000);
    druidDataSource.setMinEvictableIdleTimeMillis(300000);

    druidDataSource.setValidationQuery("select 'x'");
    druidDataSource.setTestWhileIdle(true);
    druidDataSource.setTestOnBorrow(false);
    druidDataSource.setTestOnReturn(false);
    druidDataSource.setPoolPreparedStatements(true);
    druidDataSource.setMaxOpenPreparedStatements(20);
    druidDataSource.setKeepAlive(true);
    druidDataSource.setRemoveAbandoned(true);

    return druidDataSource;
}
 
Example 8
Source File: DruidProperties.java    From SpringBootBucket with MIT License 5 votes vote down vote up
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 9
Source File: DruidProperties.java    From SpringBootBucket with MIT License 5 votes vote down vote up
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: DruidProperties.java    From SpringBootBucket with MIT License 5 votes vote down vote up
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 11
Source File: DruidProperties.java    From SpringBootBucket with MIT License 5 votes vote down vote up
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 12
Source File: DruidProperties.java    From SpringBootBucket with MIT License 5 votes vote down vote up
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 13
Source File: DruidProperties.java    From SpringBootBucket with MIT License 5 votes vote down vote up
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: DruidProperties.java    From SpringBootBucket with MIT License 5 votes vote down vote up
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 15
Source File: DruidProperties.java    From SpringBootBucket with MIT License 5 votes vote down vote up
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 16
Source File: DruidProperties.java    From spring-boot-starter-dao with Apache License 2.0 4 votes vote down vote up
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 17
Source File: DruidDataSourcePool.java    From Zebra with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public DataSource build(DataSourceConfig config, boolean withDefaultValue) {
	DruidDataSource druidDataSource = new DruidDataSource();

	druidDataSource.setName(buildDsName(config));
	druidDataSource.setUrl(config.getJdbcUrl());
	druidDataSource.setUsername(config.getUsername());
	druidDataSource.setPassword(config.getPassword());
	druidDataSource.setDriverClassName(StringUtils.isNotBlank(config.getDriverClass()) ? config.getDriverClass()
	      : JdbcDriverClassHelper.getDriverClassNameByJdbcUrl(config.getJdbcUrl()));

	if (withDefaultValue) {
		druidDataSource.setInitialSize(getIntProperty(config, "initialPoolSize", 5));
		druidDataSource.setMaxActive(getIntProperty(config, "maxPoolSize", 30));
		druidDataSource.setMinIdle(getIntProperty(config, "minPoolSize", 5));
		druidDataSource.setMaxWait(getIntProperty(config, "checkoutTimeout", 1000));
		druidDataSource.setValidationQuery(getStringProperty(config, "preferredTestQuery", "SELECT 1"));
		druidDataSource.setMinEvictableIdleTimeMillis(getIntProperty(config, "minEvictableIdleTimeMillis", 1800000));// 30min
		druidDataSource
		      .setTimeBetweenEvictionRunsMillis(getIntProperty(config, "timeBetweenEvictionRunsMillis", 30000)); // 30s
		druidDataSource.setNumTestsPerEvictionRun(getIntProperty(config, "numTestsPerEvictionRun", 6));
		druidDataSource.setValidationQueryTimeout(getIntProperty(config, "validationQueryTimeout", 0));
		druidDataSource.setRemoveAbandonedTimeout(getIntProperty(config, "removeAbandonedTimeout", 300));
		if (StringUtils.isNotBlank(getStringProperty(config, "connectionInitSql", null))) {
			List<String> initSqls = new ArrayList<String>();
			initSqls.add(getStringProperty(config, "connectionInitSql", null));
			druidDataSource.setConnectionInitSqls(initSqls);
		}

		druidDataSource.setTestWhileIdle(true);
		druidDataSource.setTestOnBorrow(false);
		druidDataSource.setTestOnReturn(false);
		druidDataSource.setRemoveAbandoned(true);
		druidDataSource.setUseUnfairLock(true);
		druidDataSource.setNotFullTimeoutRetryCount(-1);
	} else {
		try {
			PropertiesInit<DruidDataSource> propertiesInit = new PropertiesInit<DruidDataSource>(druidDataSource);
			propertiesInit.initPoolProperties(config);
		} catch (Exception e) {
			throw new ZebraConfigException(String.format("druid dataSource [%s] created error : ", config.getId()), e);
		}
	}

	this.pool = druidDataSource;
	LOGGER.info(String.format("New dataSource [%s] created.", config.getId()));

	return this.pool;
}
 
Example 18
Source File: DataSourceUtil.java    From framework with Apache License 2.0 4 votes vote down vote up
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;
}