Docker container with Spring Boot application fails trying to connect to postgres

540 views Asked by At

I have written some microserices and now i want to dokerize them. I have a problem i can't solve for a long time, maybe you can help me. I have 2 microservices which work with PostgreSQL databases. I created postgresql container and my first microservice connects correctly. This is properties of the first microservice:

server.port=8080

spring.datasource.url=jdbc:postgresql://postgres:5431/order-service
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=matvey
spring.datasource.password=matvey

This is docker-compose.yml:

  postgres-order:
    container_name: postgres-order
    image: postgres
    environment:
      POSTGRES_DB: order-service
      POSTGRES_USER: matvey
      POSTGRES_PASSWORD: matvey
      PGDATA: /data/postgres
    volumes:
      - ./volumes/postgres-order:/data/postgres
    expose:
      - "5431"
    ports:
      - "5431:5431"
    command: -p 5431
    restart: unless-stopped

  #Order Service
  order-service:
    container_name: order-service
    image: zaxarleningod/order-service:latest
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - SPRING_DATASOURCE_URL=jdbc:postgresql://postgres-order:5431/order-service
    depends_on:
      - postgres-order
      - broker
      - discovery-server
      - api-gateway

So, this service runs well without fails. I have the second, as i think, similar to the first, but for some reason it fais. This is properties of the second microservice:

server.port=8080
spring.datasource.url=jdbc:postgresql://postgres:5432/inventory-service
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=matvey
spring.datasource.password=matvey

docker-compose.yml:

  postgres-inventory:
    container_name: postgres-inventory
    image: postgres
    environment:
      POSTGRES_DB: inventory-service
      POSTGRES_USER: matvey
      POSTGRES_PASSWORD: matvey
      PGDATA: /data/postgres
    volumes:
      - ./volumes/postgres-inventory:/data/postgres
    ports:
      - "5432:5432"
    restart: unless-stopped

  #Inventory Service
  inventory-service:
    container_name: inventory-service
    image: zaxarleningod/inventory-service:latest
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - SPRING_DATASOURCE_URL=jdbc:postgresql://postgres-inventory:5432/inventory-service
    depends_on:
      - postgres-inventory
      - discovery-server
      - api-gateway

After running container, i see this information in logs:

