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

The following examples show how to use com.alibaba.druid.pool.DruidDataSource#setMaxWait() . 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: DruidDataSourceFactory.java    From journalkeeper with Apache License 2.0 6 votes vote down vote up
@Override
public DataSource createDataSource(Path path, Properties properties) {
    String url = properties.getProperty(DruidConfigs.URL);
    if (StringUtils.isBlank(url)) {
        throw new IllegalArgumentException("url not exist");
    }

    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl(url.replace(JDBCConfigs.DATASOURCE_PATH_PLACEHOLDER, path.toString()));
    dataSource.setDriverClassName(properties.getProperty(DruidConfigs.DRIVER_CLASS));
    dataSource.setUsername(properties.getProperty(DruidConfigs.USERNAME, dataSource.getUsername()));
    dataSource.setPassword(properties.getProperty(DruidConfigs.PASSWORD, dataSource.getPassword()));
    dataSource.setInitialSize(Integer.valueOf(properties.getProperty(DruidConfigs.INITIAL_SIZE, String.valueOf(dataSource.getInitialSize()))));
    dataSource.setMinIdle(Integer.valueOf(properties.getProperty(DruidConfigs.MIN_IDLE, String.valueOf(dataSource.getMinIdle()))));
    dataSource.setMaxActive(Integer.valueOf(properties.getProperty(DruidConfigs.MAX_ACTIVE, String.valueOf(dataSource.getMaxActive()))));
    dataSource.setMaxWait(Integer.valueOf(properties.getProperty(DruidConfigs.MAX_WAIT, String.valueOf(dataSource.getMaxWait()))));
    dataSource.setTimeBetweenEvictionRunsMillis(Integer.valueOf(properties.getProperty(DruidConfigs.TIME_BETWEEN_EVICTION_RUNS_MILLIS,
            String.valueOf(dataSource.getTimeBetweenEvictionRunsMillis()))));
    dataSource.setValidationQuery(properties.getProperty(DruidConfigs.VALIDATION_QUERY, DruidConfigs.DEFAULT_VALIDATION_QUERY));
    dataSource.setTestWhileIdle(Boolean.valueOf(properties.getProperty(DruidConfigs.TEST_WHILE_IDLE, String.valueOf(dataSource.isTestWhileIdle()))));
    dataSource.setTestOnBorrow(Boolean.valueOf(properties.getProperty(DruidConfigs.TEST_ONBORROW, String.valueOf(dataSource.isTestOnBorrow()))));
    return dataSource;
}
 
Example 2
Source File: DynamicDataSource.java    From tcc-transaction with Apache License 2.0 6 votes vote down vote up
private DataSource getDruidDataSource(DataSourceInfoDto dto) {
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setUrl(dto.getDbUrl());
    druidDataSource.setUsername(dto.getUsername());
    druidDataSource.setPassword(getPassword(dto.getUsername(), dto.getPassword()));
    druidDataSource.setMaxActive(dto.getMaxActive());
    druidDataSource.setInitialSize(dto.getInitialSize());
    druidDataSource.setMaxWait(60000);
    druidDataSource.setTimeBetweenEvictionRunsMillis(300000);
    druidDataSource.setMinEvictableIdleTimeMillis(300000);
    druidDataSource.setValidationQuery("select 1");
    druidDataSource.setTestWhileIdle(true);
    druidDataSource.setTestOnBorrow(false);
    druidDataSource.setTestOnReturn(false);
    druidDataSource.setPoolPreparedStatements(true);
    druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(50);
    try {
        druidDataSource.init();
    } catch (SQLException e) {
        log.error("Can't init druidDataSource from DataSourceInfo:" + dto, e);
    }
    return druidDataSource;
}
 
