`

关于MySQL的wait_timeout连接超时问题报错解决方案

阅读更多

(1)interactive_timeout:
参数含义:服务器关闭交互式连接前等待活动的秒数。交互式客户端定义为在mysql_real_connect()中使用CLIENT_INTERACTIVE选项的客户端。
参数默认值:28800秒(8小时)

(2)wait_timeout:
参数含义:服务器关闭非交互连接之前等待活动的秒数。
在线程启动时,根据全局wait_timeout值或全局interactive_timeout值初始化会话wait_timeout值,取决于客户端类型(由mysql_real_connect()的连接选项CLIENT_INTERACTIVE定义)。

参数默认值:28800秒(8小时)

问题1:这里为什么要同时设置interactive_timeout,wait_timeout的设置才会生效?
答:    不设置interactive_timeout,wait_timeout也会生效。
问题2:interactive的值如果设置的和wait_timeout不同,为什么Interactive_timeout会覆盖wait_timeout?
答:在交互模式下(CLIENT_INTERACTIVE),interactive_timeout才生效,非交互模式下,不生效。

问题3:在进行MySQL优化时,因为interactive_timeout决定的是交互连接的时间长短,而wait_timeout决定的是非交互连接的时间长短。如果在进行连接配置时mysql_real_connect()最后一个参数client_flag不设置为CLIENT_INTERACTIVE,是不是interactive_timeout的值不会覆盖wait_timeout?
答:可以做实验试试。

问题4:为了减少长连接的数量,在设置优化时是不是可以将interactive_timeout的值设置的大些,而wait_timeout的值设置的小些?但是问题2的描述好像又不允许这样。。。

答:如2所述,在交互模式下,interactive_timeout取代wait_timeout。这样,如果有的客户端是交互模式方式连接mysql server。那么客户端的timeout受制于interactive_timeout。如果有的客户端是非交互模式,长连接mysql server。那么客户端的timeout受制于wait_timeout。(是否是交互模式的连接,由客户端决定)

 

 

关于MySQL连接超时问题,估计很多人都遇到过:大致情形都是这样,开发测试时程序都是正常的,一到第二天就出先莫名错误,比如在我的项目中就是定时任务执行,每天凌晨一点执行一次,也就是24小时每隔24小时执行,远远超出了8小时

如果你刚好在数据库超时的第一时间内看到日志记录的话那么,第一次超时发生的错误就是这样的:
ERROR [org.hibernate.util.JDBCExceptionReporter] - Communications link failure

Last packet sent to the server was 0 ms ago.

如果不是第一次超时后执行,以后每次报错就变成嵌套的错误了,就是下面这样:

ERROR [org.hibernate.util.JDBCExceptionReporter] -
 No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:

 


** BEGIN NESTED EXCEPTION **


com.mysql.jdbc.exceptions.jdbc4.CommunicationsException


MESSAGE: The last packet successfully received from the server was86395 milliseconds ago.The last packet sent successfully to the server was 86395 milliseconds ago, which is longer than the server configured value of 'wait_timeout'. 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.
STACKTRACE:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was86395 milliseconds ago.The last packet sent successfully to the server was 86395 milliseconds ago, which  is longer than the server configured value of 'wait_timeout'. 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.
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
 at java.lang.reflect.Constructor.newInstance(Unknown Source)
 at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
 at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
 at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3270)
 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1932)
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
 at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
 at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)
 at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1912)
 at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
 at org.hibernate.loader.Loader.getResultSet(Loader.java:1812)
 at org.hibernate.loader.Loader.doQuery(Loader.java:697)
 at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
 at org.hibernate.loader.Loader.doList(Loader.java:2232)
 at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
 at org.hibernate.loader.Loader.list(Loader.java:2124)
 at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
 at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
 at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
 at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
 at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
 at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:835)
 at com.cn.util.db.TargetRecordDaoImpl.findbyIdAndDate(TargetRecordDaoImpl.java:23)
 at com.cn.util.parser.ExcelOperate.readExcel(ExcelOperate.java:324)
 at com.cn.util.parser.ExcelParser.parser(ExcelParser.java:41)
 at com.cn.util.timer.CRMExcelParserTarger.execute(CRMExcelParserTarger.java:76)
 at org.quartz.core.JobRunShell.run(JobRunShell.java:199)
 at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
Caused by: java.net.SocketException: Software caused connection abort: socket write error
 at java.net.SocketOutputStream.socketWrite0(Native Method)
 at java.net.SocketOutputStream.socketWrite(Unknown Source)
 at java.net.SocketOutputStream.write(Unknown Source)
 at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
 at java.io.BufferedOutputStream.flush(Unknown Source)
 at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3251)
 ... 24 more


** END NESTED EXCEPTION **


具体解释是这样的:Mysql服务器默认的“wait_timeout”是8小时【也就是默认的值默认是28800秒】,也就是说一个connection空闲超过8个小时,Mysql将自动断开该connection,通俗的讲就是一个连接在8小时内没有活动,就会自动断开该连接。
wait timeout的值可以设定,但最多只能是2147483,不能再大了。也就是约24.85天
所以即使你MySQL通过my.ini 在

# The TCP/IP Port the MySQL Server will listen on
port=3306下面
添加
# this is myown dinifition for mysql connection timeout
wait_timeout=31536000
interactive_timeout=31536000
无论超过最大限度多大的数值,只能被MySQL解析为2147483,2147483天后你的程序该出什么错还是什么错,避免不了的
后来发现Hibernate的内置连接池性能是非常的差,还不如直接用第三方的c3p0,改用C3P0连接池,这

个连接池会自动 处理数据库连接被关闭的情况。要使用C3P0很简单,先从Hibernate里把c3p0-0.9.1.jar复

制到项目的lib目录中,再在 hibernate.properties里去掉hibernate.c3p0开头的那些属性的注释(使用缺

省值或自己需要的数值),这样 Hibernate就会自动使用C3P0代替内置的连接池了。到目前为止前面的问题

没有再出现过。
具体在hibernate.cfg.xml中配置如下(在<session-factory>下面一行配,参照官网)

 

Xml代码 复制代码 收藏代码
  1. <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>  
  2.         <!--连接池的最小连接数-->    
  3.         <property name="hibernate.c3p0.min_size">5</property>  
  4.         <!--最大连接数-->    
  5.         <property name="hibernate.c3p0.max_size">30</property>  
  6.         <!--连接超时时间-->  
  7.         <property name="hibernate.c3p0.timeout">1800</property>  
  8.         <!--statemnets缓存大小-->    
  9.         <property name="hibernate.c3p0.max_statements">100</property>  
  10.         <!--每隔多少秒检测连接是否可正常使用  -->    
  11.         <property name="hibernate.c3p0.idle_test_period">121</property>  
  12.         <!--当池中的连接耗尽的时候,一次性增加的连接数量,默认为3-->    
  13.         <property name="hibernate.c3p0.acquire_increment">1</property>  
  14.         <property name="hibernate.c3p0.validate">true</property>
分享到:
评论

相关推荐

    关于MySQL的wait-timeout连接超时问题报错解决方案.pdf

    解决连接超时问题需要从多方面考虑,包括调整 wait_timeout 参数的值、使用 Connector/J 连接属性、及时释放连接等方法。只有通过合适的方法,才能避免连接超时问题的出现,提高系统性能和稳定性。

    关于MySQL的wait-timeout连接超时问题报错解决方案.docx

    MySQL 连接超时问题是常见的错误之一,解决该问题需要了解 wait_timeout 参数的重要性,并正确地使用 show variables 命令。同时,在使用 Hibernate 框架进行数据库操作时,需要注意连接超时问题,并采取相应的解决...

    MySQL wait_timeout连接超时

    MySQL的`wait_timeout`连接超时问题通常出现在长时间无操作的数据库连接上,服务器会自动关闭这些连接以释放资源。这种现象在应用中可能导致突然的数据通信中断,表现为“Communications link failure”等错误,提示...

    MySQL数据库连接超时(Wait_timeout)问题总结.pdf

    总之,MySQL的连接超时问题主要是由`wait_timeout`参数引发的,解决方法包括但不限于调整MySQL服务器配置、使用连接池、更新应用程序代码以处理连接失效的情况,以及在使用ORM框架时,按照框架的文档配置相应的连接...

    MySQL中interactive_timeout和wait_timeout的区别

    在MySQL中,`interactive_timeout` 和 `wait_timeout` 是两个重要的配置参数,它们与客户端连接到服务器的超时设置紧密相关。理解这两个参数的区别对于优化数据库性能和避免不必要的连接断开至关重要。 `...

    mysql提示got timeout reading communication packets的解决方法

    错误提示: user: ‘root’ host: `localhost’ (Got timeout reading communication ...wait_timeout 的默认值这:120 根据情况增加吧. 这两个值是一个全局变量,可以动态增加,如: mysql&gt; set global interactiv

    mysql 8小时空闲后连接超时的问题

    需要注意的是,尽管调整`interactive_timeout`可以解决连接超时问题,但过度增加超时时间可能会导致服务器资源被长时间占用,尤其是在高并发的环境中。因此,优化数据库连接管理和使用连接池是更推荐的做法。 连接...

    javaee mysql空闲关闭

    然而,为了优化资源使用,MySQL配置了一个参数`wait_timeout`,这个参数定义了连接在空闲多长时间后会被自动关闭。当JavaEE应用中的数据库连接在超过这个设定的时间没有执行任何操作,MySQL服务器会断开这个连接,这...

    还原大备份mysql文件失败的解决方法分享

    2. **wait_timeout** 和 **interactive_timeout**: 这两个参数分别定义了非交互式连接和交互式连接在无活动状态后等待多久关闭。在上述例子中,它们都被设置为2880000秒(约80小时)。这有助于防止长时间运行的还原...

    关于mysql提示too many connections问题解决方法.doc

    - `SET GLOBAL wait_timeout=120`:设置非交互式连接的超时时间,单位为秒。如果超过这个时间没有活动,连接将被自动断开。 - `SET GLOBAL interactive_timeout=300`:设置交互式连接的超时时间。与`wait_timeout`...

    解决SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded_runtimeerror怎么修复

    在MySQL数据库操作中,"SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded" 是一个常见的错误,它意味着在执行事务时,系统等待锁定资源的时间超过了预设的限制。这个错误通常发生在并发环境中,当...

    MySQL和连接相关的timeout 的详细整理

    `wait_timeout`和`interactive_timeout`则是关于连接空闲时的超时设置。`wait_timeout`用于非交互式连接,即不进行任何查询操作的连接,如果在指定时间内没有活动,服务器将自动关闭连接。而`interactive_timeout`是...

    解决MySQL 5数据库连接超时问题

    mysql5将其连接的等待时间(wait_timeout)缺省为8小时。怎么不让它超时呢

    SQL查询超时的设置方法(关于timeout的处理)

    通过对`connect_timeout`、`interactive_timeout`、`wait_timeout`等参数的调整,可以更好地控制连接的生命周期,防止因长时间无响应导致的问题,并确保系统能够有效地处理各种查询。在进行调整时,务必根据实际情况...

    Spring Boot如何解决Mysql断连问题

    Spring Boot 解决 Mysql ...解决 Spring Boot 连接 Mysql 断连问题可以使用多种方法,包括在 application.properties 文件中添加配置项、修改 Mysql 的 wait_timeout 参数等。选择合适的方法可以根据实际情况进行选择。

Global site tag (gtag.js) - Google Analytics