无效的 JDBC-Url:驱动程序 org.postgresql.Driver 声称不接受 jdbcUrl、jdbc:postgresql://${DB_ADDR_ALIAS}:${DB_PORT}/${DB_NAME}
起始情况: 我正在使用 Spring Boot 和 Maven 构建 REST 服务。此 REST 服务正在写入 Postgres 数据库。这两个组件都是通过以下 docker-compose.yml 进行 Docker 化和配置的(我使用 .env 文件来隔离机密和重复信息。${DB_Name} 计算结果为“Taskitory".):
version: "0.0.1"
services:
db:
container_name: taskitory-db
image: postgres:14.1
environment:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_ADMIN_USER}
- POSTGRES_PASSWORD=${DB_ADMIN_PW}
- PGDATA=/data/postgres
volumes:
- ./postgres-volume:/data
networks:
- taskitory-net
ports:
- "60000:5432"
restart: unless-stopped
backend:
container_name: taskitory-backend
build: .
networks:
- taskitory-net
ports:
- "80:8080"
depends_on:
- db
networks:
taskitory-net:
driver: bridge
由于我将 Spring Boot 与 Maven 和 Java 用于 REST 服务,因此我使用 JPA 和 Hibernate 写入数据库。用于访问数据库的配置位于我的 Spring Boot 项目的 application.properties 中:
server.port = 8080
logging.level.jdbc = OFF
logging.level.jdbc.sqltiming = DEBUG
logging.level.jdbc.resultsettable = DEBUG
spring.datasource.driver-class-name = org.postgresql.Driver
spring.datasource.url = jdbc:postgresql://${DB_ADDR_ALIAS}:${DB_PORT}/${DB_NAME}
spring.datasource.username = ${DB_CLIENT_USER}
spring.datasource.password = ${DB_CLIENT_PW}
spring.datasource.hikari.max-lifetime = 600000
spring.datasource.testWhileIdle = true
spring.datasource.test-on-borrow = true
spring.datasource.hikari.schema = backend
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto = validate
我也在 application.properties 中使用 .env 文件中的变量。因此 spring.datasource.url 从“jdbc:postgresql://${DB_ADDR_ALIAS}:${DB_PORT}/${DB_NAME}”计算为“jdbc:postgresql://db” :5432/Taskitory”,其中包含 .env 文件中的以下代码:
DB_ADDR_ALIAS="db"
DB_PORT="5432"
DB_NAME="Taskitory"
DB_BE_SCHEMA="backend"
问题描述: 为了让 Spring Boot 应用程序连接到数据库,我将 jdbc-url 的主机设置为 docker-compose.yml 中指定的数据库容器的服务名称(“db”)。另外,我将 jdbc-url 的端口设置为 postgres 的本机端口“5432”。这样,数据库容器应该可以通过两个容器都连接到的 docker-network“taskitory-net”从 Spring Boot 容器访问。当我尝试构建 Spring Boot 应用程序时,出现以下错误(我需要缩短错误消息以不超过字符限制):
#10 30.45 Caused by: java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:postgresql://${DB_ADDR_ALIAS}:${DB_PORT}/${DB_NAME}
#10 30.45 at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:110) ~[HikariCP-4.0.3.jar:na]
#10 30.45 at com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:331) ~[HikariCP-4.0.3.jar:na]
#10 30.45 at com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:114) ~[HikariCP-4.0.3.jar:na]
#10 30.45 at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:108) ~[HikariCP-4.0.3.jar:na]
#10 30.45 at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-4.0.3.jar:na]
#10 30.45 at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:181) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:44) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.internal.exec.ImprovedExtractionContextImpl.getJdbcConnection(ImprovedExtractionContextImpl.java:63) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.extract.spi.ExtractionContext.getQueryResults(ExtractionContext.java:43) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorLegacyImpl.extractMetadata(SequenceInformationExtractorLegacyImpl.java:39) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl.initializeSequences(DatabaseInformationImpl.java:66) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl.<init>(DatabaseInformationImpl.java:60) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.internal.Helper.buildDatabaseInformation(Helper.java:183) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.internal.AbstractSchemaValidator.doValidation(AbstractSchemaValidator.java:68) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:200) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:81) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:335) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:471) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1498) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.14.jar:5.3.14]
#10 30.45 at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.14.jar:5.3.14]
#10 30.45 at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.14.jar:5.3.14]
#10 30.45 ... 86 common frames omitted
#10 30.45
#10 30.48 [ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 3.495 s <<< FAILURE! - in de.krayadev.taskitory.TaskitoryApplicationTests
#10 30.49 [ERROR] contextLoads Time elapsed: 0.007 s <<< ERROR!
#10 30.49 java.lang.IllegalStateException: Failed to load ApplicationContext
#10 30.49 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]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:postgresql://${DB_ADDR_ALIAS}:${DB_PORT}/${DB_NAME}
#10 30.49 Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:postgresql://${DB_ADDR_ALIAS}:${DB_PORT}/${DB_NAME}
#10 30.49 Caused by: java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:postgresql://${DB_ADDR_ALIAS}:${DB_PORT}/${DB_NAME}
我尝试过的:
- 我尝试使用以下命令构建 Spring Boot 应用程序 :数据库容器已启动并运行,然后停止。在这两种情况下都会出现此错误。
- 我尝试了主机(“db”、“localhost”、“.env-variable”)和端口(“ 5432”、“60000”、“.env-variable”)作为 jdbc-url。
- 使用 .env-variables 我收到上面显示的错误。
- 如果将主机“db”(不是通过 .env-variable)与任何端口一起使用,我会收到以下错误:
#10 28.84 Caused by: java.net.UnknownHostException: db
#10 28.84 at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:567) ~[na:na]
#10 28.84 at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
#10 28.84 at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
#10 28.84 at org.postgresql.core.PGStream.createSocket(PGStream.java:231) ~[postgresql-42.2.18.jar:42.2.18]
#10 28.84 at org.postgresql.core.PGStream.<init>(PGStream.java:95) ~[postgresql-42.2.18.jar:42.2.18]
#10 28.84 at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98) ~[postgresql-42.2.18.jar:42.2.18]
#10 28.84 at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213) ~[postgresql-42.2.18.jar:42.2.18]
#10 28.84 ... 115 common frames omitted
#10 28.84
#10 28.87 [ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.365 s <<< FAILURE! - in de.krayadev.taskitory.TaskitoryApplicationTests
#10 28.87 [ERROR] contextLoads Time elapsed: 0.005 s <<< ERROR!
#10 28.87 java.lang.IllegalStateException: Failed to load ApplicationContext
#10 28.87 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]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
#10 28.87 Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
#10 28.87 Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
#10 28.87 Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
#10 28.87 Caused by: java.net.UnknownHostException: db
- 使用主机“localhost”和任何端口我收到以下错误:
#10 29.47 Caused by: java.net.ConnectException: Connection refused
#10 29.47 at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
#10 29.47 at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
#10 29.47 at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
#10 29.47 at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
#10 29.47 at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
#10 29.47 at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
#10 29.47 at org.postgresql.core.PGStream.createSocket(PGStream.java:231) ~[postgresql-42.2.18.jar:42.2.18]
#10 29.47 at org.postgresql.core.PGStream.<init>(PGStream.java:95) ~[postgresql-42.2.18.jar:42.2.18]
#10 29.47 at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98) ~[postgresql-42.2.18.jar:42.2.18]
#10 29.47 at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213) ~[postgresql-42.2.18.jar:42.2.18]
#10 29.47 ... 115 common frames omitted
#10 29.47
#10 29.49 [ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 5.539 s <<< FAILURE! - in de.krayadev.taskitory.TaskitoryApplicationTests
#10 29.49 [ERROR] contextLoads Time elapsed: 0.005 s <<< ERROR!
#10 29.49 java.lang.IllegalStateException: Failed to load ApplicationContext
#10 29.49 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]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
#10 29.49 Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
#10 29.49 Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
#10 29.49 Caused by: org.postgresql.util.PSQLException: Connection to localhost:60000 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
#10 29.49 Caused by: java.net.ConnectException: Connection refused
如果您错过了解决我的问题的内容,请随时发表评论。
Starting situation:
I am building a REST-Service with Spring Boot and Maven. This REST-Service is writing to a Postgres database. Both components are dockerized and configured through the following docker-compose.yml (I use a .env file to isolate the confidential and repeated information. ${DB_Name} evaluates to "Taskitory".):
version: "0.0.1"
services:
db:
container_name: taskitory-db
image: postgres:14.1
environment:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_ADMIN_USER}
- POSTGRES_PASSWORD=${DB_ADMIN_PW}
- PGDATA=/data/postgres
volumes:
- ./postgres-volume:/data
networks:
- taskitory-net
ports:
- "60000:5432"
restart: unless-stopped
backend:
container_name: taskitory-backend
build: .
networks:
- taskitory-net
ports:
- "80:8080"
depends_on:
- db
networks:
taskitory-net:
driver: bridge
As I'm using Spring Boot with Maven and Java for my REST-Service, I use JPA and Hibernate for writing to my database. The config for accessing the db is located in the application.properties of my Spring Boot project:
server.port = 8080
logging.level.jdbc = OFF
logging.level.jdbc.sqltiming = DEBUG
logging.level.jdbc.resultsettable = DEBUG
spring.datasource.driver-class-name = org.postgresql.Driver
spring.datasource.url = jdbc:postgresql://${DB_ADDR_ALIAS}:${DB_PORT}/${DB_NAME}
spring.datasource.username = ${DB_CLIENT_USER}
spring.datasource.password = ${DB_CLIENT_PW}
spring.datasource.hikari.max-lifetime = 600000
spring.datasource.testWhileIdle = true
spring.datasource.test-on-borrow = true
spring.datasource.hikari.schema = backend
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto = validate
I use the variables from the .env file in the application.properties too. Therefore the spring.datasource.url evaluates from "jdbc:postgresql://${DB_ADDR_ALIAS}:${DB_PORT}/${DB_NAME}" to "jdbc:postgresql://db:5432/Taskitory" with the following code from the .env file:
DB_ADDR_ALIAS="db"
DB_PORT="5432"
DB_NAME="Taskitory"
DB_BE_SCHEMA="backend"
Problem description:
For my Spring Boot application to connect to the database I set the host of the jdbc-url to the service name of my DB container ("db") specified in my docker-compose.yml. Also I set the port of the jdbc-url to the native port of postgres "5432". This way the DB container should be accessible from the Spring Boot container through the docker-network "taskitory-net" which both containers are connected to. When I try to build my Spring Boot Application the following error appears (I needed to shorten the error messages to not blow the character limit):
#10 30.45 Caused by: java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:postgresql://${DB_ADDR_ALIAS}:${DB_PORT}/${DB_NAME}
#10 30.45 at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:110) ~[HikariCP-4.0.3.jar:na]
#10 30.45 at com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:331) ~[HikariCP-4.0.3.jar:na]
#10 30.45 at com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:114) ~[HikariCP-4.0.3.jar:na]
#10 30.45 at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:108) ~[HikariCP-4.0.3.jar:na]
#10 30.45 at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-4.0.3.jar:na]
#10 30.45 at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:181) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:44) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.internal.exec.ImprovedExtractionContextImpl.getJdbcConnection(ImprovedExtractionContextImpl.java:63) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.extract.spi.ExtractionContext.getQueryResults(ExtractionContext.java:43) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorLegacyImpl.extractMetadata(SequenceInformationExtractorLegacyImpl.java:39) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl.initializeSequences(DatabaseInformationImpl.java:66) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl.<init>(DatabaseInformationImpl.java:60) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.internal.Helper.buildDatabaseInformation(Helper.java:183) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.internal.AbstractSchemaValidator.doValidation(AbstractSchemaValidator.java:68) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:200) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:81) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:335) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:471) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1498) ~[hibernate-core-5.6.3.Final.jar:5.6.3.Final]
#10 30.45 at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.14.jar:5.3.14]
#10 30.45 at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.14.jar:5.3.14]
#10 30.45 at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.14.jar:5.3.14]
#10 30.45 ... 86 common frames omitted
#10 30.45
#10 30.48 [ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 3.495 s <<< FAILURE! - in de.krayadev.taskitory.TaskitoryApplicationTests
#10 30.49 [ERROR] contextLoads Time elapsed: 0.007 s <<< ERROR!
#10 30.49 java.lang.IllegalStateException: Failed to load ApplicationContext
#10 30.49 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]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:postgresql://${DB_ADDR_ALIAS}:${DB_PORT}/${DB_NAME}
#10 30.49 Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:postgresql://${DB_ADDR_ALIAS}:${DB_PORT}/${DB_NAME}
#10 30.49 Caused by: java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:postgresql://${DB_ADDR_ALIAS}:${DB_PORT}/${DB_NAME}
What I tried:
- I tried to build the Spring Boot application with the DB container up and running and with it stopped. In both cases this error comes up.
- I tried all combinations of host ("db", "localhost", ".env-variable") and port ("5432", "60000", ".env-variable") for the jdbc-url.
- With using the .env-variables I get the above shown error.
- In case of using the host "db" (not over .env-variable) with any port I get the following error:
#10 28.84 Caused by: java.net.UnknownHostException: db
#10 28.84 at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:567) ~[na:na]
#10 28.84 at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
#10 28.84 at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
#10 28.84 at org.postgresql.core.PGStream.createSocket(PGStream.java:231) ~[postgresql-42.2.18.jar:42.2.18]
#10 28.84 at org.postgresql.core.PGStream.<init>(PGStream.java:95) ~[postgresql-42.2.18.jar:42.2.18]
#10 28.84 at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98) ~[postgresql-42.2.18.jar:42.2.18]
#10 28.84 at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213) ~[postgresql-42.2.18.jar:42.2.18]
#10 28.84 ... 115 common frames omitted
#10 28.84
#10 28.87 [ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.365 s <<< FAILURE! - in de.krayadev.taskitory.TaskitoryApplicationTests
#10 28.87 [ERROR] contextLoads Time elapsed: 0.005 s <<< ERROR!
#10 28.87 java.lang.IllegalStateException: Failed to load ApplicationContext
#10 28.87 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]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
#10 28.87 Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
#10 28.87 Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
#10 28.87 Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
#10 28.87 Caused by: java.net.UnknownHostException: db
- With the host "localhost" and any port I get the following error:
#10 29.47 Caused by: java.net.ConnectException: Connection refused
#10 29.47 at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
#10 29.47 at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
#10 29.47 at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
#10 29.47 at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
#10 29.47 at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
#10 29.47 at java.base/java.net.Socket.connect(Socket.java:633) ~[na:na]
#10 29.47 at org.postgresql.core.PGStream.createSocket(PGStream.java:231) ~[postgresql-42.2.18.jar:42.2.18]
#10 29.47 at org.postgresql.core.PGStream.<init>(PGStream.java:95) ~[postgresql-42.2.18.jar:42.2.18]
#10 29.47 at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:98) ~[postgresql-42.2.18.jar:42.2.18]
#10 29.47 at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:213) ~[postgresql-42.2.18.jar:42.2.18]
#10 29.47 ... 115 common frames omitted
#10 29.47
#10 29.49 [ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 5.539 s <<< FAILURE! - in de.krayadev.taskitory.TaskitoryApplicationTests
#10 29.49 [ERROR] contextLoads Time elapsed: 0.005 s <<< ERROR!
#10 29.49 java.lang.IllegalStateException: Failed to load ApplicationContext
#10 29.49 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]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
#10 29.49 Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
#10 29.49 Caused by: org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
#10 29.49 Caused by: org.postgresql.util.PSQLException: Connection to localhost:60000 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
#10 29.49 Caused by: java.net.ConnectException: Connection refused
If you miss something for solving my problem feel free to comment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过将
-DskipTests=true
添加到 DOCKERFILE 中的mvn package
解决了这个问题:问题是 Spring Boot 应用程序当然无法找到 docker 网络构建时间。因此,无法运行应用程序来运行测试。
I solved the issue by adding
-DskipTests=true
to mymvn package
in the DOCKERFILE:The problem was that the Spring Boot application of course couldn't find the docker network in build time. Therefore the application couldn't be run to run the tests.
您应该将 .env 变量传递到后端容器,就像对 db 所做的那样:
对于名称解析部分,我会尝试“taskitory-db”。
You should be passing the .env variables to backend container as you did for db:
For the name resolution part, I would give a try for "taskitory-db".
我想我解决了 application.properties 文件中 .env-variables 的部分。为此,我使用 ARG 命令在 Spring Boot 应用程序的 Dockerfile 中定义了构建时间的环境变量。我使用 .env 变量在 Docker-compose.yml 中初始化了这些参数:
Dockerfile
Docker-compose.yml
.env file
application .properties
但是,仍然存在 Spring Boot 应用程序在构建时无法识别主机“db”的问题:
Caused by: java.net.UnknownHostException: db
。在我的研究过程中,我得出的结论是,在“application.properties”文件中,该字段只有组合“jdbc:postgresql://db:5432/Taskitory” “spring.datasource.url”适用。在后端容器内使用 localhost 将引用后端容器本身,因此不适用于连接到数据库容器。I think I solved the part with the .env-variables in the application.properties file. To do this, I defined environment variables for the build time in the Dockerfile of my Spring Boot application using the ARG command. I initialized these args in Docker-compose.yml with the .env variables:
Dockerfile
Docker-compose.yml
.env file
application.properties
However, there is still the problem that the Spring Boot application does not recognize the host "db" at build time:
Caused by: java.net.UnknownHostException: db
. During my research I came to the conclusion that in the "application.properties" file only the combination "jdbc:postgresql://db:5432/Taskitory" for the field "spring.datasource.url" is applicable. The use of localhost inside of the backend container would reference the backend container itself and therefore is not applicable for connecting to the db container.