由 MySQL 提供支持的基于 Hibernate 的 Java Servlet 应用程序中的 Hibernate 通信链路故障

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2912322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:24:33  来源:igfitidea点击:

Hibernate Communications Link Failure in Hibernate Based Java Servlet application powered by MySQL

javamysqlhibernatejdbc

提问by Vatsala

Let me describe my question -

让我描述一下我的问题——

I have a Java application - Hibernate as the DB interfacing layer over MySQL. I get the communications link failure error in my application. The occurence of this error is a very specific case. I get this error , When I leave mysql server unattended for more than approximately 6 hours (i.e. when there are no queries issued to MySQL for more than approximately 6 hours). I am pasting a top 'exception' level description below, and adding a pastebin link for a detailed stacktrace description.

我有一个 Java 应用程序 - Hibernate 作为 MySQL 上的数据库接口层。我在我的应用程序中收到通信链接失败错误。此错误的发生是一个非常特殊的情况。我收到此错误,当我将 mysql 服务器无人看管超过大约 6 小时(即当没有向 MySQL 发出查询的时间超过大约 6 小时)。我在下面粘贴了一个顶级“异常”级别的描述,并添加了一个用于详细堆栈跟踪描述的 pastebin 链接。

javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Cannot open connection - Caused by: org.hibernate.exception.JDBCConnectionException: Cannot open connection - Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure - The last packet successfully received from the server was 1,274,868,181,212 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago. - Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure - The last packet successfully received from the server was 1,274,868,181,212 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago. - Caused by: java.net.ConnectException: Connection refused: connect

javax.persistence.PersistenceException:org.hibernate.exception.JDBCConnectionException:无法打开连接 - 原因:org.hibernate.exception.JDBCConnectionException:无法打开连接 - 原因:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败 - 从服务器成功接收的最后一个数据包是 1,274,868,181,212 毫秒前。最后一个成功发送到服务器的数据包是 0 毫秒前。- 引起:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败 - 从服务器成功接收的最后一个数据包是 1,274,868,181,212 毫秒前。最后一个成功发送到服务器的数据包是 0 毫秒前。- 引起:java.net.ConnectException:连接被拒绝:连接

the link to the pastebin for further investigation - http://pastebin.com/4KujAmgD

用于进一步调查的 pastebin 链接 - http://pastebin.com/4KujAmgD

What I understand from these exception statements is that MySQL is refusing to take in any connections after a period of idle/nil activity. I have been reading up a bit about this via google search, and came to know that one of the possible ways to overcome this is to set values for c3p0 properties as c3p0 comes bundled with Hibernate. Specifically, I read from here http://www.mchange.com/projects/c3p0/index.htmlthat setting two properties idleConnectionTestPeriod and preferredTestQuery will solve this for me. But these values dont seem to have had an effect.

我从这些异常语句中了解到,在一段时间的空闲/无活动之后,MySQL 拒绝接受任何连接。我一直在通过谷歌搜索阅读一些关于此的内容,并了解到克服此问题的一种可能方法是设置 c3p0 属性的值,因为 c3p0 与 Hibernate 捆绑在一起。具体来说,我从这里阅读http://www.mchange.com/projects/c3p0/index.html设置两个属性 idleConnectionTestPeriod 和 preferredTestQuery 将为我解决这个问题。但这些值似乎没有产生影响。

Is this the correct approach to fixing this? If not, what is the right way to get over this?

这是解决这个问题的正确方法吗?如果没有,克服这个问题的正确方法是什么?

The following are related Communications Link Failure questions at stackoverflow.com, but I've not found a satisfactory answer in their answers. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failureHow to handle: Communication link failure

以下是 stackoverflow.com 上的相关通信链路故障问题,但我在他们的回答中没有找到满意的答案。 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure如何处理:通讯链接失败

Note 1 - i dont get this error when I am using my application continuosly. Note 2 - I use JPA with Hibernate and hence my hibernate.dialect,etc hibernate properties reside within the persistence.xml in the META-INF folder (does that prevent the c3p0 properties from working?)

注意 1 - 当我连续使用我的应用程序时,我没有收到此错误。注意 2 - 我将 JPA 与 Hibernate 一起使用,因此我的 hibernate.dialect 等休眠属性驻留在 META-INF 文件夹中的 persistence.xml 中(这是否会阻止 c3p0 属性工作?)

edit - the c3p0 parameters I tried out are in the comments

编辑 - 我试过的 c3p0 参数在评论中

采纳答案by Sheng Chien

I had issue before. It looks like MySQL server times out your connection. Timeout is 28800 by default which is 8 hours. Please refer to this link for more details.

我之前有问题。看起来 MySQL 服务器超时了您的连接。超时默认为 28800,即 8 小时。请参阅此链接了解更多详情。

http://shengchien.blogspot.com/2009/10/hibernate-c3p0-and-mysql.html

http://shengchien.blogspot.com/2009/10/hibernate-c3p0-and-mysql.html

Hope it is useful to you.

希望对你有用。

回答by mac

Even I faced this error, I got it solved only after enabling autoReconnect=true in c3p0 config

即使我遇到了这个错误,我也是在 c3p0 配置中启用 autoReconnect=true 后才解决的

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ex_app?autoReconnect=true</property>

回答by luisacevedo

I solved my communication link failure with c3p0. I came across the blog below that explained that the org.hibernate version must be equal to the c3p0 version, so this is the pom configuration that made my day

我用 c3p0 解决了我的通信链接故障。我看到下面的博客解释说 org.hibernate 版本必须等于 c3p0 版本,所以这是让我开心的 pom 配置

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.6.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
     <artifactId>hibernate-entitymanager</artifactId>
     <version>4.3.1.Final</version>
 </dependency>
<dependency>
 <groupId>org.hibernate</groupId>
  <artifactId>hibernate-c3p0</artifactId>
 <version>4.3.6.Final</version>
</dependency>

  <dependency>
    <groupId>c3p0</groupId>
    <artifactId>c3p0</artifactId>
   <version>0.9.1.2</version>
</dependency>

like he explains, it was enough to add only one property to hibernate.cfg.xml to get c3p0 pooling

就像他解释的那样,只需向 hibernate.cfg.xml 添加一个属性即可获得 c3p0 池化

       <property name="hibernate.c3p0.min_size">10</property>

no more "communication link error" , "expected to read 5 bytes,read 0" after that. here is the link:https://howtodoinjava.com/hibernate/hibernate-c3p0-connection-pool-configuration-tutorial/

之后不再有“通信链接错误”,“预期读取 5 个字节,读取 0”。这是链接:https: //howtodoinjava.com/hibernate/hibernate-c3p0-connection-pool-configuration-tutorial/