与MySql的连接正在自动中止。如何正确配置Connector / J? [英] Connection with MySql is being aborted automatically. How to configure Connector/J properly?

查看:246
本文介绍了与MySql的连接正在自动中止。如何正确配置Connector / J?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I read this advice from error message:


你应该考虑到期
和/或测试连接有效性$ b在您的应用程序中使用之前$ b,
增加服务器配置客户端超时的
值,或使用
Connector / J连接属性
'autoconnect = true'以避免此
问题。

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.

我正在使用Spring和JPA。我应该在哪里配置Connector / J? (在 persistence.xml 中,或在 entityManagerFactory 弹簧配置中,或在 dateSource 弹簧配置,还是其他地方?)

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?)

推荐答案

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

The text describes three solutions to prevent connection aborts:


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

  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"


  • 增加超时时间。这通常是数据库的属性。您可以增加此值以查看是否减少了连接中止。

  • Increase the timeout. This is normally a property of the database. You can increase this value to see if you get less connection abort.

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

    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.

    • Connection pooling options with JDBC: DBCP vs C3P0
    • http://snipplr.com/view/14725/c3p0-datasource-config-connectionpool/
    • http://www.mchange.com/projects/c3p0/index.html#configuring_connection_testing

    附加评论:


    • 数据源/池也可以超时,它对应于空闲连接保留在池中的时间。不要与db超时混淆。

    • 有几种方法可以测试连接的有效性。一种常见的方法是使用虚拟测试表。池将在虚拟测试表上发出一个选择,以查看连接是否仍然正常。

    这篇关于与MySql的连接正在自动中止。如何正确配置Connector / J?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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