title: Unable to acquire JDBC Connection on integration test when using Docker bridge network
body: <p>When I run <code>maven test</code> locally is passed. But got this error when I run it on CI server. </p>
  
  <pre><code>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
  </code></pre>
  
  <p>When running local test, they all passed, maven test default setting provided by IntelliJ IDEA is used.<br>
  Since the error complains about database connection, so I checked by Jenkins Audit to Database Plugin. Connection Successful!</p>
  
  <p><a href="https://i.stack.imgur.com/M0xQw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/M0xQw.png" alt="enter image description here"></a></p>
  
  <p>The connection parameter in my <code>application.properties</code> also corresponds to this </p>
  
  <pre><code>spring.datasource.url=jdbc:mysql://mysql:3306/database?useLegacyDatetimeCode=false&amp;serverTimezone=Asia/Shanghai
  spring.datasource.username=root
  spring.datasource.password=password
  spring.datasource.maxActive=5
  </code></pre>
  
  <p>The MySQL in the URL is the MySQL docker container name. If change it with <code>localhost</code> or private IP in <code>docker container inspect mysql</code> the error message is the same, while the Stacktrace is a little different on last two lines.</p>
  
  <p>for localhost</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>for private IP</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The different I think is the host in URL, localhost is used for the local test.
  While the Jenkins server used Docker bridge network.</p>
  
  <p>The container status is:  </p>
  
  <pre><code>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-&gt;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-&gt;50000/tcp, 0.0.0.0:2048-&gt;8080/tcp   frosty_cray
  </code></pre>
  
  <p>When I run the JUnit test in IntelliJ, it fails sometimes on the local environment. The error log is like:</p>
  
  <pre><code>Caused by: org.h2.jdbc.JdbcSQLException: Schema "DATABASE" not found; SQL statement:
  TRUNCATE TABLE database.data_log 
  </code></pre>
  
  <p>I have searched the issue, it's said h2 database use upper case by default.
  After run <code>maven test</code>, this issue will go if run JUnit test in IDE again. But this should be not related to the root cause.</p>
  
  <p>Search on the error message, find some similar question but with different nested exception:  </p>
  
  <blockquote>
    <p><a href="https://stackoverflow.com/q/32211481/1278112">Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException</a>  </p>
    
    <p><a href="https://stackoverflow.com/q/46530896/1278112">SpingREST: Could not open JPA EntityManager for transaction; nested exception is org.hiberna</a>   </p>
    
    <p><a href="https://stackoverflow.com/q/19193507/1278112">Could not open JPA EntityManager for transaction; org.hibernate.exception.GenericJDBCException: Could not open connection</a>  </p>
    
    <p><a href="https://stackoverflow.com/q/13327998/1278112">Could not open JPA EntityManager for transaction in spring</a>  </p>
  </blockquote>
  
  <p>All of them is about <code>nested exception is javax.persistence.PersistenceException</code><br>
  But <code>nested exception is org.hibernate.exception.JDBCConnectionException:</code> is my situation.
  Read <a href="https://stackoverflow.com/questions/2839321/connect-java-to-a-mysql-database/2840358#2840358">Connect Java to a MySQL database</a><br>
  however since that plugin connects OK, means the connection from Jenkins container to MySQL container is fine.</p>
  
  <p><strong>Summarise:</strong><br>
  1. local test with maven passed<br>
  2. Jenkins plugin connect to MySQL success<br>
  3. Integration test fails when run from Jenkins<br>
  4. local test environment is WIN10 64bit; Jenkins run in docker container on Ubuntu 16.04 64bit server, with MySQL 5.7 container connects to the same bridge network.</p>