Reputation: 161
Trying to start up a Spring Boot application that connects to two PostgreSQL databases using docker-compose and I am getting a connection refused when Spring tries to connect to either of these databases.
My configuration is as follows:
docker-compose.yml
version: '3.2'
services:
mydb-1:
container_name: mydb-1
image: mydb-1
ports:
- '5432:5432'
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=testdb1
mydb-2:
container_name: mydb-2
image: mydb-2
ports:
- '5433:5432'
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=testdb2
my-server:
image: my-server-spring
restart: on-failure
depends_on:
- mydb-1
- mydb-2
environment:
- SPRING_DB1-DATASOURCE_JDBC-URL=jdbc:postgresql://mydb-1:5432/testdb1
- SPRING_DB2-DATASOURCE_JDBC-URL=jdbc:postgresql://mydb-2:5433/testdb2
expose:
- '8080'
ports:
- '8080:8080'
Spring application.properties
spring.db1-datasource.jdbc-url= jdbc:postgresql://localhost:5432/testdb1
spring.db1-datasource.username= postgres
spring.db1-datasource.password= postgres
spring.db1-datasource.driverClassName= org.postgresql.Driver
spring.db2-datasource.jdbc-url= jdbc:postgresql://localhost:5433/testdb2
spring.db2-datasource.username= postgres
spring.db2-datasource.password= postgres
spring.db2-datasource.driverClassName= org.postgresql.Driver
Stacktrace (Part of)
org.postgresql.util.PSQLException: Connection to testdb2:5433 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
advidi-server_1 | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:285) ~[postgresql-42.2.14.jar!/:42.2.14]
advidi-server_1 | at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.14.jar!/:42.2.14]
advidi-server_1 | at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:217) ~[postgresql-42.2.14.jar!/:42.2.14]
advidi-server_1 | at org.postgresql.Driver.makeConnection(Driver.java:458) ~[postgresql-42.2.14.jar!/:42.2.14]
advidi-server_1 | at org.postgresql.Driver.connect(Driver.java:260) ~[postgresql-42.2.14.jar!/:42.2.14]
advidi-server_1 | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1 | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1 | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1 | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1 | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1 | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1 | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.4.5.jar!/:na]
advidi-server_1 | at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.4.18.Final.jar!/:5.4.18.Final]
advidi-server_1 | at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) ~[hibernate-core-5.4.18.Final.jar!/:5.4.18.Final]
Initially I thought that the problem was that the Spring application was trying to connect to the dbs before their bootstrapping is complete, but that doesn't seem to be the case since with restart: on-failure
it should at some point manage to connect.
The default localhost
values should also not be a problem since these are replaced by the environment variables in my docker-compose file.
Any ideas?
Upvotes: 0
Views: 2016
Reputation: 111
You're passing SPRING_DB1-DATASOURCE_JDBC-URL
in your environment, and not using it in the application.properties. So the variable are not overriding.
You need to either in application.properties use something like
spring.db1-datasource.jdbc-url=$SPRING_DB1-DATASOURCE_JDBC-URL
OR
in environment set the exact name of the variables in your application. properties
- spring.db1-datasource.jdbc-url=jdbc:postgresql://mydb-1:5432/testdb1
I would suggest the second option, which will still allow you to use the application.properties as default values for running locally.
Upvotes: 0
Reputation: 1518
You're not showing the mydb-1
and mydb-2
image configurations, so I'm practically guessing here.
ports:
entries.localhost
because that's the container itself.:5432
in the connection strings because that's the default.application.properties
and the environment:
entry in the server configuration. Which is the application using? Anyway, your connection strings should end with mydb-1/testdb1
and mydb-2/testdb2
- nothing more complicated than that.Upvotes: 2