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

The following examples show how to use com.alibaba.druid.pool.DruidDataSource#setLogAbandoned() . 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: 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 2
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 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: 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 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: 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 7
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;
}