【开发心得】正确使用druid

关于Druid的使用,我们很多时候遵从默认,直接使用配置文件,直接采用如下配置

  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    url: 'jdbc:mysql://localhost:3306/xxx?useUnicode=yes&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&character_set_server=utf8mb4&useSSL=false&allowPublicKeyRetrieval=true'
    username: root
    password: xxxx
    initial-size: 5
    min-idle: 5
    max-active: 20
    max-wait: 30000
    validation-query: select 1 from dual
    time-between-eviction-runs-millis: 3600000 #1小时

这样的配置,实际上是有问题的,比如,假设数据库波动,是无法自动重连的(druid连接池具有该功能,上述配置,生效的只有driver,url,username,password)。

正确的配置:

  datasource:
    druid:
      type: com.alibaba.druid.pool.DruidDataSource
      driverClassName: com.mysql.cj.jdbc.Driver
      url: 'jdbc:mysql://localhost:3306/xxx?useUnicode=yes&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&character_set_server=utf8mb4&useSSL=false&allowPublicKeyRetrieval=true'
      username: root
      password: xxxx
      initial-size: 5
      min-idle: 5
      max-active: 20
      max-wait: 30000
      validation-query: select 1 from dual
      time-between-eviction-runs-millis: 3600000 #1小时

究其原因在于DruidDataSourceWarapper的注入

 

posted @ 2022-02-08 17:12  虹梦未来  阅读(2)  评论(0编辑  收藏  举报  来源