mybatis-plus多数据源整合alibaba druid连接池配置validationQuery不生效问题

文章讲述了在配置Druid连接池和多数据源(包括PostgreSQL和MariaDB)时遇到的问题,初始配置导致启动报错。问题的关键在于不同的数据库验证查询语句不同,修正后的解决方案是为每个数据源指定适合的validationQuery,例如`selectversion()`和`select1`。
摘要由CSDN通过智能技术生成

一开始配置连接池以及多数据源信息是这样的

datasource:
    druid:
      filters: stat
      maxActive: 20
      initialSize: 1
      maxWait: 60000
      minIdle: 1
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      validationQuery: select 'x'
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      poolPreparedStatements: true
      maxOpenPreparedStatements: 100
    dynamic:
      primary: master
      strict: false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候会抛出异常,不启动则使用默认数据源.
      datasource:
        druid:
          initial-size: 5
          max-active: 5
          min-idle: 1
          max-wait: 60000
        master:
          url: jdbc:postgresql://ip:5431/test?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=GMT%2B8
          username: postgres
          password: SmartAgriculture123.
          driver-class-name: org.postgresql.Driver
        bigdata:
          url: jdbc:mariadb://ip:10030/test?characterEncoding=utf-8&useSSL=false&useTimezone=true
          username: root
          password: geovis@123
          driver-class-name: org.mariadb.jdbc.Driver

但是启动时会报错

明明配置了,但是仍然报错,一时找不到原因,

问题解决:找到博客 方案来源

 不同数据库的数据判断语句不同,语句可查看该连接

最终解决问题后配置如下:

datasource:
    druid:
      filters: stat
      maxActive: 20
      initialSize: 1
      maxWait: 60000
      minIdle: 1
      timeBetweenEvictionRunsMillis: 60000
      minEvictableIdleTimeMillis: 300000
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      validationQuery: select 'x'
      poolPreparedStatements: true
      maxOpenPreparedStatements: 100
    dynamic:
      primary: master
      strict: false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候会抛出异常,不启动则使用默认数据源.
      datasource:
        druid:
          initial-size: 5
          max-active: 5
          min-idle: 1
          max-wait: 60000
        master:
          url: jdbc:postgresql://ip:5431/test?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&useSSL=false&serverTimezone=GMT%2B8
          username: postgres
          password: SmartAgriculture123.
          driver-class-name: org.postgresql.Driver
          druid:
            validationQuery: select version()
        bigdata:
          url: jdbc:mariadb://ip:10030/test?characterEncoding=utf-8&useSSL=false&useTimezone=true
          username: root
          password: geovis@123
          driver-class-name: org.mariadb.jdbc.Driver
          druid:
            validationQuery: select 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值