为什么在一天左右之后Hibernate / JDBC / MySQL会断开连接? [英] Why does Hibernate/JDBC/MySQL drop connections after a day or so?

查看:139
本文介绍了为什么在一天左右之后Hibernate / JDBC / MySQL会断开连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个服务器进程偶尔响应来自客户端的消息并执行只读事务。

几天后,服务器运行时,他们停止正常工作,当我检查它时,会发现有大量关于连接被关闭的消息。



当我检查它时,它变成了默认情况下hibernate工作在某种开发模式下,连接在几个小时后被丢弃,我开始使用c3po进行连接池。



但是,即使使用c3po,在服务器启动后大约24小时左右,我也会遇到这个问题。



有没有人遇到过这个问题,并知道如何解决它?我对配置hibernate的复杂性不够熟悉。

解决方案

MySQL JDBC驱动程序在闲置8小时后超时删除连接。



您可以在您的JDBC URL中设置 autoReconnect = true ,这会导致驱动程序重新连接如果在断开连接后尝试查询。但是这有副作用;例如会话状态和事务不能通过新连接维护。



如果使用 autoReconnect ,JDBC连接重新建立,但它不会自动重新执行您的查询得到异常。所以您需要在应用程序中捕获 SQLException 并重试查询。 阅读 http://dev.mysql.com/doc/refman/5.0/ en / connector-j-reference-configuration-properties.html 以获取更多详细信息。


I have several server processes that once in a while respond to messages from the clients and perform read-only transactions.

After about a few days that the servers are running, they stop working correctly and when I check it turns out that there's a whole bunch of messages about the connection being closed.

When I checked it out, it turned out that hibernate by default works in some sort of development mode where connections are dropped after a few hours, and I started using c3po for connection pooling.

However, even with c3po, I get that problem about 24 hours or so after the servers are started.

Has anyone encountered that problem and knows how to address it? I'm not familiar enough with the intricacies of configuring hibernate.

解决方案

The MySQL JDBC driver times out after 8 hours of inactivity and drops the connection.

You can set autoReconnect=true in your JDBC URL, and this causes the driver to reconnect if you try to query after it has disconnected. But this has side effects; for instance session state and transactions cannot be maintained over a new connection.

If you use autoReconnect, the JDBC connection is reestablished, but it doesn't automatically re-execute your query that got the exception. So you do need to catch SQLException in your application and retry queries.

Read http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html for more details.

这篇关于为什么在一天左右之后Hibernate / JDBC / MySQL会断开连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