简体   繁体   中英

Spring Boot + Flyway + AWS: Caused by: java.sql.SQLException: No suitable driver found

I get this except in AWS Elastic Beanstalk, but the app works locally.

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

The JDBC URL is specified correctly. I have this in 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

I do have \\WEB-INF\\lib\\mysql-connector-java-5.1.46.jar in the WAR file.

I don't know if it matters but I recently added a JDBC TokenStore for Spring Security OAuth2 and added this in the main class.

@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();
    }

I don't know if it matters (never did before) but there was an empty environment variable set

01-Aug-2018 07:44:38.290 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -DJDBC_CONNECTION_STRING=

I was able to fix it by setting environment variable

SPRING_DATASOURCE_DRIVER_CLASS_NAME=com.mysql.jdbc.Driver

Which AWS turns into

01-Aug-2018 17:01:03.505 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -DSPRING_DATASOURCE_DRIVER_CLASS_NAME=com.mysql.jdbc.Driver

I don't know why it suddenly required that property. I am using flyway-core-3.2.1.jar . I didn't specify a version in https://flywaydb.org/documentation/plugins/springboot so that's what it picked up.

You need to add the spring.datasource.driver-class-name configuration property:

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

JDBC automatic driver loading only works for drivers on the initial (system) class path of the application, but drivers located in WEB-INF/lib are added to a context class path at a later time and cannot be automatically loaded.

This means you need to explicitly load them, which is what Spring Boot does if you specify spring.datasource.driver-class-name (or the environment variable SPRING_DATASOURCE_DRIVER_CLASS_NAME as you found out).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM