1. 介绍

gb 底层使用spring boot , 因此默认的数据库连接池也是使用spring boot的推荐顺序

tomcat 连接池 → Hikari CP → dbcp

2. 使用

2.1. tomcat 连接池

确保系统的build.gradle中包含spring-boot-starter-jdbc 类库

        implementation('org.springframework.boot:spring-boot-starter-jdbc')

配置application.yml 中的数据库连接属性

spring:
    profiles: development
    h2.console.enabled: true
    h2.console.path: /dbconsole
    datasource:
      driverClassName: org.h2.Driver
      url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
      username: sa
      password:
      type: org.apache.tomcat.jdbc.pool.DataSource
      tomcat:
        initialSize: 1
        minIdle: 3
        maxActive: 100
        # 
        maxWait: 60000
        # 
        timeBetweenEvictionRunsMillis: 60000
        # 
        minEvictableIdleTimeMillis: 30000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        # PSCachePSCache
        poolPreparedStatements: true
        maxPoolPreparedStatementPerConnectionSize: 20

启动系统, 可以从输出日志中获得数据库连接池的信息(已配置成功tomcat 默认数据库连接池)

注意要为每个profile 都配置连接池

2.2. hikari 连接池

确保系统的build.gradle中包含spring-boot-starter-jdbc 类库

   implementation group: 'hikari-cp', name: 'hikari-cp', version: '2.6.0'

配置application.yml 中的数据库连接属性

spring:
    profiles: development
    h2.console.enabled: true
    h2.console.path: /dbconsole
    datasource:
      driverClassName: org.h2.Driver
      url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
      username: sa
      password:
      type: com.zaxxer.hikari.HikariDataSource   #org.apache.tomcat.jdbc.pool.DataSource  # com.alibaba.druid.pool.DruidDataSource
      hikari:
        minIdle = 5
        maxPoolSize: 100
        maxLifetime = 180000
        connectionTimeout: 30000
        validationTimeout: 5000
        idleTimeout: 60000
        initializationFailTimeout: 1
        isAutoCommit: true

启动系统, 可以从输出日志中获得数据库连接池的信息(已配置成功hikari连接池)

Spring-Boot-2.0 以上版本将默认的数据库连接池从tomcat jdbc pool改为了hikari

2.3. 阿里的Druid 连接池

使用gb的插件 druid plugin

此插件不提供hibernate自动生成表结构的功能,建议在生成环境中使用(已创立表结构的环境)

2.3.1. 使用

在gradle文件中加入

        implementation('org.yunchen.gb:gb-plugin-druid:x.x.x')

2.3.2. 配置

配置application.yml 中的数据库连接属性

spring:
    profiles: development
    h2.console.enabled: true
    h2.console.path: /dbconsole
    datasource:
      driverClassName: org.h2.Driver
      url: jdbc:h2:./prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
      username: sa
      password:
      type: com.alibaba.druid.pool.DruidDataSource
      druid:
        initialSize: 1
        minIdle: 3
        maxActive: 100
        # 
        maxWait: 60000
        # 
        timeBetweenEvictionRunsMillis: 60000
        # 
        minEvictableIdleTimeMillis: 30000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        # PSCachePSCache
        poolPreparedStatements: true
        maxPoolPreparedStatementPerConnectionSize: 20
        # filterssql
        filters: stat

配置 druid 监控路径的访问控制

修改Startup启动类的createRequestMap方法,增加如下:

  new Requestmap(name:'druid监控管理',url: '/druid/**', configAttribute: "hasAnyRole('ROLE_ADMIN')").save(flush: true);

启动系统后,使用管理员登录,访问 http://localhost:8080/project/druid 查看连接池监控情况:

druid.png

2.3.3. 提供密码加密

配置application.yml 中的数据库连接属性

spring:
    profiles: development
    h2.console.enabled: true
    h2.console.path: /dbconsole
    datasource:
      driverClassName: org.h2.Driver
      url: jdbc:h2:./prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
      username: sa
      password: WVMjPhfXQrIsWRo0/RCqAVvYtTU9WNVToKJohb8AlUmHwnV6vwFL+FM2CNFDMJwGHW1iCmyaUlF+sgvFdogqEA==
      type: com.alibaba.druid.pool.DruidDataSource
      publicKey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIiwHpFrDijV+GzwRTzWJk8D3j3jFfhsMFJ/7k1NTvBuLgL+TdIHgaMNOIEjHpXzuvX38J3FtOK8hLrySncVGOMCAwEAAQ==
      druid:
        initialSize: 1
        minIdle: 3
        maxActive: 100
        # 
        maxWait: 60000
        # 
        timeBetweenEvictionRunsMillis: 60000
        # 
        minEvictableIdleTimeMillis: 30000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        # PSCachePSCache
        poolPreparedStatements: true
        maxPoolPreparedStatementPerConnectionSize: 20
        connectionProperties: config.decrypt=true;config.decrypt.key=${spring.datasource.publicKey}
        # filterssql
        filters: config,stat,wall,log4j
        filter:
          config:
            enabled: true