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

The following examples show how to use com.alibaba.druid.pool.DruidDataSource#setFilters() . 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 Spring-Blog with Apache License 2.0 6 votes vote down vote up
/**
 * 手动创建DruidDataSource,通过DataSourceProperties 读取配置
 * 参数格式
 * spring.datasource.url=jdbc:mysql://localhost:3306/charles_blog
 * spring.datasource.username=root
 * spring.datasource.password=root
 * spring.datasource.driver-class-name=com.mysql.jdbc.Driver
 *
 * @return DataSource
 * @throws SQLException
 */
@Bean(name = "slaveDataSource")
@Qualifier("slaveDataSource")
public DataSource slaveDataSource() throws SQLException {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl(properties.getUrl());
    dataSource.setDriverClassName(properties.getDriverClassName());
    dataSource.setUsername(properties.getUsername());
    dataSource.setPassword(properties.getPassword());
    dataSource.setInitialSize(5);
    dataSource.setMinIdle(5);
    dataSource.setMaxActive(100);
    dataSource.setMaxWait(60000);
    dataSource.setTimeBetweenEvictionRunsMillis(60000);
    dataSource.setMinEvictableIdleTimeMillis(300000);
    dataSource.setValidationQuery("SELECT 'x'");
    dataSource.setTestWhileIdle(true);
    dataSource.setTestOnBorrow(false);
    dataSource.setTestOnReturn(false);
    dataSource.setPoolPreparedStatements(true);
    dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
    dataSource.setFilters("stat,wall");
    return dataSource;
}
 
