Unable to acquire JDBC Connection on integration test when using Docker bridge network
Clash Royale CLAN TAG#URR8PPP
Unable to acquire JDBC Connection on integration test when using Docker bridge network
When I run maven test
locally is passed. But got this error when I run it on CI server.
maven test
Error Message
Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
Stacktrace
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
Caused by: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: java.net.UnknownHostException: mysql
When running local test, they all passed, maven test default setting provided by IntelliJ IDEA is used.
Since the error complains about database connection, so I checked by Jenkins Audit to Database Plugin. Connection Successful!
The connection parameter in my application.properties
also corresponds to this
application.properties
spring.datasource.url=jdbc:mysql://mysql:3306/database?useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.maxActive=5
The MySQL in the URL is the MySQL docker container name. If change it with localhost
or private IP in docker container inspect mysql
the error message is the same, while the Stacktrace is a little different on last two lines.
localhost
docker container inspect mysql
for localhost
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: java.net.ConnectException: Connection refused (Connection refused)
for private IP
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: java.net.SocketTimeoutException: connect timed out
The different I think is the host in URL, localhost is used for the local test.
While the Jenkins server used Docker bridge network.
The container status is:
docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
51ea7c7864a4 mysql:5.7 "docker-entrypoint.s…" 19 hours ago Up 19 hours 0.0.0.0:3306->3306/tcp mysql
de364f7b5eaf maven:3-jdk-8 "/usr/local/bin/mvn-…" 21 hours ago Up 21 hours
optimistic_stallman
a6545591e358 jenkinsci/blueocean "/sbin/tini -- /usr/…" 43 hours ago Up 43 hours 0.0.0.0:50000->50000/tcp, 0.0.0.0:2048->8080/tcp frosty_cray
When I run the JUnit test in IntelliJ, it fails sometimes on the local environment. The error log is like:
Caused by: org.h2.jdbc.JdbcSQLException: Schema "DATABASE" not found; SQL statement:
TRUNCATE TABLE database.data_log
I have searched the issue, it's said h2 database use upper case by default.
After run maven test
, this issue will go if run JUnit test in IDE again. But this should be not related to the root cause.
maven test
Search on the error message, find some similiar question but with different nested exception:
Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException
SpingREST: Could not open JPA EntityManager for transaction; nested exception is org.hiberna
Could not open JPA EntityManager for transaction; org.hibernate.exception.GenericJDBCException: Could not open connection
Could not open JPA EntityManager for transaction in spring
All of them is about nested exception is javax.persistence.PersistenceException
But nested exception is org.hibernate.exception.JDBCConnectionException:
is my situation.
Read Connect Java to a MySQL database
however since that plugin connects OK, means the connection from Jenkins container to MySQL container is fine.
nested exception is javax.persistence.PersistenceException
nested exception is org.hibernate.exception.JDBCConnectionException:
Summarise:
1. local test with maven passed
2. Jenkins plugin connect to MySQL success
3. Integration test fails when run from Jenkins
4. local test environment is WIN10 64bit; Jenkins run in docker container on Unbuntu 16.04 64bit server, with MySQL 5.7 container connects to the same bridge network.
@MaxFarsikov the
testcontainer
seems cool, however, after reading their document and sample, I don't feel confident enough to use it in the current project. Maybe I will try it on some side project later.– Shihe Zhang
Aug 7 at 1:35
testcontainer
The error is stating that
no packets were sent
, indicating that your application and mySQL is not talking to each other....Also you have mentioned that Jenkins and mySQL is talking to each other... that's fine ...But have you confirmed if mySQL and your application
(where is it deployed on the remote server) is talking to each other ?? check this for more info stackoverflow.com/questions/2983248/…– rohit thomas
Aug 7 at 3:23
no packets were sent
mySQL and your application
@rohitthomas I agree with you. The problem possibly lying between MySQL and the application. I have read that post and referred it in mine. Still, no clue where to check. Local test sometimes fails but will pass after I run
maven test
and will pass later on for a while.– Shihe Zhang
Aug 7 at 3:43
maven test
couple things I need to understand .... The application is running in your local and working fine?? the application is hosted on another server and it fails only there or also in local ?? finally when does it fail sometimes have you debugged that scenario ?? also maven test will pass --> have you written the connection only for this scenario ??
– rohit thomas
Aug 7 at 3:48
3 Answers
3
you should bind docker container mysql port to a port in VM.
This is well explained in the below thread.
Worth trying...
How to connect with MySQL DB running as container in docker?
docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password mysql:5.7
is ran already– Shihe Zhang
Aug 8 at 4:23
docker run -d --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password mysql:5.7
If it's the port issue, the plugin wouldn't connect successfully.
– Shihe Zhang
Aug 8 at 4:27
Thanks to @rohit-thomas. We narrow down the question to something related to the host of the URL.
The simple answer is changing the host of the JDBC URL in the spring boot application.properties
to the docker host IP address. Fromspring.datasource.url=jdbc:mysql://mysql:3306/database?
tospring.datasource.url=jdbc:mysql://172.17.0.1:3306/database?
application.properties
spring.datasource.url=jdbc:mysql://mysql:3306/database?
spring.datasource.url=jdbc:mysql://172.17.0.1:3306/database?
From inside of a Docker container, how do I connect to the localhost of the machine?
This post also helps as the final solution.
ip addr show docker0
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
...
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
...
My conclusion is:
The Jenkins container builds from an image is able to communicate with the MySQL container with its container name or private address on docker bridge network. However, as the application built by the Jenkins is not able to do that.
Since the MySQL container port has bound to the host machine, the application could communicate with MySQL container through the host port.
If the conclusion is wrong, comments are welcome.
Some things you can check that might help you resolve this issue.
In application.properties try to use docker host IP address.
from
spring.datasource.url = jdbc:mysql://mysql:3306/DATABASE_URI_PATH
to
spring.datasource.url = jdbc:mysql://192.168.99.100:33060/DATABASE_URI_PATH
Note:
You will need to map your IP and port when you do docker run or ports in docker file.And use the same docker network among your containers.
Verify if you server app can reach your mysql or vice versa. Go inside the docker container and try to ping.
@ShiheZhang i just edited my answer. Should be from jdbc:mysql://mysql:3306/DATABASE_URI_PATH to jdbc:mysql://192.168.99.100:3306/DATABASE_URI_PATH
– Winnnn
Aug 13 at 2:22
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
If you are using Spring boot and running your test DB in docker, I would recommend you to have a look at testcontainers.org , and here is instruction how to use it in the best way: areguig.github.io/… (you can skip Spock part)
– Max Farsikov
Aug 6 at 14:39