Example 3
Source File: JdbcTransactionRecoverRepository.java    From Raincat with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void init(final String appName, final TxConfig txConfig) {
    dataSource = new DruidDataSource();
    final TxDbConfig txDbConfig = txConfig.getTxDbConfig();
    dataSource.setUrl(txDbConfig.getUrl());
    dataSource.setDriverClassName(txDbConfig.getDriverClassName());
    dataSource.setUsername(txDbConfig.getUsername());
    dataSource.setPassword(txDbConfig.getPassword());
    dataSource.setInitialSize(txDbConfig.getInitialSize());
    dataSource.setMaxActive(txDbConfig.getMaxActive());
    dataSource.setMinIdle(txDbConfig.getMinIdle());
    dataSource.setMaxWait(txDbConfig.getMaxWait());
    dataSource.setValidationQuery(txDbConfig.getValidationQuery());
    dataSource.setTestOnBorrow(txDbConfig.getTestOnBorrow());
    dataSource.setTestOnReturn(txDbConfig.getTestOnReturn());
    dataSource.setTestWhileIdle(txDbConfig.getTestWhileIdle());
    dataSource.setPoolPreparedStatements(txDbConfig.getPoolPreparedStatements());
    dataSource.setMaxPoolPreparedStatementPerConnectionSize(txDbConfig.getMaxPoolPreparedStatementPerConnectionSize());
    this.tableName = RepositoryPathUtils.buildDbTableName(appName);
    executeUpdate(SqlHelper.buildCreateTableSql(tableName, txDbConfig.getDriverClassName()));
}
 
Example 4
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 5
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 6
Source File: DruidProperties.java    From LuckyFrameWeb with GNU Affero General Public License v3.0 5 votes vote down vote up
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 7
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 8
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 9
Source File: DynamicDBUtil.java    From jeecg-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 获取数据源【最底层方法,不要随便调用】
 *
 * @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 10
Source File: DruidDataSourceFactory.java    From tsharding with MIT License 5 votes vote down vote up
@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 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 RuoYi-Vue with MIT License 5 votes vote down vote up
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 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: ClusterDataSourceConfig.java    From springBoot-study with Apache License 2.0 5 votes vote down vote up
@Bean(name = "clusterDataSource")
public DataSource clusterDataSource() {
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUrl(url);  
    dataSource.setUsername(username);  
    dataSource.setPassword(password);  
    dataSource.setDriverClassName(driverClass);  
      
    //具体配置 
    dataSource.setInitialSize(initialSize);  
    dataSource.setMinIdle(minIdle);  
    dataSource.setMaxActive(maxActive);  
    dataSource.setMaxWait(maxWait);  
    dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);  
    dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);  
    dataSource.setValidationQuery(validationQuery);  
    dataSource.setTestWhileIdle(testWhileIdle);  
    dataSource.setTestOnBorrow(testOnBorrow);  
    dataSource.setTestOnReturn(testOnReturn);  
    dataSource.setPoolPreparedStatements(poolPreparedStatements);  
    dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);  
    try {  
        dataSource.setFilters(filters);  
    } catch (SQLException e) { 
    	e.printStackTrace();
    }  
    dataSource.setConnectionProperties(connectionProperties);  
    return dataSource;
}
 
Example 15
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 16
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 17
Source File: DBTest.java    From canal with Apache License 2.0 4 votes vote down vote up
@Test
public void test01() throws SQLException {
    DruidDataSource dataSource = new DruidDataSource();
    // dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
    // dataSource.setUrl("jdbc:oracle:thin:@127.0.0.1:49161:XE");
    // dataSource.setUsername("mytest");
    // dataSource.setPassword("m121212");

    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true");
    dataSource.setUsername("root");
    dataSource.setPassword("121212");

    dataSource.setInitialSize(1);
    dataSource.setMinIdle(1);
    dataSource.setMaxActive(2);
    dataSource.setMaxWait(60000);
    dataSource.setTimeBetweenEvictionRunsMillis(60000);
    dataSource.setMinEvictableIdleTimeMillis(300000);

    dataSource.init();

    Connection conn = dataSource.getConnection();

    conn.setAutoCommit(false);
    PreparedStatement pstmt = conn.prepareStatement("insert into user (id,name,role_id,c_time,test1,test2) values (?,?,?,?,?,?)");

    java.util.Date now = new java.util.Date();
    for (int i = 1; i <= 10000; i++) {
        pstmt.clearParameters();
        pstmt.setLong(1, (long) i);
        pstmt.setString(2, "test_" + i);
        pstmt.setLong(3, (long) i % 4 + 1);
        pstmt.setDate(4, new java.sql.Date(now.getTime()));
        pstmt.setString(5, null);
        pstmt.setBytes(6, null);

        pstmt.execute();
        if (i % 5000 == 0) {
            conn.commit();
        }
    }
    conn.commit();

    pstmt.close();

    // Statement stmt = conn.createStatement();
    // ResultSet rs = stmt.executeQuery("select * from user t where 1=2");
    //
    // ResultSetMetaData rsm = rs.getMetaData();
    // int cnt = rsm.getColumnCount();
    // for (int i = 1; i <= cnt; i++) {
    // System.out.println(rsm.getColumnName(i) + " " +
    // rsm.getColumnType(i));
    // }

    // rs.close();
    // stmt.close();

    // PreparedStatement pstmt = conn
    // .prepareStatement("insert into tb_user
    // (id,name,role_id,c_time,test1,test2)
    // values (?,?,?,?,?,?)");
    // pstmt.setBigDecimal(1, new BigDecimal("5"));
    // pstmt.setString(2, "test");
    // pstmt.setBigDecimal(3, new BigDecimal("1"));
    // pstmt.setDate(4, new Date(new java.util.Date().getTime()));
    // byte[] a = { (byte) 1, (byte) 2 };
    // pstmt.setBytes(5, a);
    // pstmt.setBytes(6, a);
    // pstmt.execute();
    //
    // pstmt.close();

    conn.close();
    dataSource.close();
}
 