Example 2
Source File: DruidDataSourceFactoryBean.java    From cloud-config with MIT License 6 votes vote down vote up
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 vote down vote up
@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: DruidConfiguration.java    From Liudao with GNU General Public License v3.0 6 votes vote down vote up
@Bean
public DataSource druidDataSource() {
    DruidDataSource datasource = new DruidDataSource();
    datasource.setUrl(propertyResolver.getProperty("url"));
    datasource.setDriverClassName(propertyResolver.getProperty("driver-class-name"));
    datasource.setUsername(propertyResolver.getProperty("username"));
    datasource.setPassword(propertyResolver.getProperty("password"));
    datasource.setInitialSize(Integer.valueOf(propertyResolver.getProperty("initialSize")));
    datasource.setMinIdle(Integer.valueOf(propertyResolver.getProperty("minIdle")));
    datasource.setMaxWait(Long.valueOf(propertyResolver.getProperty("maxWait")));
    datasource.setMaxActive(Integer.valueOf(propertyResolver.getProperty("maxActive")));
    datasource.setPoolPreparedStatements(Boolean.valueOf(propertyResolver.getProperty("poolPreparedStatements")));
    datasource.setMaxPoolPreparedStatementPerConnectionSize(Integer.valueOf(propertyResolver.getProperty("maxPoolPreparedStatementPerConnectionSize")));
    try {
        datasource.setFilters(propertyResolver.getProperty("filters"));
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return datasource;
}
 
Example 5
Source File: DruidDataSourceConfig.java    From micro-service with Apache License 2.0 5 votes vote down vote up
@Bean
@Primary
public DataSource druidDataSource(){
    DruidDataSource datasource = new DruidDataSource();

    datasource.setUrl(dataSourceProperties.getUrl());
    datasource.setUsername(dataSourceProperties.getUsername());
    datasource.setPassword(dataSourceProperties.getPassword());
    datasource.setDriverClassName(dataSourceProperties.getDriverClassName());
    datasource.setInitialSize(dataSourceProperties.getInitialSize());
    datasource.setMinIdle(dataSourceProperties.getMinIdle());
    datasource.setMaxActive(dataSourceProperties.getMaxActive());
    datasource.setMaxWait(dataSourceProperties.getMaxWait());
    datasource.setTimeBetweenEvictionRunsMillis(dataSourceProperties.getTimeBetweenEvictionRunsMillis());
    datasource.setMinEvictableIdleTimeMillis(dataSourceProperties.getMinEvictableIdleTimeMillis());
    datasource.setValidationQuery(dataSourceProperties.getValidationQuery());
    datasource.setTestWhileIdle(dataSourceProperties.isTestWhileIdle());
    datasource.setTestOnBorrow(dataSourceProperties.isTestOnBorrow());
    datasource.setTestOnReturn(dataSourceProperties.isTestOnReturn());
    datasource.setPoolPreparedStatements(dataSourceProperties.isPoolPreparedStatements());
    try {
        datasource.setFilters(dataSourceProperties.getFilters());
    } catch (SQLException e) {
        logger.error("Druid configuration initialization filter error.", e);
    }
    return datasource;
}
 
Example 6
Source File: DruidDataSourceConfig.java    From ssm-demo with MIT License 5 votes vote down vote up
@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 7
Source File: DruidConfig.java    From Photo with GNU General Public License v3.0 5 votes vote down vote up
@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 8
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 9
Source File: DruidConfig.java    From sophia_scaffolding with Apache License 2.0 5 votes vote down vote up
@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 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: DruidDataSourceConfig.java    From xxpay-master with MIT License 5 votes vote down vote up
@Bean(initMethod = "init", destroyMethod = "close")
public DruidDataSource dataSource() throws SQLException {
    if (StringUtils.isEmpty(propertyResolver.getProperty("url"))) {
        System.out.println("Your database connection pool configuration is incorrect!"
                + " Please check your Spring profile, current profiles are:"
                + Arrays.toString(environment.getActiveProfiles()));
        throw new ApplicationContextException(
                "Database connection pool is not configured correctly");
    }
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setDriverClassName(propertyResolver.getProperty("driver-class-name"));
    druidDataSource.setUrl(propertyResolver.getProperty("url"));
    druidDataSource.setUsername(propertyResolver.getProperty("username"));
    druidDataSource.setPassword(propertyResolver.getProperty("password"));
    druidDataSource.setInitialSize(Integer.parseInt(propertyResolver.getProperty("initialSize")));
    druidDataSource.setMinIdle(Integer.parseInt(propertyResolver.getProperty("minIdle")));
    druidDataSource.setMaxActive(Integer.parseInt(propertyResolver.getProperty("maxActive")));
    druidDataSource.setMaxWait(Integer.parseInt(propertyResolver.getProperty("maxWait")));
    druidDataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(propertyResolver.getProperty("timeBetweenEvictionRunsMillis")));
    druidDataSource.setMinEvictableIdleTimeMillis(Long.parseLong(propertyResolver.getProperty("minEvictableIdleTimeMillis")));
    druidDataSource.setValidationQuery(propertyResolver.getProperty("validationQuery"));
    druidDataSource.setTestWhileIdle(Boolean.parseBoolean(propertyResolver.getProperty("testWhileIdle")));
    druidDataSource.setTestOnBorrow(Boolean.parseBoolean(propertyResolver.getProperty("testOnBorrow")));
    druidDataSource.setTestOnReturn(Boolean.parseBoolean(propertyResolver.getProperty("testOnReturn")));
    druidDataSource.setPoolPreparedStatements(Boolean.parseBoolean(propertyResolver.getProperty("poolPreparedStatements")));
    druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(Integer.parseInt(propertyResolver.getProperty("maxPoolPreparedStatementPerConnectionSize")));
    druidDataSource.setFilters(propertyResolver.getProperty("filters"));
    return druidDataSource;
}
 
Example 12
Source File: ClusterDruidDataSourceConfig.java    From SpringBoot-Study with Apache License 2.0 5 votes vote down vote up
@ConfigurationProperties(prefix = "spring.datasource.cluster")
@Bean(name = "clusterDataSource")
public DataSource clusterDataSource() {
    DruidDataSource dataSource = new DruidDataSource();
    try {
        dataSource.setFilters("stat,wall,log4j");
        dataSource.setUseGlobalDataSourceStat(true);
    } catch (SQLException e) {
        //
    }
    return dataSource;
}
 
