zoukankan      html  css  js  c++  java
  • druid:java代码创建连接池

    PropertiesDB 是一个读取配置文件的类,也可以不用,每个参数直接用String代替。

    public DataSource dataSource(PropertiesDB propertiesDB) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(propertiesDB.getUrl());
        dataSource.setDriverClassName(propertiesDB.getDriverClassName());
        dataSource.setUsername(propertiesDB.getUsername());
        dataSource.setPassword(propertiesDB.getPassword());
        if (StringUtils.isNotBlank(propertiesDB.getConnectionProperties())) {
          dataSource.setConnectionProperties(propertiesDB.getConnectionProperties());
        }
        dataSource.setInitialSize(propertiesDB.getInitialSize());
        dataSource.setMinIdle(propertiesDB.getMinIdle());
        dataSource.setMaxWait(propertiesDB.getMaxActive());
        dataSource.setMaxActive(propertiesDB.getMaxActive());
        dataSource.setTimeBetweenEvictionRunsMillis(propertiesDB.getTimeBetweenEvictionRunsMillis());
        dataSource.setMinEvictableIdleTimeMillis(propertiesDB.getMinEvictableIdleTimeMillis());
        dataSource.setTestWhileIdle(propertiesDB.getTestWhileIdle());
        if (StringUtils.isNotBlank(propertiesDB.getValidationQuery())) {
          dataSource.setValidationQuery(propertiesDB.getValidationQuery());
        }
        dataSource.setTestOnBorrow(propertiesDB.getTestOnBorrow());
        dataSource.setTestOnReturn(propertiesDB.getTestOnReturn());
        dataSource.setPoolPreparedStatements(propertiesDB.getPoolPreparedStatements());
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(
            propertiesDB.getMaxPoolPreparedStatementPerConnectionSize());
        if (StringUtils.isNotBlank(propertiesDB.getFilters())) {
          try {
            dataSource.setFilters(propertiesDB.getFilters());
          } catch (SQLException e) {
            logger.error("初始化数据库连接池发生异常:{}", e.toString());
          }
        }
        return dataSource;
      }
  • 相关阅读:
    20151019
    20151013
    20150810
    20150626
    20150625
    20150530
    HTML特殊字符大全
    label标签跳出循环
    IIS 负载均衡
    .NET代码执行效率优化
  • 原文地址:https://www.cnblogs.com/huiy/p/9721951.html
Copyright © 2011-2022 走看看