Example 18
Source File: DataDruidFactory.java    From DataSphereStudio with Apache License 2.0 4 votes vote down vote up
private static DruidDataSource createDataSource(Properties props, Logger log, String type) {
		String name = null;
		String url = null;
		String username = null;
		String password = null;
		
		if (type.equals("Job")) {
			name = props.getProperty("job.datachecker.jdo.option.name");
			url = props.getProperty("job.datachecker.jdo.option.url");
			username = props.getProperty("job.datachecker.jdo.option.username");
			password = props.getProperty("job.datachecker.jdo.option.password");
			try {
//				password = new String(Base64.getDecoder().decode(props.getProperty("job.datachecker.jdo.option.password").getBytes()),"UTF-8");
				password = props.getProperty("job.datachecker.jdo.option.password");
			} catch (Exception e){
				log.error("password decore failed" + e);
			}
		}
		int initialSize = Integer.valueOf(props.getProperty("datachecker.jdo.option.initial.size", "1"));
		int maxActive = Integer.valueOf(props.getProperty("datachecker.jdo.option.max.active", "100"));
		int minIdle = Integer.valueOf(props.getProperty("datachecker.jdo.option.min.idle", "1"));
		long maxWait = Long.valueOf(props.getProperty("datachecker.jdo.option.max.wait", "60000"));
		String validationQuery = props.getProperty("datachecker.jdo.option.validation.quert", "SELECT 'x'");
		long timeBetweenEvictionRunsMillis = Long.valueOf(props.getProperty("datachecker.jdo.option.time.between.eviction.runs.millis", "6000"));
		long minEvictableIdleTimeMillis = Long.valueOf(props.getProperty("datachecker.jdo.option.evictable.idle,time.millis", "300000"));
		boolean testOnBorrow = Boolean.valueOf(props.getProperty("datachecker.jdo.option.test.on.borrow", "true"));
		int maxOpenPreparedStatements = Integer.valueOf(props.getProperty("datachecker.jdo.option.max.open.prepared.statements", "-1"));
		
		
		if (timeBetweenEvictionRunsMillis > minEvictableIdleTimeMillis) {
			timeBetweenEvictionRunsMillis = minEvictableIdleTimeMillis;
		}
		
		DruidDataSource ds = new DruidDataSource();
		
		if (StringUtils.isNotBlank(name)) {
			ds.setName(name);
		}
		
		ds.setUrl(url);
		ds.setDriverClassName("com.mysql.jdbc.Driver");
	    ds.setUsername(username);
	    ds.setPassword(password);
	    ds.setInitialSize(initialSize);
	    ds.setMinIdle(minIdle);
	    ds.setMaxActive(maxActive);
	    ds.setMaxWait(maxWait);
	    ds.setTestOnBorrow(testOnBorrow);
	    ds.setValidationQuery(validationQuery);
	    ds.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
	    ds.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
	    if (maxOpenPreparedStatements > 0) {
	      ds.setPoolPreparedStatements(true);
	      ds.setMaxPoolPreparedStatementPerConnectionSize(
	          maxOpenPreparedStatements);
	    } else {
	      ds.setPoolPreparedStatements(false);
	    }
	    log.info("Druid data source initialed!");
	    return ds;
	}
 