Example 13
Source File: MasterDataSourceConfig.java    From springBoot-study with Apache License 2.0 5 votes vote down vote up
@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 14
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 15
Source File: DruidConfig.java    From mykit-delay with Apache License 2.0 5 votes vote down vote up
public DataSource newInstanceDruidDataSource() throws Exception {
    if (mydataSource != null) {
        return mydataSource;
    }
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setUrl(this.url);
    druidDataSource.setUsername(this.username);
    druidDataSource.setPassword(this.password);
    druidDataSource.setDriverClassName(this.driverClassName);
    druidDataSource.setInitialSize(this.initialSize);
    druidDataSource.setMaxActive(this.maxActive);
    druidDataSource.setMinIdle(this.minIdle);
    druidDataSource.setMaxWait(this.maxWait);
    druidDataSource.setTimeBetweenEvictionRunsMillis(this.timeBetweenEvictionRunsMillis);
    druidDataSource.setMinEvictableIdleTimeMillis(this.minEvictableIdleTimeMillis);
    druidDataSource.setValidationQuery(this.validationQuery);
    druidDataSource.setTestWhileIdle(this.testWhileIdle);
    druidDataSource.setTestOnBorrow(this.testOnBorrow);
    druidDataSource.setTestOnReturn(this.testOnReturn);
    druidDataSource.setPoolPreparedStatements(this.poolPreparedStatements);
    druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(this.maxPoolPreparedStatementPerConnectionSize);
    druidDataSource.setFilters(this.filters);

    try {
        if (null != druidDataSource) {
            druidDataSource.setFilters("wall,stat");
            druidDataSource.setUseGlobalDataSourceStat(true);
            druidDataSource.init();
        }
    } catch (Exception e) {
        throw new RuntimeException("load datasource error, dbProperties is :", e);
    }
    synchronized (this) {
        mydataSource = druidDataSource;
    }
    druidDataSource.init();
    return druidDataSource;
}
 
Example 16
Source File: DruidDataSourceConfig.java    From spring-boot-shiro with Apache License 2.0 5 votes vote down vote up
@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 17
Source File: DruidDataSourceConfig.java    From genericdao with Artistic License 2.0 4 votes vote down vote up
/**
 * 构建alibaba的数据库连接池
 * @param environment
 * @param propertyPrefix
 * @return
 */
@Override
public DataSource buildDataSource(Environment environment , String propertyPrefix){
	DruidDataSource dataSource = new DruidDataSource() ;
	
	dataSource.setDriverClassName(environment.getRequiredProperty(propertyPrefix+PROPERTY_NAME_JDBC_DRIVER));
	dataSource.setUrl(environment.getRequiredProperty(propertyPrefix+PROPERTY_NAME_JDBC_URL));
	dataSource.setUsername(environment.getRequiredProperty(propertyPrefix+PROPERTY_NAME_JDBC_USERNAME));
	dataSource.setPassword(environment.getRequiredProperty(propertyPrefix+PROPERTY_NAME_JDBC_PASSWORD));
	
	//分别设置最大和最小连接数
	if(environment.containsProperty(propertyPrefix+PROPERTY_NAME_JDBC_POOL_MAXACTIVE)){
		dataSource.setMaxActive(environment.getProperty(propertyPrefix+PROPERTY_NAME_JDBC_POOL_MAXACTIVE , Integer.class));
	}
	
	if(environment.containsProperty(propertyPrefix+PROPERTY_NAME_JDBC_POOL_MINIDLE)){
		dataSource.setMinIdle(environment.getProperty(propertyPrefix+PROPERTY_NAME_JDBC_POOL_MINIDLE , Integer.class));
	}
	
	dataSource.setDefaultAutoCommit(false);
	
	//是否需要进行验证
	dataSource.setValidationQuery(environment.getRequiredProperty(propertyPrefix+PROPERTY_NAME_VALIDATION_QUERY));
	//由连接池中获取连接时,需要进行验证
	dataSource.setTestOnBorrow(true) ;
	//归还连接时不需要验证
	dataSource.setTestOnReturn(false) ;
	//连接空闲时进行验证
	dataSource.setTestWhileIdle(true) ;

	//设置datasource监控,只对sql执行状态进行监控
	try {
		dataSource.setFilters("stat") ;
	}catch (Exception e){
		e.printStackTrace() ;
	}

	if(StringUtils.isNotBlank(environment.getProperty(propertyPrefix+PROPERTY_NAME_VALIDATION_INTERVAL))){
		//连接有效的验证时间间隔
		dataSource.setValidationQueryTimeout(environment.getRequiredProperty(propertyPrefix+PROPERTY_NAME_VALIDATION_INTERVAL , Integer.class));
		//连接空闲验证的时间间隔
		dataSource.setTimeBetweenEvictionRunsMillis(environment.getRequiredProperty(propertyPrefix+PROPERTY_NAME_VALIDATION_INTERVAL , Long.class));
	}
	
	return dataSource ;
}
 