2023-02-28 15:51:14 2023-02-28T12:51:14.465Z  INFO 1 --- [  restartedMain] SQL dialect                              : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2023-02-28 15:51:14 2023-02-28T12:51:14.465Z  WARN 1 --- [  restartedMain] org.hibernate.orm.deprecation            : HHH90000026: MySQL5Dialect has been deprecated; use org.hibernate.dialect.MySQLDialect instead
2023-02-28 15:51:14 2023-02-28T12:51:14.529Z  WARN 1 --- [  restartedMain] com.zaxxer.hikari.pool.ProxyConnection   : HikariPool-1 - Connection org.postgresql.jdbc.PgConnection@5a08bd46 marked as broken because of SQLSTATE(0A000), ErrorCode(0)
2023-02-28 15:51:14 
2023-02-28 15:51:14 java.sql.SQLFeatureNotSupportedException: Method org.postgresql.jdbc.PgConnection.createClob() is not yet implemented.
2023-02-28 15:51:14     at org.postgresql.Driver.notImplemented(Driver.java:731) ~[postgresql-42.5.4.jar:42.5.4]
2023-02-28 15:51:14     at org.postgresql.jdbc.PgConnection.createClob(PgConnection.java:1420) ~[postgresql-42.5.4.jar:42.5.4]
2023-02-28 15:51:14     at com.zaxxer.hikari.pool.HikariProxyConnection.createClob(HikariProxyConnection.java) ~[HikariCP-5.0.1.jar:na]
2023-02-28 15:51:14     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
2023-02-28 15:51:14     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:na]
2023-02-28 15:51:14     at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:na]
2023-02-28 15:51:14     at java.base/java.lang.reflect.Method.invoke(Unknown Source) ~[na:na]
2023-02-28 15:51:14     at org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl.useContextualLobCreation(LobCreatorBuilderImpl.java:121) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
2023-02-28 15:51:14     at org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl.makeLobCreatorBuilder(LobCreatorBuilderImpl.java:57) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
2023-02-28 15:51:14     at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl.<init>(JdbcEnvironmentImpl.java:301) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
2023-02-28 15:51:14     at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:220) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
2023-02-28 15:51:14     at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:36) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
2023-02-28 15:51:14     at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:119) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
2023-02-28 15:51:14     at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:255) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
2023-02-28 15:51:14     at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:230) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
2023-02-28 15:51:14     at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
2023-02-28 15:51:14     at org.hibernate.boot.model.relational.Database.<init>(Database.java:44) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
2023-02-28 15:51:14     at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.getDatabase(InFlightMetadataCollectorImpl.java:218) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
2023-02-28 15:51:14     at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:191) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
2023-02-28 15:51:14     at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:138) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
2023-02-28 15:51:14     at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1350) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
2023-02-28 15:51:14     at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1421) ~[hibernate-core-6.1.6.Final.jar:6.1.6.Final]
2023-02-28 15:51:14     at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:66) ~[spring-orm-6.0.4.jar:6.0.4]
2023-02-28 15:51:14     at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376) ~[spring-orm-6.0.4.jar:6.0.4]
2023-02-28 15:51:14     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-6.0.4.jar:6.0.4]
2023-02-28 15:51:14     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-6.0.4.jar:6.0.4]
2023-02-28 15:51:14     at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352) ~[spring-orm-6.0.4.jar:6.0.4]
2023-02-28 15:51:14     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1797) ~[spring-beans-6.0.4.jar:6.0.4]
2023-02-28 15:51:14     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1747) ~[spring-beans-6.0.4.jar:6.0.4]
2023-02-28 15:51:14     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:599) ~[spring-beans-6.0.4.jar:6.0.4]
2023-02-28 15:51:14     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521) ~[spring-beans-6.0.4.jar:6.0.4]
2023-02-28 15:51:14     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.4.jar:6.0.4]
2023-02-28 15:51:14     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.4.jar:6.0.4]
2023-02-28 15:51:14     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.4.jar:6.0.4]
2023-02-28 15:51:14     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.4.jar:6.0.4]
2023-02-28 15:51:14     at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1130) ~[spring-context-6.0.4.jar:6.0.4]
2023-02-28 15:51:14     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:905) ~[spring-context-6.0.4.jar:6.0.4]
2023-02-28 15:51:14     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) ~[spring-context-6.0.4.jar:6.0.4]
2023-02-28 15:51:14     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.0.2.jar:3.0.2]
2023-02-28 15:51:14     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[spring-boot-3.0.2.jar:3.0.2]
2023-02-28 15:51:14     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:432) ~[spring-boot-3.0.2.jar:3.0.2]
2023-02-28 15:51:14     at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-3.0.2.jar:3.0.2]
2023-02-28 15:51:14     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[spring-boot-3.0.2.jar:3.0.2]
2023-02-28 15:51:14     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) ~[spring-boot-3.0.2.jar:3.0.2]
2023-02-28 15:51:14     at project_structure.inventory_service.InventoryServiceApplication.main(InventoryServiceApplication.java:16) ~[classes/:na]
2023-02-28 15:51:14     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
2023-02-28 15:51:14     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:na]
2023-02-28 15:51:14     at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:na]
2023-02-28 15:51:14     at java.base/java.lang.reflect.Method.invoke(Unknown Source) ~[na:na]
2023-02-28 15:51:14     at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-3.0.2.jar:3.0.2]
2023-02-28 15:51:14 
2023-02-28 15:51:15 Hibernate: drop table if exists inventories
2023-02-28 15:51:15 Hibernate: create table inventories (id bigint not null auto_increment, quantity integer, sku_code varchar(255), primary key (id)) engine=MyISAM
2023-02-28 15:51:15 2023-02-28T12:51:15.743Z  WARN 1 --- [  restartedMain] o.h.t.s.i.ExceptionHandlerLoggedImpl     : GenerationTarget encountered exception accepting command : Error executing DDL "create table inventories (id bigint not null auto_increment, quantity integer, sku_code varchar(255), primary key (id)) engine=MyISAM" via JDBC Statement
2023-02-28 15:51:15 
2023-02-28 15:51:15 org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table inventories (id bigint not null auto_increment, quantity integer, sku_code varchar(255), primary key (id)) engine=MyISAM" via JDBC Statement

As i see, application is using MySQL Dialect for some reason and can't create table with auto incrementing, while my first similar microservice is using right postgreSQL dialect. Where can be problem here? Link to github: https://github.com/MatveyAndrosyukk/cheat_sheet_project/tree/master/microservices-realization.

1

There are 1 answers

1
Ken Chan On

Normally Hibernate will determine which dialect to use by asking some questions of the JDBC Connection . Not sure why it determines to use MySQL dialect. But the following JDBC connection warning is suspicious :

2023-02-28 15:51:14 2023-02-28T12:51:14.529Z WARN 1 --- [ restartedMain] com.zaxxer.hikari.pool.ProxyConnection : HikariPool-1

  • Connection org.postgresql.jdbc.PgConnection@5a08bd46 marked as broken because of SQLSTATE(0A000), ErrorCode(0)

You can try to skip this automatic dialect resolution process by explicitly configure it to use PostgreSQL dialect which can be done by adding the following in application.properties :

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect