与 MySql 的连接自动中止。如何正确配置Connector/J?

发布于 2024-08-17 16:53:04 字数 346 浏览 3 评论 0原文

我从错误消息中读到了这个建议:

您应该考虑过期 和/或测试连接有效性 在您的应用程序中使用之前, 增加服务器配置 客户端超时值,或使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

漫雪独思 2024-08-24 16:53:04

文本描述了防止连接中止的三种解决方案:

  1. 使用 autoReconnect=true 配置连接字符串。这是 URL 连接字符串的一个属性,在驱动程序级别工作。您需要更改数据源配置中的连接字符串。

    url="jdbc:mysql://localhost:3306/confluence?autoReconnect=true"
    
  2. 增加超时时间。这通常是数据库的属性。您可以增加此值,看看是否会减少连接中止。

  3. 配置连接池以测试连接有效性。这是在池中完成的,而不是在驱动程序级别完成的。这将取决于您使用的数据源实现。但如果您使用池数据源,则它应该可以在数据源的属性中进行配置,例如 c3p0 .

附加注释:

  • 数据源/池也可以有超时,它对应于空闲连接在池中保留的时间。不要与数据库超时混淆。
  • 有多种方法可以测试连接的有效性。一种常见的方法是使用虚拟测试表。池将在虚拟测试表上发出选择,以查看连接是否仍然正常。

The text describes three solutions to prevent connection aborts:

  1. 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.

    url="jdbc:mysql://localhost:3306/confluence?autoReconnect=true"
    
  2. Increase the timeout. This is normally a property of the database. You can increase this value to see if you get less connection abort.

  3. 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:

  • The datasource/pool can also have a timeout, which corresponds to the time an idle connection remains in the pool. To not confused with the db timeout.
  • There are several way to test the validity of a connection. One common way is to have dummy test table. The pool will issue a select on the dummy test table to see if the connection is still OK.
遗忘曾经 2024-08-24 16:53:04

不建议使用AutoReconnect。来自 MySQL

驱动程序是否应该尝试重新建立失效和/或失效的连接?
如果启用,驱动程序将为在以下位置发出的查询抛出异常:
属于当前事务的陈旧或失效连接,
但会在下一个查询发出之前尝试重新连接
新事务中的连接。该功能的使用不
推荐,因为它具有与会话状态相关的副作用
应用程序不处理 SQLException 时的数据一致性
正确地使用,并且仅设计为当您无法使用时使用
配置您的应用程序以处理因死亡而导致的 SQLException
和陈旧的连接正确。或者,作为最后一个选择,
研究将 MySQL 服务器变量“wait_timeout”设置为高值
值,而不是默认的 8 小时。

AutoReconnect is not recommended. From MySQL here

Should the driver try to re-establish stale and/or dead connections?
If enabled the driver will throw an exception for a queries issued on
a stale or dead connection, which belong to the current transaction,
but will attempt reconnect before the next query issued on the
connection in a new transaction. The use of this feature is not
recommended, because it has side effects related to session state and
data consistency when applications don't handle SQLExceptions
properly, and is only designed to be used when you are unable to
configure your application to handle SQLExceptions resulting from dead
and stale connections properly. Alternatively, as a last option,
investigate setting the MySQL server variable "wait_timeout" to a high
value, rather than the default of 8 hours.

故事与诗 2024-08-24 16:53:04

我经历了很多解决方案,我的问题得到了解决,但一段时间后连接超时或断开。2 3 天后,我得到了解决我的问题的解决方案。

许多解决方案建议使用 autoReconnect=true 但当我浏览文档时。我在源代码中看到以下描述 autoReconnect 参数的文本:

不建议使用此功能,因为它具有与会话状态和数据一致性相关的副作用

当我查看 Hibernate 代码时。 Hibernate的基本连接机制不支持重连,必须使用H3C0连接池(它本身并不总是支持重连)。

但是一旦使用了 H3C0,默认行为似乎是处理请求,如果连接断开,那么用户会看到错误 - 但至少它会为下一个请求重新连接。我认为一个错误比无限错误好,但仍然不如零错误。事实证明,需要选项 testConnectionOnCheckout - 文档不推荐该选项,因为在请求之前测试连接可能会导致性能降低。当然,软件首先必须能够运行,其次才是运行速度快。

因此,总而言之,要获得与“工作”的连接(我将其定义为包括通过无错误地重新连​​接来处理断开的连接):在“hibernate.cfg.xml”中:

  <!-- hibernate.cfg.xml -->
    <property name="c3p0.min_size">5</property>
    <property name="c3p0.max_size">20</property>
    <property name="c3p0.timeout">1800</property>
    <property name="c3p0.max_statements">50</property>
    <!-- no "connection.pool_size" entry! -->

然后创建一个文件“c3p0.properties”,该文件必须位于类路径的根(即无法为应用程序的特定部分覆盖它):

c3p0.properties

c3p0.testConnectionOnCheckout=true

如果此解决方案不起作用,那么还有更多可能的解决方案:-

1. Add

<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

 Also dont forget to place the c3p0-0.9.1.jar in the classpath. 



    2. Instead of using that c3p0.properties file, couldn't you just use this property in your hibernate.cfg.xml:

<property name="hibernate.c3p0.validate">true</property>

    Also checkout the last post on this page:

    https://forum.hibernate.org/viewtopic.php?p=2399313

    If all these not work than go [more][1] and read in detail


  [1]: http://hibernatedb.blogspot.in/2009/05/automatic-reconnect-from-hibernate-to.html

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”:

  <!-- hibernate.cfg.xml -->
    <property name="c3p0.min_size">5</property>
    <property name="c3p0.max_size">20</property>
    <property name="c3p0.timeout">1800</property>
    <property name="c3p0.max_statements">50</property>
    <!-- no "connection.pool_size" entry! -->

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:-

1. Add

<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>

 Also dont forget to place the c3p0-0.9.1.jar in the classpath. 



    2. Instead of using that c3p0.properties file, couldn't you just use this property in your hibernate.cfg.xml:

<property name="hibernate.c3p0.validate">true</property>

    Also checkout the last post on this page:

    https://forum.hibernate.org/viewtopic.php?p=2399313

    If all these not work than go [more][1] and read in detail


  [1]: http://hibernatedb.blogspot.in/2009/05/automatic-reconnect-from-hibernate-to.html
挽容 2024-08-24 16:53:04

其他答案是更好的长期解决方案。但如果您只需要立即重新运行 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 then restart 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 the terminal, then type shutdown.sh, then type startup.sh. Wait a few moments for the startup sequence to complete, then your apps will work again for a while.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文