Example 18
Source File: MasterDataSourceConfig.java    From springBoot-study with Apache License 2.0 4 votes vote down vote up
@Bean(name = "masterDataSource")
    @Primary //标志这个 Bean 如果在多个同类 Bean 候选时,该 Bean 优先被考虑。 
    public DataSource masterDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(url);  
        dataSource.setUsername(username);  
//        dataSource.setPassword(password);  
//        dataSource.setPassword("23654");  
        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) { 
        	logger.error(e.getMessage());
        }  
        dataSource.setConnectionProperties(connectionProperties);  
        String password2=AESUtil.decrypt(password);
        //如果为空,说明解密失败,那么就直接连接,若还是失败,说明密码错误,直接退出程序
        dataSource.setPassword(password2==null?password:password2);
    	//如果连接失败,则直接退出程序!
    	if(!testCon(dataSource)){
    		logger.error("数据库连接失败!程序退出!");
    		System.exit(1);
    	}
    	//连接成功并且是明文的话,就直接回写密文
    	if(password2==null){
	    	SetSystemProperty ssp = new SetSystemProperty();
	    	Map<String, String> map = new HashMap<String, String>();
	    	map.put("spring.datasource.password", AESUtil.encrypt(password));
			ssp.updateProperties("application.properties", map, "password Encode");
			logger.error("密文回写成功!");
    	}
        return dataSource;
    }
 
Example 19
Source File: TAtomDsConfHandle.java    From tddl with Apache License 2.0 3 votes vote down vote up
/**
 * druid特殊开关,针对特殊编码转换,目前是B2B中文站专用
 * 
 * @param connectionProperties
 * @param druidDataSource
 * @throws SQLException
 */
public static void fillDruidFilters(Map<String, String> connectionProperties, DruidDataSource druidDataSource)
                                                                                                              throws SQLException {
    if (connectionProperties.containsKey("clientEncoding") || connectionProperties.containsKey("serverEncoding")) {
        druidDataSource.setFilters(TDDL_DRUID_ENCODING_FILTER);
    }
}
 
Example 20
Source File: TAtomDsConfHandle.java    From tddl5 with Apache License 2.0 3 votes vote down vote up
/**
 * druid特殊开关,针对特殊编码转换,目前是B2B中文站专用
 * 
 * @param connectionProperties
 * @param druidDataSource
 * @throws SQLException
 */
public static void fillDruidFilters(Map<String, String> connectionProperties, DruidDataSource druidDataSource)
                                                                                                              throws SQLException {
    if (connectionProperties.containsKey("clientEncoding") || connectionProperties.containsKey("serverEncoding")) {
        druidDataSource.setFilters("encoding");
    }
}