与 MySql 的连接自动中止。如何正确配置Connector/J?
我从错误消息中读到了这个建议:
您应该考虑过期 和/或测试连接有效性 在您的应用程序中使用之前, 增加服务器配置 客户端超时值,或使用 Connector/J 连接属性 'autoReconnect=true' 以避免这种情况 问题。
我正在使用 Spring 和 JPA。我应该在哪里配置 Connector/J? (在persistence.xml
中,或在entityManagerFactory
spring配置中,或在dateSource
spring配置中,或其他地方?)
I read this advice from error message:
You should consider either expiring
and/or testing connection validity
before use in your application,
increasing the server configured
values for client timeouts, or using
the Connector/J connection property
'autoReconnect=true' to avoid this
problem.
I'm using Spring and JPA. Where should I configure Connector/J? (in persistence.xml
, or in entityManagerFactory
spring configuration, or in dateSource
spring configuration, or somewhere else?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
文本描述了防止连接中止的三种解决方案:
使用
autoReconnect=true
配置连接字符串。这是 URL 连接字符串的一个属性,在驱动程序级别工作。您需要更改数据源配置中的连接字符串。增加超时时间。这通常是数据库的属性。您可以增加此值,看看是否会减少连接中止。
配置连接池以测试连接有效性。这是在池中完成的,而不是在驱动程序级别完成的。这将取决于您使用的数据源实现。但如果您使用池数据源,则它应该可以在数据源的属性中进行配置,例如 c3p0 .
附加注释:
The text describes three solutions to prevent connection aborts:
Configure the connection string with
autoReconnect=true
. This is a property of the URL connection string, which works at the driver level. You need to change the connection string in the data source configuration.Increase the timeout. This is normally a property of the database. You can increase this value to see if you get less connection abort.
Configure the connection pool to test the connection validatiy. This is done at the pool, not a the driver level. This will depend on the data source implementation that you use. But it should be configurable in the property of the data source, if you use a pooled one, e.g. c3p0.
Additionnal comments:
不建议使用
AutoReconnect
。来自 MySQLAutoReconnect
is not recommended. From MySQL here我经历了很多解决方案,我的问题得到了解决,但一段时间后连接超时或断开。2 3 天后,我得到了解决我的问题的解决方案。
许多解决方案建议使用 autoReconnect=true 但当我浏览文档时。我在源代码中看到以下描述 autoReconnect 参数的文本:
不建议使用此功能,因为它具有与会话状态和数据一致性相关的副作用
当我查看 Hibernate 代码时。 Hibernate的基本连接机制不支持重连,必须使用H3C0连接池(它本身并不总是支持重连)。
但是一旦使用了 H3C0,默认行为似乎是处理请求,如果连接断开,那么用户会看到错误 - 但至少它会为下一个请求重新连接。我认为一个错误比无限错误好,但仍然不如零错误。事实证明,需要选项 testConnectionOnCheckout - 文档不推荐该选项,因为在请求之前测试连接可能会导致性能降低。当然,软件首先必须能够运行,其次才是运行速度快。
因此,总而言之,要获得与“工作”的连接(我将其定义为包括通过无错误地重新连接来处理断开的连接):在“hibernate.cfg.xml”中:
然后创建一个文件“c3p0.properties”,该文件必须位于类路径的根(即无法为应用程序的特定部分覆盖它):
c3p0.properties
c3p0.testConnectionOnCheckout=true
如果此解决方案不起作用,那么还有更多可能的解决方案:-
I was go through many solutions and my problem was solved but after some time the connection is timeout or disconnected.After 2 3 days I got a solution that solve my problem.
many solution suggest to use autoReconnect=true but when I was go through the docs. I saw the following text in the source describing the autoReconnect parameter:
The use of this feature is not recommended, because it has side effects related to session state and data consistency
When I looked in the Hibernate code. The basic connection mechanism of Hibernate doesn’t support reconnecting, one has to use H3C0 connection pool (which itself didn't always support reconnecting).
But once one’s used H3C0, the default behavior seems to be that to process a request, if the connection is dead then the user sees and error - but at least it reconnects for the next request. I suppose one error is better than infinite errors, but still not as good as zero errors. It turns out one needs the optiontestConnectionOnCheckout- which the documentation doesn’t recommend because testing the connection before a request might lead to lower performance. Surely the software firstly has to work, only secondly does it have to work fast.
So, to summarize, to get a connection to “work” (which I define as including handling dropped connections by reconnecting without error): In “hibernate.cfg.xml”:
Then create a file “c3p0.properties” which must be in the root of the classpath (i.e. no way to override it for particular parts of the application):
c3p0.properties
c3p0.testConnectionOnCheckout=true
If this solution don't work than there are more possible solutions:-
其他答案是更好的长期解决方案。但如果您只需要立即重新运行 MySQL 连接,您可以
关闭
然后重新启动
Tomcat,一切都会正常工作一段时间。这使您能够在找出长期解决方案的同时避免系统停机。导航到
终端
中的$CATALINA_HOME
,然后输入shutdown.sh
,然后输入startup.sh
。等待启动序列完成,然后您的应用程序将再次运行一段时间。The other answers are better long term solutions. But if you just need the MySQL connection running again right away, you can
shutdown
thenrestart
Tomcat and everything will work fine for a while. This enables you to avoid system downtime while you figure out a longer term solution.Navigate to
$CATALINA_HOME
in theterminal
, then typeshutdown.sh
, then typestartup.sh
. Wait a few moments for the startup sequence to complete, then your apps will work again for a while.