为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

守候你守候我 2021-05-01 16:57:25
当我在maven test本地运行时通过。但是,当我在CI服务器上运行该错误时,出现此错误。Error MessageCould not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC ConnectionStacktraceorg.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC ConnectionCaused by: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC ConnectionCaused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failureThe 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检查了Database Plugin。连接成功!我的连接参数application.properties也与此对应spring.datasource.url=jdbc:mysql://mysql:3306/database?useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghaispring.datasource.username=rootspring.datasource.password=passwordspring.datasource.maxActive=5URL中的MySQL是MySQL docker容器名称。如果用错误消息中的localhost或专用IP进行更改docker container inspect mysql是相同的,而后两行中的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)用于私有IPThe 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桥接网络。总结:1.通过maven的本地测试通过2. Jenkins插件成功连接到MySQL3.从Jenkins运行时,集成测试失败。4.本地测试环境为WIN10 64bit;Jenkins在Ubuntu 16.04 64位服务器上的docker容器中运行,MySQL 5.7容器连接到同一网桥网络。
查看完整描述

3 回答

?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

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

在下面的线程中对此进行了很好的解释。

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


查看完整回答
反对 回复 2021-05-12
  • 3 回答
  • 0 关注
  • 534 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号