当前位置: 首页 > 知识库问答 >
问题:

使用Docker bridge网络时无法在集成测试中获取JDBC连接

通煜祺
2023-03-14

当我运行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

当运行本地测试时,它们都通过了,使用IntelliJ IDEA提供的maven测试默认设置。
由于错误是关于数据库连接的,所以我通过Jenkins Audit检查了数据库插件。连接成功!

我的application.properties中的connection参数也与此对应

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

URL中的MySQL是MySQL docker容器名。如果在Docker container inspect MySQL中使用localhost或private IP更改它,则错误消息是相同的,而Stacktrace在最后两行略有不同。

对于本地主机

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)

对于私有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

我认为不同的是URL中的主机,localhost用于本地测试。而Jenkins服务器则使用Docker bridge网络。

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

当我在IntelliJ中运行JUnit测试时,它有时会在本地环境中失败。错误日志如下所示:

Caused by: org.h2.jdbc.JdbcSQLException: Schema "DATABASE" not found; SQL statement:
TRUNCATE TABLE database.data_log 

我搜索过这个问题,据说h2数据库默认使用大写。在运行maven test之后,如果再次在IDE中运行JUnit测试,这个问题就会消失。但这应该与根本原因无关。

在错误消息上进行搜索,找到一些相似的问题,但有不同的嵌套异常:

无法为事务打开JPA EntityManager;org.hibernate.exception.GenericJDBCException:无法打开连接

无法在spring中为事务打开JPA EntityManager

所有这些都是关于嵌套异常是javax.persistence.persistenceException
嵌套异常是org.hibernate.exception.jdbcConnectionException:是我的情况。阅读Connect Java to a MySQL Database
但是,由于该插件连接正常,意味着从Jenkins容器到MySQL容器的连接正常。

摘要:
1。通过了maven的本地测试
2。Jenkins插件连接到MySQL成功
3。从Jenkins
4运行时集成测试失败。本地测试环境为WIN10 64bit;Jenkins运行在Ubuntu16.04 64位服务器上的docker容器中,与MySQL5.7容器连接到同一个桥接网络。

共有1个答案

沈旻
2023-03-14

您应该将docker容器mysql端口绑定到VM中的一个端口。

这在下面的线程中有很好的解释。

值得一试……
如何在Docker中与作为容器运行的MySQL DB连接?

 类似资料:
  • 我有一个Hibernate和光数据源的Spring Boot项目。如果我使用注入的SessionFactory对象来获取会话对象,几天后,我会发现与数据库操作相关的任何方法都有这样的异常(只有重新启动才能解决这个问题): 似乎手动使用的会话使此问题。(我有类似的项目,具有相同的配置和功能,但没有注入SessionFactory和Session...我根本没有这样的问题) 应用程序. yaml: 数

  • 我们的项目中有Spring boot/Hibernate/MYSQL应用程序,并使用Hikari作为连接池。服务启动几分钟后,我们发现以下问题: org.springframework.web.util.嵌套异常:请求处理失败;嵌套异常org.springframework.dao.DataAccessResourceFailureExc0019:无法获取JDBC连接;嵌套异常org.hibern

  • 我通过动态创建具有以下属性的数据源来连接mysql数据库,它工作正常,但过了一段时间后,它一直给我错误“无法获取JDBC连接”。 它被托管在AWS和Tomcat环境中。DB是AWS的RDS。 更新: 原因:

  • 我用的是Spring靴。我的数据库属性存在于ApplicationItest.properties文件中。 如何做这两个测试?我必须使用的正确注释是什么?有人能举个例子吗?

  • 我正在使用maven运行spring集成测试。我试图在启用调试端口的情况下运行它,这样我就可以从IntelliJ idea连接到它。 下面的命令行在过去工作过。它用于允许surefire启动初始化,然后等待连接到端口8000,然后继续测试: 然而,这给了我以下例外: [错误]在当前项目和插件组[com.zillow,org.apache.maven.plugins,org.codehaus.moj