Tech stack : Java 17 , SpringBoot 3.3.2, MySQL 8.0.27 ,JPA, Liquibase, Docker
I’m tring to deploy springboot application with mysql using Liquibase as shema migration tool.Here images,containers and database also created successfully.but when springboot app attaching to mysql its throwing liqubase exception
Git hub
full stacked trace
2024-08-14 22:12:11
2024-08-14 22:12:11 . ____ _ __ _ _
2024-08-14 22:12:11 /\ / ___'_ __ _ _(_)_ __ __ _
2024-08-14 22:12:11 ( ( )___ | '_ | '_| | '_ / _` |
2024-08-14 22:12:11 \/ ___)| |_)| | | | | || (_| | ) ) ) )
2024-08-14 22:12:11 ' |____| .__|_| |_|_| |___, | / / / /
2024-08-14 22:12:11 =========|_|==============|___/=/_/_/_/
2024-08-14 22:12:11
2024-08-14 22:12:11 :: Spring Boot :: (v3.3.2)
2024-08-14 22:12:11
2024-08-14 22:12:13 2024-08-14T16:42:13.013Z INFO 1 --- [ main] c.c.digital.library.LibraryApplication : Starting LibraryApplication v0.0.1-SNAPSHOT using Java 17-ea with PID 1 (/opt/app.jar started by root in /opt)
2024-08-14 22:12:13 2024-08-14T16:42:13.020Z INFO 1 --- [ main] c.c.digital.library.LibraryApplication : The following 1 profile is active: "dev"
2024-08-14 22:12:18 2024-08-14T16:42:18.705Z INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2024-08-14 22:12:18 2024-08-14T16:42:18.778Z INFO 1 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 61 ms. Found 2 JPA repository interfaces.
2024-08-14 22:12:19 2024-08-14T16:42:19.542Z INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8086 (http)
2024-08-14 22:12:19 2024-08-14T16:42:19.557Z INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2024-08-14 22:12:19 2024-08-14T16:42:19.557Z INFO 1 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.26]
2024-08-14 22:12:19 2024-08-14T16:42:19.670Z INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2024-08-14 22:12:19 2024-08-14T16:42:19.672Z INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 6494 ms
2024-08-14 22:12:19 2024-08-14T16:42:19.897Z INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2024-08-14 22:12:26 2024-08-14T16:42:26.822Z WARN 1 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Failed to initialize dependency 'liquibase' of LoadTimeWeaverAware bean 'entityManagerFactory': Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: liquibase.exception.DatabaseException: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
2024-08-14 22:12:26 2024-08-14T16:42:26.833Z INFO 1 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2024-08-14 22:12:26 2024-08-14T16:42:26.848Z INFO 1 --- [ main] .s.b.a.l.ConditionEvaluationReportLogger :
2024-08-14 22:12:26
2024-08-14 22:12:26 Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-08-14 22:12:26 2024-08-14T16:42:26.876Z ERROR 1 --- [ main] o.s.boot.SpringApplication : Application run failed
2024-08-14 22:12:26
2024-08-14 22:12:26 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Failed to initialize dependency 'liquibase' of LoadTimeWeaverAware bean 'entityManagerFactory': Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: liquibase.exception.DatabaseException: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
2024-08-14 22:12:26 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:326) ~[spring-beans-6.1.11.jar!/:6.1.11]
2024-08-14 22:12:26 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:205) ~[spring-beans-6.1.11.jar!/:6.1.11]
2024-08-14 22:12:26 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:954) ~[spring-context-6.1.11.jar!/:6.1.11]
2024-08-14 22:12:26 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:625) ~[spring-context-6.1.11.jar!/:6.1.11]
2024-08-14 22:12:26 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.3.2.jar!/:3.3.2]
2024-08-14 22:12:26 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-3.3.2.jar!/:3.3.2]
2024-08-14 22:12:26 at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456) ~[spring-boot-3.3.2.jar!/:3.3.2]
2024-08-14 22:12:26 at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) ~[spring-boot-3.3.2.jar!/:3.3.2]
2024-08-14 22:12:26 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363) ~[spring-boot-3.3.2.jar!/:3.3.2]
2024-08-14 22:12:26 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352) ~[spring-boot-3.3.2.jar!/:3.3.2]
2024-08-14 22:12:26 at com.collabera.digital.library.LibraryApplication.main(LibraryApplication.java:10) ~[!/:0.0.1-SNAPSHOT]
2024-08-14 22:12:26 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
2024-08-14 22:12:26 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[na:na]
2024-08-14 22:12:26 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
2024-08-14 22:12:26 at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
2024-08-14 22:12:26 at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:91) ~[app.jar:0.0.1-SNAPSHOT]
2024-08-14 22:12:26 at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:53) ~[app.jar:0.0.1-SNAPSHOT]
2024-08-14 22:12:26 at org.springframework.boot.loader.launch.JarLauncher.main(JarLauncher.java:58) ~[app.jar:0.0.1-SNAPSHOT]
2024-08-14 22:12:26 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: liquibase.exception.DatabaseException: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
2024-08-14 22:12:26 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1806) ~[spring-beans-6.1.11.jar!/:6.1.11]
2024-08-14 22:12:26 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600) ~[spring-beans-6.1.11.jar!/:6.1.11]
2024-08-14 22:12:26 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522) ~[spring-beans-6.1.11.jar!/:6.1.11]
2024-08-14 22:12:26 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337) ~[spring-beans-6.1.11.jar!/:6.1.11]
2024-08-14 22:12:26 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.1.11.jar!/:6.1.11]
2024-08-14 22:12:26 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335) ~[spring-beans-6.1.11.jar!/:6.1.11]
2024-08-14 22:12:26 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.1.11.jar!/:6.1.11]
2024-08-14 22:12:26 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:313) ~[spring-beans-6.1.11.jar!/:6.1.11]
2024-08-14 22:12:26 ... 17 common frames omitted
2024-08-14 22:12:26 Caused by: liquibase.exception.UnexpectedLiquibaseException: liquibase.exception.DatabaseException: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
2024-08-14 22:12:26 at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:259) ~[liquibase-core-4.27.0.jar!/:na]
2024-08-14 22:12:26 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853) ~[spring-beans-6.1.11.jar!/:6.1.11]
2024-08-14 22:12:26 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1802) ~[spring-beans-6.1.11.jar!/:6.1.11]
2024-08-14 22:12:26 ... 24 common frames omitted
2024-08-14 22:12:26 Caused by: liquibase.exception.DatabaseException: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
2024-08-14 22:12:26 at liquibase.integration.spring.SpringLiquibase.lambda$afterPropertiesSet$0(SpringLiquibase.java:251) ~[liquibase-core-4.27.0.jar!/:na]
2024-08-14 22:12:26 at liquibase.Scope.lambda$child$0(Scope.java:190) ~[liquibase-core-4.27.0.jar!/:na]
2024-08-14 22:12:26 at liquibase.Scope.child(Scope.java:199) ~[liquibase-core-4.27.0.jar!/:na]
2024-08-14 22:12:26 at liquibase.Scope.child(Scope.java:189) ~[liquibase-core-4.27.0.jar!/:na]
2024-08-14 22:12:26 at liquibase.Scope.child(Scope.java:168) ~[liquibase-core-4.27.0.jar!/:na]
2024-08-14 22:12:26 at liquibase.Scope.child(Scope.java:256) ~[liquibase-core-4.27.0.jar!/:na]
2024-08-14 22:12:26 at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:242) ~[liquibase-core-4.27.0.jar!/:na]
2024-08-14 22:12:26 ... 26 common frames omitted
2024-08-14 22:12:26 Caused by: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
2024-08-14 22:12:26 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:111) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:98) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:90) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:64) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:74) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:885) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:810) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:438) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:189) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:137) ~[HikariCP-5.1.0.jar!/:na]
2024-08-14 22:12:26 at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:360) ~[HikariCP-5.1.0.jar!/:na]
2024-08-14 22:12:26 at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:202) ~[HikariCP-5.1.0.jar!/:na]
2024-08-14 22:12:26 at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:461) ~[HikariCP-5.1.0.jar!/:na]
2024-08-14 22:12:26 at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:550) ~[HikariCP-5.1.0.jar!/:na]
2024-08-14 22:12:26 at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:98) ~[HikariCP-5.1.0.jar!/:na]
2024-08-14 22:12:26 at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:111) ~[HikariCP-5.1.0.jar!/:na]
2024-08-14 22:12:26 at liquibase.integration.spring.SpringLiquibase.lambda$afterPropertiesSet$0(SpringLiquibase.java:246) ~[liquibase-core-4.27.0.jar!/:na]
2024-08-14 22:12:26 ... 32 common frames omitted
2024-08-14 22:12:26 Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
2024-08-14 22:12:26
2024-08-14 22:12:26 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
2024-08-14 22:12:26 at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
2024-08-14 22:12:26 at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:78) ~[na:na]
2024-08-14 22:12:26 at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
2024-08-14 22:12:26 at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
2024-08-14 22:12:26 at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
2024-08-14 22:12:26 at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:104) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:149) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:165) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:88) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.mysql.cj.NativeSession.connect(NativeSession.java:120) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:829) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 ... 44 common frames omitted
2024-08-14 22:12:26 Caused by: java.net.ConnectException: Connection refused
2024-08-14 22:12:26 at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
2024-08-14 22:12:26 at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:669) ~[na:na]
2024-08-14 22:12:26 at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
2024-08-14 22:12:26 at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
2024-08-14 22:12:26 at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:331) ~[na:na]
2024-08-14 22:12:26 at java.base/java.net.Socket.connect(Socket.java:630) ~[na:na]
2024-08-14 22:12:26 at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:153) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:62) ~[mysql-connector-j-8.3.0.jar!/:8.3.0]
2024-08-14 22:12:26 ... 46 common frames omitted
2024-08-14 22:12:26
This is my application.yml
server:
port: 8086
spring:
jpa:
show-sql: true
database: mysql
database-platform: 'org.hibernate.dialect.MySQL8Dialect'
hibernate:
datasource:
url: 'jdbc:mysql://mysqldb:3306/collabera?createDatabaseIfNotExist=true'
username: root
password: root
driver-class-name: 'com.mysql.cj.jdbc.Driver'
liquibase:
change-log: classpath:db/changelog/db.changelog-master.xml
docker-compose file
version: '3.8'
services:
app:
image: musni007/collaberadigitallibrary:0.0.1
ports:
- 8086:8086
depends_on:
- mysqldb
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://mysqldb:3306/collabera?autoReconnect=true&allowPublicKeyRetrieval=true&useSSL=false
SPRING_DATASOURCE_USERNAME: admin
SPRING_DATASOURCE_PASSWORD: root
mysqldb:
image: mysql:8.0.27
ports:
- 3307:3306
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: collabera
MYSQL_USER: admin
MYSQL_PASSWORD: root
Liqubase master file
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<include file="db/changelog/v1.0.0.xml"/>
</databaseChangeLog>
and my create tables changes xml file-v1.0.0.xml
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<changeSet author="Musni" id="01">
<sqlFile path="db/sql/dump.sql" />
</changeSet>
</databaseChangeLog>
If someone help me on this ASAP, it would be greatly appreciated.Here with i have i attached my
github link
2
Answers
Your port configuration for MySQL looks like it’s incorrect.
In a compose file, when you’re exposing ports it’s declared as
HOST_PORT:CONTAINER_PORT
. You have3307:3306
so your MySQL container will make port 3306 in the container available at3307
in the host.3306
is the correct container port to use as that’s MySQL’s default port. 3307 is fine for the host port as long as clients use that port. However, you’re using 3306 in yourapplication.yml
so MySQL is inaccessible. Changing to3307
should fix the problem:It looks like the issue might be related to the timing of when the
app
service tries to connect to themysqldb
service. Even though you’ve specifieddepends_on
, this only ensures that Docker starts themysqldb
service before theapp
service. It doesn’t guarantee that MySQL is ready to accept connections by the time theapp
service starts.I think you can enhance the
depends_on
directive by adding a health check to ensure that the MySQL service is fully operational before theapp
service starts. Here’s how you can modify your Docker Compose file:This should prevent your application from trying to connect to the database before it’s ready.