JetBrains / Exposed

Kotlin SQL Framework
http://jetbrains.github.io/Exposed/
Apache License 2.0
8.37k stars 694 forks source link

Database Init fails after upgrade to Spring Boot 2.4.x #1231

Closed benjaminkomen closed 3 years ago

benjaminkomen commented 3 years ago

After upgrading from Spring boot 2.3.5.RELEASE to 2.4.x our application cannot initialise the database connection using Exposed anymore. The full stack trace is attached below.

The piece of code where the exceptions happens:

    @Bean
    fun initDatabase(dataSource: DataSource): Database {
        val connect = Database.connect(dataSource)
        transaction {
            SchemaUtils.create(
                OrderTable,
               ***REDACTED***
            )
        }
        return connect
    }

The datasource injected in this function is just a datasource with username, password and URL of type MariaDbPoolDataSource where the URL is: jdbc:mariadb:aurora://***REDACTED***-***REDACTED***.cluster-***REDACTED***.eu-west-1.rds.amazonaws.com:3306/***REDACTED***?connectTimeout=60000&minPoolSize=0&maxIdleTime=60

Any ideas what is going on?

Full stack trace:

2021-05-10 17:31:07.649 ERROR 80020 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'initDatabase' defined in class path resource [net/***REDACTED***/adapters/exposed/ExposedConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.jetbrains.exposed.sql.Database]: Factory method 'initDatabase' threw exception; nested exception is java.sql.SQLSyntaxErrorException: No connection available within the specified time (option 'connectTimeout': 60,000 ms)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:63)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:782)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:774)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:339)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1340)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1329)
    at net.***REDACTED***.Application$Companion.main(Application.kt:107)
    at net.***REDACTED***.Application.main(Application.kt)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.jetbrains.exposed.sql.Database]: Factory method 'initDatabase' threw exception; nested exception is java.sql.SQLSyntaxErrorException: No connection available within the specified time (option 'connectTimeout': 60,000 ms)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
    ... 21 common frames omitted
Caused by: java.sql.SQLSyntaxErrorException: No connection available within the specified time (option 'connectTimeout': 60,000 ms)
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionFactory.createException(ExceptionFactory.java:62)
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionFactory.create(ExceptionFactory.java:153)
    at org.mariadb.jdbc.MariaDbPoolDataSource.getConnection(MariaDbPoolDataSource.java:239)
    at org.jetbrains.exposed.sql.Database$Companion$connect$3.invoke(Database.kt:123)
    at org.jetbrains.exposed.sql.Database$Companion$connect$3.invoke(Database.kt:58)
    at org.jetbrains.exposed.sql.Database$Companion$doConnect$3.invoke(Database.kt:112)
    at org.jetbrains.exposed.sql.Database$Companion$doConnect$3.invoke(Database.kt:58)
    at org.jetbrains.exposed.sql.Database.metadata$exposed_core(Database.kt:22)
    at org.jetbrains.exposed.sql.Database$vendor$2.invoke(Database.kt:34)
    at org.jetbrains.exposed.sql.Database$vendor$2.invoke(Database.kt:15)
    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
    at org.jetbrains.exposed.sql.Database.getVendor(Database.kt)
    at org.jetbrains.exposed.sql.Database$Companion.getDefaultIsolationLevel(Database.kt:164)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManager.getDefaultIsolationLevel(ThreadLocalTransactionManager.kt:20)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:121)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction$default(ThreadLocalTransactionManager.kt:120)
    at net.***REDACTED***.adapters.exposed.ExposedConfiguration.initDatabase(ExposedConfiguration.kt:67)
    at net.***REDACTED***.adapters.exposed.ExposedConfiguration$$EnhancerBySpringCGLIB$$a0c274d1.CGLIB$initDatabase$1(<generated>)
    at net.***REDACTED***.adapters.exposed.ExposedConfiguration$$EnhancerBySpringCGLIB$$a0c274d1$$FastClassBySpringCGLIB$$d1c65d13.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
    at net.***REDACTED***.adapters.exposed.ExposedConfiguration$$EnhancerBySpringCGLIB$$a0c274d1.initDatabase(<generated>)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 22 common frames omitted
Caused by: java.sql.SQLSyntaxErrorException: No connection available within the specified time (option 'connectTimeout': 60,000 ms)
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionFactory.createException(ExceptionFactory.java:62)
    at org.mariadb.jdbc.internal.util.exceptions.ExceptionFactory.create(ExceptionFactory.java:171)
    at org.mariadb.jdbc.internal.util.pool.Pool.getConnection(Pool.java:413)
    at org.mariadb.jdbc.MariaDbPoolDataSource.getConnection(MariaDbPoolDataSource.java:237)
    ... 46 common frames omitted
Tapac commented 3 years ago

What versions of Exposed, MariaDB driver, Spring Boot do you use?

Tapac commented 3 years ago

Also, looks similar to https://mariadb.com/kb/en/pooledconnection-with-wrong-user-credentials-timeouts-without-information/ and look at that test in the MariaDB driver.

benjaminkomen commented 3 years ago

What versions of Exposed, MariaDB driver, Spring Boot do you use?

Exposed: version 0.31.1 MariaDB driver: org.mariadb.jdbc:mariadb-java-client:2.7.2 Spring Boot: as the title says 2.4.x but what I tried this week was 2.4.5 (note: 2.3.5.RELEASE still works, this issue arrises after the upgrade to 2.4.0 or higher)

Tapac commented 3 years ago

So the only change is an upgrade of the Spring Boot version? Do you use spring-transaction or exposed-spring-boot-starter modules?

benjaminkomen commented 3 years ago

So the only change is an upgrade of the Spring Boot version? Do you use spring-transaction or exposed-spring-boot-starter modules?

Correct, that is the only change. We use neither of the two modules you mentioned.

I might look into that wrong user credentials bug you mentioned, it might be that after upgrading to spring boot 2.4.x the credentials are loaded from the wrong profile (because spring boot changed the profiles loading from the application.yml a bit).

Tapac commented 3 years ago

@benjaminkomen , were you able to find out the problem? Maybe it's related to #1167 ?

benjaminkomen commented 3 years ago

I haven't looked at it again, just kept spring boot on version 2.3.5. Maybe good to look into again next week, thx for the reminder.

Tapac commented 3 years ago

Exposed 0.32.1 depends on Spring Boot 2.3.5, so please check the that version

benjaminkomen commented 3 years ago

Interesting, with Exposed 0.32.1 and with spring boot 2.5.1 it does seem to work! But what has changed, I wonder..

benjaminkomen commented 3 years ago

Thanks for your help @Tapac , I'm going to close this issue, as it has been resolved for me.