skip to Main Content

I was trying my hands on running my Spring Boot application with MySQL database on Docker Desktop via docker compose. I am getting this error upon running docker compose up .

app-1      | java.lang.NullPointerException: Cannot invoke "org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(java.sql.SQLException, String)" because the return value of "org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.sqlExceptionHelper()" is null
app-1      |    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:116) ~[hibernate-core-6.5.2.Final.jar!/:6.5.2.Final]
app-1      |    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.getJdbcEnvironmentUsingJdbcMetadata(JdbcEnvironmentInitiator.java:290) ~[hibernate-core-6.5.2.Final.jar!/:6.5.2.Final]

app-1      |    at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
app-1      |    at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:91) ~[AuthAPI-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
app-1      |    at org.springframework.boot.loader.launch.Launcher.launch(Launcher.java:53) ~[AuthAPI-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
app-1      |    at org.springframework.boot.loader.launch.JarLauncher.main(JarLauncher.java:58) ~[AuthAPI-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
app-1      |
app-1      | 2024-07-19T09:13:48.297Z  WARN 1 --- [AuthAPI] [           main] org.hibernate.orm.deprecation            : HHH90000025: MySQLDialect does not need to be specified explicitly using 'hibernate.dialect' (remove the property setting and it will be selected by default)
app-1      | 2024-07-19T09:13:53.120Z  INFO 1 --- [AuthAPI] [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration)
app-1      | 2024-07-19T09:13:53.164Z  INFO 1 --- [AuthAPI] [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
app-1      | 2024-07-19T09:13:54.169Z  WARN 1 --- [AuthAPI] [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 08S01
app-1      | 2024-07-19T09:13:54.169Z ERROR 1 --- [AuthAPI] [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : Communications link failure        
app-1      |
app-1      | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app-1      | 2024-07-19T09:13:54.511Z ERROR 1 --- [AuthAPI] [           main] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityMana
gerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution [Communications link failure
app-1      |
app-1      | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.] [n/a]        
app-1      | 2024-07-19T09:13:54.516Z ERROR 1 --- [AuthAPI] [           main] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exce
ption: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'jwtFilter': Unsatisfied dependency expresse
d through field 'userDetailsService': Error creating bean with name 'customUserDetailsService': Unsatisfied dependency expressed through field 'userReposito
ry': Error creating bean with name 'userRepository' defined in com.yourengineerbro.AuthAPI.repository.UserRepository defined in @EnableJpaRepositories decla
red on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaSharedEM_entityManagerFactory' while setting bean property 'entityManager'
app-1      | 2024-07-19T09:13:54.637Z  INFO 1 --- [AuthAPI] [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
app-1      | 2024-07-19T09:13:55.129Z  WARN 1 --- [AuthAPI] [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server
app-1      | 2024-07-19T09:13:55.151Z  INFO 1 --- [AuthAPI] [           main] .s.b.a.l.ConditionEvaluationReportLogger : 
app-1      |
app-1      | Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
app-1      | 2024-07-19T09:13:55.228Z ERROR 1 --- [AuthAPI] [           main] o.s.boot.SpringApplication               : Application run failed
app-1      |
app-1      | org.springframework.context.ApplicationContextException: Unable to start web server
app-1      |    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:165) ~[spring-boot-3.3.1.jar!/:3.3.1]

app-1      |    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:188) ~[spring-boot-3.3.1.jar!/:3.3.1]
app-1      |    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:162) ~[spring-boot-3.3.1.jar!/:3.3.1]
app-1      |    ... 15 common frames omitted
app-1      | Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jwtFilter': Unsatisfied dependency 
expressed through field 'userDetailsService': Error creating bean with name 'customUserDetailsService': Unsatisfied dependency expressed through field 'user
Repository': Error creating bean with name 'userRepository' defined in com.yourengineerbro.AuthAPI.repository.UserRepository defined in @EnableJpaRepositori
es declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaSharedEM_entityManagerFactory' while setting bean property 'entityManager'

app-1      |    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:128) ~[spring-boot-3.3.1.jar!/:3.3.1]       
app-1      |    ... 20 common frames omitted
app-1      | Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customUserDetailsService': Unsatisf
ied dependency expressed through field 'userRepository': Error creating bean with name 'userRepository' defined in com.yourengineerbro.AuthAPI.repository.Us
erRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaSharedEM_entityManagerFactory' while setting bean property 'entityManager'

app-1      |    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1353) ~[spring-beans-6.1.10.jar!/:6.1.10]
app-1      |    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:784) ~[spring-beans-6.1.10.jar!/:6.1.10]
app-1      |    ... 62 common frames omitted
app-1      | Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository' defined in com.yourengineerb
ro.AuthAPI.repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaSharedEM_entityManagerFactory' while setting bean property 'entityManager'

app-1      |    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1353) ~[spring-beans-6.1.10.jar!/:6.1.10]
app-1      |    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:784) ~[spring-beans-6.1.10.jar!/:6.1.10]
app-1      |    ... 76 common frames omitted
app-1      | Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaSharedEM_entityManagerFactory': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument

