Caused by: java.sql.SQLException: No suitable driver

springboot

这边文章是关于dynamic-datasource,不解释其他问题

对于这个问题其实很诡异啊,我在idea运行时候项目不报错,单独打成jar 的时候也不报错,唯独!达成war 的时候报错(公司规定war包架构),我一开始以为是因为springboot 问题。后来我怀疑是 dynamic-datasource 问题。

Caused by: java.lang.RuntimeException: Failed to get driver instance for jdbcUrl=jdbc:mysql://100.100.100.300:3306/abc?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
	at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:114)
	at com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:325)
	at com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:114)
	at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:108)
	at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81)
	at com.baomidou.dynamic.datasource.creator.HikariDataSourceCreator.createDataSource(HikariDataSourceCreator.java:90)
	at com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator.createDataSource(DefaultDataSourceCreator.java:68)
	at com.baomidou.dynamic.datasource.provider.AbstractDataSourceProvider.createDataSourceMap(AbstractDataSourceProvider.java:44)
	at com.baomidou.dynamic.datasource.provider.YmlDynamicDataSourceProvider.loadDataSources(YmlDynamicDataSourceProvider.java:42)
	at com.baomidou.dynamic.datasource.DynamicRoutingDataSource.afterPropertiesSet(DynamicRoutingDataSource.java:229)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790)
	... 81 more
Caused by: java.sql.SQLException: No suitable driver
	at java.sql.DriverManager.getDriver(DriverManager.java:315)
	at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:106)
	... 92 more

通过上面日志报错啊,可以明显看出来是没有对应 driver ,意思就是没有导入jar包

我的项目配置一开始是配置的是driver-class-name: com.mysql.jdbc.Driver
datasource下面,发现有问题

spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    strict: false
    primary: db2
    dynamic:
      datasource:
        db1:
          url: jdbc:mysql://xx.xx.xx.xx:3307/dynamic
          username: root
          password: 123456

        db2:
          url: jdbc:mysql://xx.xx.xx.xx:3307/dynamic
          username: root
          password: 123456

    hikari:
        max-pool-size: 200
        idle-timeout: 18000
        min-idle: 10
        is-auto-commit: true
        connection-timeout: 30000
        max-lifetime: 180000
        connection-test-query: select 1
    driver-class-name: com.mysql.jdbc.Driver

后面我在看dynamic 时候,上面注释上写着 # 3.2.0开始支持SPI可省略此配置,配置之后还是不行。
官网配置说明:https://github.com/baomidou/dynamic-datasource-spring-boot-starter

spring:
  datasource:
    dynamic:
      primary: master #设置默认的数据源或者数据源组,默认值即为master
      strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
      datasource:
        master:
          url: jdbc:mysql://xx.xx.xx.xx:3306/dynamic
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
        slave_1:
          url: jdbc:mysql://xx.xx.xx.xx:3307/dynamic
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver
        slave_2:
          url: ENC(xxxxx) # 内置加密,使用请查看详细文档
          username: ENC(xxxxx)
          password: ENC(xxxxx)
          driver-class-name: com.mysql.jdbc.Driver
       #......省略
       #以上会配置一个默认库master,一个组slave下有两个子库slave_1,slave_2

第二种方式,将driver-class-name提前到spring 级别下面,很诡异啊,这样子就可以了

spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    strict: false
    primary: db2
    dynamic:
      datasource:
        db1:
          url: jdbc:mysql://xx.xx.xx.xx:3307/dynamic
          username: root
          password: 123456

        db2:
          url: jdbc:mysql://xx.xx.xx.xx:3307/dynamic
          username: root
          password: 123456

      hikari:
        max-pool-size: 200
        idle-timeout: 18000
        min-idle: 10
        is-auto-commit: true
        connection-timeout: 30000
        max-lifetime: 180000
        connection-test-query: select 1
  driver-class-name: com.mysql.jdbc.Driver

应该不是这个样子吗?就是应该在datasource下面,很诡异
Caused by: java.sql.SQLException: No suitable driver_第1张图片

你可能感兴趣的:(java,经验分享,tomcat,java,spring)