【问题标题】:Spring Boot + Flyway + AWS: Caused by: java.sql.SQLException: No suitable driver foundSpring Boot + Flyway + AWS:原因:java.sql.SQLException:找不到合适的驱动程序
【发布时间】:2019-01-09 07:08:03
【问题描述】:

我在 AWS Elastic Beanstalk 中除外,但该应用程序在本地运行。

01-Aug-2018 07:44:54.815 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: 
 org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
Caused by: org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://d-use1-xx.xxxxxxxxxxx.us-east-1.rds.amazonaws.com:3306/xxxxxxxxxxx

正确指定了 JDBC URL。我有这个在file.war/WEB-INF/classes/application.properties

spring.datasource.url = jdbc:mysql://d-use1-xx.xxxxxxxxxx.us-east-1.rds.amazonaws.com:3306/xxxxxxxxx
spring.datasource.username = xxxx
spring.datasource.password = xxxxxxxxx

我在 WAR 文件中确实有 \WEB-INF\lib\mysql-connector-java-5.1.46.jar

我不知道这是否重要,但我最近为 Spring Security OAuth2 添加了一个 JDBC TokenStore,并将其添加到主类中。

@SpringBootApplication
@MapperScan("com.xxxxxx.xxxxxx.mapper")
public class XxxxxxxxxxxxApplication extends SpringBootServletInitializer {

    @Bean(name = "OAuth")
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }

我不知道这是否重要(以前从未这样做过)但是设置了一个空的环境变量

01-Aug-2018 07:44:38.290 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:-DJDBC_CONNECTION_STRING=

【问题讨论】:

  • 添加你的flyway配置&&有问题的mysql版本
  • 我正在使用 Spring Boot。没有 Flyway 配置。见flywaydb.org/documentation/plugins/springboot。我的本地 MySQL 是mysql Ver 15.1 Distrib 10.1.30-MariaDB。 AWS 数据库是 MySQL 5.6.39。
  • application.properties 中唯一的 Flyway 设置是 flyway.baselineOnMigrate = true

标签: java spring spring-boot jdbc flyway


【解决方案1】:

您需要添加spring.datasource.driver-class-name 配置属性:

spring.datasource.driver-class-name = com.mysql.jdbc.Driver

JDBC 自动加载驱动程序仅适用于应用程序初始(系统)类路径上的驱动程序,但位于WEB-INF/lib 的驱动程序稍后会添加到上下文类路径中,并且无法自动加载。

这意味着您需要显式加载它们,如果您指定 spring.datasource.driver-class-name(或您发现的环境变量 SPRING_DATASOURCE_DRIVER_CLASS_NAME),这就是 Spring Boot 所做的。

【讨论】:

    【解决方案2】:

    我可以通过设置环境变量来修复它

    SPRING_DATASOURCE_DRIVER_CLASS_NAME=com.mysql.jdbc.Driver
    

    哪个AWS变成了

    01-Aug-2018 17:01:03.505 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log 命令行参数:-DSPRING_DATASOURCE_DRIVER_CLASS_NAME=com.mysql.jdbc.Driver

    我不知道为什么它突然需要那个属性。我正在使用flyway-core-3.2.1.jar。我没有在 https://flywaydb.org/documentation/plugins/springboot 中指定版本,所以它就是这样。

    【讨论】:

      猜你喜欢
      • 2013-07-04
      • 2010-09-14
      • 2016-11-28
      • 2011-08-02
      • 2012-08-28
      • 2015-07-05
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多