skip to Main Content

I have problem when I use docker compose and connection string i assign in ENV not recognize in spring application.yml
this is my docker-compose.yml


version: '3'

services:
  mysqldb:
    image: mysql:8.3.0
    ports:
      - "3307:3306"
    environment:
      MYSQL_ROOT_PASSWORD: 23012003aA@
      MYSQL_DATABASE: checklist
    restart: always
  
  spring:
    build:
      context: ./back_end
      dockerfile: Dockerfile
    depends_on:
      - mysqldb
    environment:
      - BE_PORT=9292
      - DATASOURCE_URL=jdbc:mysql://localhost:3307/checklist
      - DATASOURCE_USERNAME=root
      - DATASOURCE_PASSWORD=123456789aA@
    ports:
      - "9292:9292"

this is my applicatiom.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/checklist?useSSL=false&createDatabaseIfNotExist=true
    username: ${DATASOURCE_USERNAME}
    password: ${DATASOURCE_PASSWORD}
  jpa:
    show-sql: false
    hibernate:
      ddl-auto: update
    multipart:
      max-file-size: 10MB
      max-request-size: 10MB

why when I run it not recognize my connection string.

I try to paste connection string but it not work.

it run error about hibernate.

2

Answers


  1. Chosen as BEST ANSWER
     ERROR : Failed 
    to initialize JPA EntityManagerFactory: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due to: Unable to determine Dialect without JDBC metadata (please set 'jakarta.persistence.jdbc.url' for common cases or 'hibernate.dialect' when a custom Dialect implementation must be provided)
    spring-1    | CheckList_project  o.s.boot.SpringApplication                         reportFailure 2024-04-15 Mon 15:34:45 [      ] ERROR : Application run failed
    spring-1    | 
    spring-1    | org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path 
    resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due to: Unable to determine Dialect without JDBC metadata (please set 'jakarta.persistence.jdbc.url' for common cases or 'hibernate.dialect' when a custom Dialect implementation must be provided)
    
    Caused by: org.hibernate.HibernateException: Unable to determine Dialect without JDBC metadata (please set 'jakarta.persistence.jdbc.url' for common cases or 'hibernate.dialect' when a custom Dialect implementation must be provided)
    spring-1    |   at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:191) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
    spring-1    |   at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:87) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
    spring-1    |   at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.getJdbcEnvironmentWithDefaults(JdbcEnvironmentInitiator.java:143) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
    spring-1    |   at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.getJdbcEnvironmentUsingJdbcMetadata(JdbcEnvironmentInitiator.java:348) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
    spring-1    |   at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:107) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
    spring-1    |   at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
    spring-1    |   at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:130) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
    spring-1    |   at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) ~[hibernate-core-6.4.4.Final.jar:6.4.4.Final]
    spring-1    |   ... 31 common frames omitted
    spring-1    | 
    spring-1    | [INFO] ------------------------------------------------------------------------
    spring-1    | [INFO] BUILD FAILURE
    spring-1    | [INFO] ------------------------------------------------------------------------
    spring-1    | [INFO] Total time:  6.347 s
    spring-1    | [INFO] Finished at: 2024-04-15T15:34:45Z
    spring-1    | [INFO] ------------------------------------------------------------------------
    spring-1    | [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:3.2.3:run (default-cli) on project checklistproject_server: Process terminated with exit code: 1 -> [Help 1]
    spring-1    | [ERROR]
    spring-1    | [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    spring-1    | [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    spring-1    | [ERROR]
    spring-1    | [ERROR] For more information about the errors and possible solutions, please read the following articles:
    spring-1    | [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
    

    after I change follow us but my spring application return this error for me.

    this is my application.yml

    spring:
      datasource:
        url: ${SPRING_DATASOURCE_URL:jdbc:mysql://localhost:3307/checklist?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=UTC}
        username: ${SPRING_DATASOURCE_USERNAME:root}
        password: ${SPRING_DATASOURCE_PASSWORD:123@}
      jpa:
        show-sql: false
        hibernate:
          ddl-auto: update
        properties:
          hibernate:
            dialect: org.hibernate.dialect.MySQLDialect
    
    

    my docker compose for spring and mysql

     mysqldb:
        image: mysql:8.3.0
        ports:
          - "3307:3306"
        environment:
          MYSQL_ROOT_PASSWORD: 23012003aA@
          MYSQL_DATABASE: checklist
        restart: always
        networks:
          - checklist-network
      spring:
        build:
          context: ./back_end
          dockerfile: Dockerfile
        depends_on:
          - mysqldb
          - keycloak
        environment:
          - SERVER_PORT=9292
          - SPRING_DATASOURCE_URL=jdbc:mysql://mysqldb:3307/checklist?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=UTC
          - SPRING_DATASOURCE_USERNAME=root
          - SPRING_DATASOURCE_PASSWORD=123@
          - LOGGING_LEVEL_ROOT=INFO
          - LOGGING_LEVEL_ORG_SPRINGFRAMEWORK=ERROR
        ports:
          - "9292:9292"
        networks:
          - checklist-network
    
    

  2. You seems like not clearly about that how to associate spring applicatio with docker .

    The reason you failed is that while both application and database might in same docker-compose , but they are in indenpent docker container , it means if you indicate the resouce url as localhost , it would request the application container itself not databasereousce container.

    Fail Reason

    The resource you define in docker should build in same network ,besides this put resouce and application all of them in same network , you can achieve this by define network like following code snippet Docker-compose. after above step , we should inject the resource variables into spring application.yaml

    Above operation Step By Step like following:

    1. define network in docker-compose,
    2. checkout the reource address
    3. inject the database env variables to application
    4. checkout spring application.yaml read the resouce from env

    application.yaml

    spring:
      datasource:
        url: ${DATASOURCE_URL}
        username: ${DATASOURCE_USERNAME}
        password: ${DATASOURCE_PASSWORD}
      jpa:
        show-sql: false
        hibernate:
          ddl-auto: update
      multipart:
        max-file-size: 10MB
        max-request-size: 10MB
    
    

    Docker-compose

    version: '3'
    
    services:
      mysqldb:
        image: mysql:8.3.0
        ports:
          - "3307:3306"
        environment:
          MYSQL_ROOT_PASSWORD: 23012003aA@
          MYSQL_DATABASE: checklist
        restart: always
        networks:
          - my_network
      
      spring:
        build:
          context: ./back_end
          dockerfile: Dockerfile
        depends_on:
          - mysqldb
        environment:
          - BE_PORT=9292
          - DATASOURCE_URL=jdbc:mysql://mysqldb:3306/checklist?useSSL=false&createDatabaseIfNotExist=true
          - DATASOURCE_USERNAME=root
          - DATASOURCE_PASSWORD=123456789aA@
        ports:
          - "9292:9292"
        networks:
          - my_network
    
    networks:
      my_network:
        driver: bridge
    
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search