Example 19
Source File: DruidConfig.java    From renren-fast with GNU General Public License v3.0 4 votes vote down vote up
@Bean
@Primary
public DataSource dataSource(){
    DruidDataSource datasource = new DruidDataSource();

    datasource.setUrl(this.dbUrl);
    datasource.setUsername(username);
    datasource.setPassword(password);
    datasource.setDriverClassName(driverClassName);
    //configuration
    if(initialSize != null) {
        datasource.setInitialSize(initialSize);
    }
    if(minIdle != null) {
        datasource.setMinIdle(minIdle);
    }
    if(maxActive != null) {
        datasource.setMaxActive(maxActive);
    }
    if(maxWait != null) {
        datasource.setMaxWait(maxWait);
    }
    if(timeBetweenEvictionRunsMillis != null) {
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    }
    if(minEvictableIdleTimeMillis != null) {
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    }
    if(validationQuery!=null) {
        datasource.setValidationQuery(validationQuery);
    }
    if(testWhileIdle != null) {
        datasource.setTestWhileIdle(testWhileIdle);
    }
    if(testOnBorrow != null) {
        datasource.setTestOnBorrow(testOnBorrow);
    }
    if(testOnReturn != null) {
        datasource.setTestOnReturn(testOnReturn);
    }
    if(poolPreparedStatements != null) {
        datasource.setPoolPreparedStatements(poolPreparedStatements);
    }
    if(maxPoolPreparedStatementPerConnectionSize != null) {
        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
    }

    if(connectionProperties != null) {
        datasource.setConnectionProperties(connectionProperties);
    }

    List<Filter> filters = new ArrayList<>();
    filters.add(statFilter());
    filters.add(wallFilter());
    datasource.setProxyFilters(filters);

    return datasource;
}
 
Example 20
Source File: DBTest.java    From canal-1.1.3 with Apache License 2.0 4 votes vote down vote up
@Test
public void test01() throws SQLException {
    DruidDataSource dataSource = new DruidDataSource();
    // dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
    // dataSource.setUrl("jdbc:oracle:thin:@127.0.0.1:49161:XE");
    // dataSource.setUsername("mytest");
    // dataSource.setPassword("m121212");

    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/mytest?useUnicode=true");
    dataSource.setUsername("root");
    dataSource.setPassword("121212");

    dataSource.setInitialSize(1);
    dataSource.setMinIdle(1);
    dataSource.setMaxActive(2);
    dataSource.setMaxWait(60000);
    dataSource.setTimeBetweenEvictionRunsMillis(60000);
    dataSource.setMinEvictableIdleTimeMillis(300000);

    dataSource.init();

    Connection conn = dataSource.getConnection();

    conn.setAutoCommit(false);
    PreparedStatement pstmt = conn
        .prepareStatement("insert into user (id,name,role_id,c_time,test1,test2) values (?,?,?,?,?,?)");

    java.util.Date now = new java.util.Date();
    for (int i = 1; i <= 10000; i++) {
        pstmt.clearParameters();
        pstmt.setLong(1, (long) i);
        pstmt.setString(2, "test_" + i);
        pstmt.setLong(3, (long) i % 4 + 1);
        pstmt.setDate(4, new java.sql.Date(now.getTime()));
        pstmt.setString(5, null);
        pstmt.setBytes(6, null);

        pstmt.execute();
        if (i % 5000 == 0) {
            conn.commit();
        }
    }
    conn.commit();

    pstmt.close();

    // Statement stmt = conn.createStatement();
    // ResultSet rs = stmt.executeQuery("select * from user t where 1=2");
    //
    // ResultSetMetaData rsm = rs.getMetaData();
    // int cnt = rsm.getColumnCount();
    // for (int i = 1; i <= cnt; i++) {
    // System.out.println(rsm.getColumnName(i) + " " + rsm.getColumnType(i));
    // }

    // rs.close();
    // stmt.close();

    // PreparedStatement pstmt = conn
    // .prepareStatement("insert into tb_user (id,name,role_id,c_time,test1,test2)
    // values (?,?,?,?,?,?)");
    // pstmt.setBigDecimal(1, new BigDecimal("5"));
    // pstmt.setString(2, "test");
    // pstmt.setBigDecimal(3, new BigDecimal("1"));
    // pstmt.setDate(4, new Date(new java.util.Date().getTime()));
    // byte[] a = { (byte) 1, (byte) 2 };
    // pstmt.setBytes(5, a);
    // pstmt.setBytes(6, a);
    // pstmt.execute();
    //
    // pstmt.close();

    conn.close();
    dataSource.close();
}