app-1      |    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:365) ~[spring-beans-6.1.10.jar!/:6.1.10]
app-1      |    ... 89 common frames omitted
app-1      | Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path 
resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution [Communications link failure      

app-1      |    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:365) ~[spring-beans-6.1.10.jar!/:6.1.10]
app-1      |    ... 101 common frames omitted
app-1      | Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution [Communications link failure
app-1      |
app-1      | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.] [n/a]        
app-1      |    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421) ~[spring-orm-6.1.10.jar!/:6.1.10]
app-1      |    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-6.1.10.jar!/:6.1.10]
app-1      |    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:366) ~[spring-orm-6.1.10.jar!/:6.1.10]
app-1      |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1835) ~[spring-beans-6.1.10.jar!/:6.1.10]
app-1      |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1784) ~[spring-beans-6.1.10.jar!/:6.1.10]
app-1      |    ... 108 common frames omitted
app-1      | Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution [Communications link failure      
app-1      |
app-1      | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.] [n/a]        
app-1      |    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:100) ~[hibernate-core-6.5.2.Final.jar!/:6.5.2.Final]

app-1      |    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-6.1.10.jar!/:6.1.10]
app-1      |    ... 112 common frames omitted
app-1      | Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
app-1      |
app-1      | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app-1      |    at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:175) ~[mysql-connector-j-8.0.33.jar!/:8.0.33]

com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:150) ~[mysql-connector-j-8.0.33.jar!/:8.0.33]
app-1      |    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:166) ~[mysql-connector-j-8.0.33.jar!/:8.0.33]
app-1      |    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89) ~[mysql-connector-j-8.0.33.jar!/:8.0.33]
app-1      |    at com.mysql.cj.NativeSession.connect(NativeSession.java:121) ~[mysql-connector-j-8.0.33.jar!/:8.0.33]
app-1      |    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:945) ~[mysql-connector-j-8.0.33.jar!/:8.0.33]
app-1      |    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:815) ~[mysql-connector-j-8.0.33.jar!/:8.0.33]
app-1      |    ... 146 common frames omitted
app-1      | Caused by: java.net.ConnectException: Connection refused
app-1      |    at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
app-1      |    at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
app-1      |    at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
app-1      |    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
app-1      |    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
app-1      |    at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
app-1      |    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:153) ~[mysql-connector-j-8.0.33.jar!/:8.0.33]
app-1      |    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63) ~[mysql-connector-j-8.0.33.jar!/:8.0.33]
app-1      |    ... 149 common frames omitted

Here is my docker-compose.yml:

version: '3.7'
services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    env_file: ./.env
    ports:
      - $SPRING_LOCAL_PORT:$SPRING_DOCKER_PORT
    depends_on:
      mysqldb:
        condition: service_healthy
    environment:
      - MYSQLDB_DOCKER_PORT=${MYSQLDB_DOCKER_PORT}
      - MYSQLDB_USER=${MYSQLDB_USER}
      - MYSQLDB_ROOT_PASSWORD=${MYSQLDB_ROOT_PASSWORD}
      - MYSQLDB_DATABASE=${MYSQLDB_DATABASE}
      - JWT_SECRET=${JWT_SECRET}
      - JWT_EXPIRATION=${JWT_EXPIRATION}
    networks:
      - springboot-mysql-network
  mysqldb:
    image: mysql:8.0.33
    env_file: ./.env
    ports:
      - $MYSQLDB_LOCAL_PORT:$MYSQLDB_DOCKER_PORT
    environment:
      - MYSQL_DATABASE=${MYSQLDB_DATABASE}
      - MYSQL_ROOT_PASSWORD=${MYSQLDB_ROOT_PASSWORD}
    volumes:
      - mysql-data:/var/lib/mysql
    networks:
      - springboot-mysql-network
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
      retries: 10
      interval: 3s
      timeout: 30s
volumes:
  mysql-data:
networks:
  springboot-mysql-network:
    name: springboot-mysql-network

application.properties:

spring.application.name=AuthAPI
jwt.secret=${JWT_SECRET}
jwt.expiration=${JWT_EXPIRATION}
spring.jpa.database=mysql
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:${MYSQLDB_DOCKER_PORT}/${MYSQLDB_DATABASE}?useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=${MYSQLDB_USER}
spring.datasource.password=${MYSQLDB_ROOT_PASSWORD}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.sql.init.mode=always
#spring.datasource.initialization-mode=always
#spring.datasource.hikari.allow-pool-suspension=true

dockerfile:

FROM maven:3.8.3-openjdk-17 AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package
ENTRYPOINT ["java","-jar","/home/app/target/AuthAPI-0.0.1-SNAPSHOT.jar"]

.env:

MYSQLDB_USER=root
MYSQLDB_ROOT_PASSWORD=password
MYSQLDB_DATABASE=connectDB
MYSQLDB_LOCAL_PORT=3307
MYSQLDB_DOCKER_PORT=3306

SPRING_LOCAL_PORT=8080
SPRING_DOCKER_PORT=8080

JWT_SECRET=your_secret_key
JWT_EXPIRATION=3600000

I am not able to identify how to fix this.

I have tried several Stack Overflow answers on similar issues, like

  • adding a platform tag in docker-compose.
  • Changing the mysql version in docker compose.

It seems that still my application container having hard time connecting with MySQL DB. I have been stuck around this for around 1 day now. Any help here will be appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks a lot to Mark for his observation. You saved my day!

    I had to change "localhost" to "mysqldb" inside application.properties.

    Also, I was using MYSQL_ROOT_PASSWORD as password, turns out default password was "root" for user "root".

    When I faced error saying my user table doesn't exist, then had to specifically add spring.jpa.hibernate.ddl-auto=update to tell spring to create table if not exist.


  2. Make sure that all your settings are correct. I’ve got the same error message simply because I had a typo in the database name of the jdbc URL.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search