【开发心得】正确使用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 @   虹梦未来  阅读(5)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示