关于MySQL的wait_timeout连接超时(BEGIN NESTED EXCEPTION )

BEGIN NESTED EXCEPTION

在说这个错误之前先说明我的项目是通过Hibernate来进行数据库操作的

关于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>下面一行配,参照官网)

<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>   
        <!--连接池的最小连接数-->     
        <property name="hibernate.c3p0.min_size">5</property>   
        <!--最大连接数-->     
        <property name="hibernate.c3p0.max_size">30</property>   
        <!--连接超时时间-->   
        <property name="hibernate.c3p0.timeout">1800</property>   
        <!--statemnets缓存大小-->     
        <property name="hibernate.c3p0.max_statements">100</property>   
        <!--每隔多少秒检测连接是否可正常使用  -->     
        <property name="hibernate.c3p0.idle_test_period">121</property>   
        <!--当池中的连接耗尽的时候,一次性增加的连接数量,默认为3-->     
        <property name="hibernate.c3p0.acquire_increment">1</property>   
        <property name="hibernate.c3p0.validate">true</property> 

阅读更多
换一批

BEGIN NESTED EXCEPTION 问题,高手指教!

11-22

这几天网页不时的出现下边的问题,这是怎么回事,高手指教!链接信息和错误信息如下:rnClass.forName("org.gjt.mm.mysql.Driver").newInstance();rnString url ="jdbc:mysql://"+server+":"+port+"/"+dbname+"?user="+user+"&password="+pass+"&useUnicode=true&characterEncoding=gbk";rnconn= DriverManager.getConnection(url); rnrnrnrnrn500 Servlet Exceptionrncom.mysql.jdbc.CommunicationsException: Communications link failure duernto underlying exception: rnrn** BEGIN NESTED EXCEPTION ** rnrnjava.net.SocketExceptionrnMESSAGE: Connection resetrnrnSTACKTRACE:rnrnjava.net.SocketException: Connection resetrn at java.net.SocketInputStream.read(SocketInputStream.java:168)rn at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:113)rn at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:160)rn at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:188)rn at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1931)rn at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2380)rn at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2909)rn at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1600)rn at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1695)rn at com.mysql.jdbc.Connection.execSQL(Connection.java:3020)rn at com.mysql.jdbc.Connection.setAutoCommit(Connection.java:4989)rn at com.mysql.jdbc.Connection.initializePropsFromServer(Connection.java:3854)rn at com.mysql.jdbc.Connection.createNewIO(Connection.java:2585)rn at com.mysql.jdbc.Connection.(Connection.java:1485)rn at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)rn at java.sql.DriverManager.getConnection(DriverManager.java:512)rn at java.sql.DriverManager.getConnection(DriverManager.java:193)rn at _jsp._content__jsp._jspService(_content__jsp.java:62)rn at com.caucho.jsp.JavaPage.service(JavaPage.java:60)rn at com.caucho.jsp.Page.pageservice(Page.java:570)rn at com.caucho.server.dispatch.PageFilterChain.doFilter(PageFilterChain.java:175)rn at com.caucho.server.webapp.DispatchFilterChain.doFilter(DispatchFilterChain.java:115)rn at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:229)rn at com.caucho.server.webapp.RequestDispatcherImpl.include(RequestDispatcherImpl.java:485)rn at com.caucho.server.webapp.RequestDispatcherImpl.include(RequestDispatcherImpl.java:350)rn at com.caucho.jsp.PageContextImpl.include(PageContextImpl.java:926)rn at _jsp._index__jsp._jspService(_index__jsp.java:33)rn at com.caucho.jsp.JavaPage.service(JavaPage.java:60)rn at com.caucho.jsp.Page.pageservice(Page.java:570)rn at com.caucho.server.dispatch.PageFilterChain.doFilter(PageFilterChain.java:175)rn at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:178)rn at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:229)rn at com.caucho.server.hmux.HmuxRequest.handleRequest(HmuxRequest.java:419)rn at com.caucho.server.port.TcpConnection.run(TcpConnection.java:389)rn at com.caucho.util.ThreadPool.runTasks(ThreadPool.java:507)rn at com.caucho.util.ThreadPool.run(ThreadPool.java:433)rn at java.lang.Thread.run(Thread.java:534)rnrnrn** END NESTED EXCEPTION **rnrnrnrnLast packet sent to the server was 40 ms ago.rn at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2592)rn at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2909)rn at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1600)rn at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1695)rn at com.mysql.jdbc.Connection.execSQL(Connection.java:3020)rn at com.mysql.jdbc.Connection.setAutoCommit(Connection.java:4989)rn at com.mysql.jdbc.Connection.initializePropsFromServer(Connection.java:3854)rn at com.mysql.jdbc.Connection.createNewIO(Connection.java:2585)rn at com.mysql.jdbc.Connection.(Connection.java:1485)rn at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)rn at java.sql.DriverManager.getConnection(DriverManager.java:512)rn at java.sql.DriverManager.getConnection(DriverManager.java:193)rn at _jsp._content__jsp._jspService(content.jsp:36)rn at com.caucho.jsp.JavaPage.service(JavaPage.java:60)rn at com.caucho.jsp.Page.pageservice(Page.java:570)rn at com.caucho.server.dispatch.PageFilterChain.doFilter(PageFilterChain.java:175)rn at com.caucho.server.webapp.DispatchFilterChain.doFilter(DispatchFilterChain.java:115)rn at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:229)rn at com.caucho.server.webapp.RequestDispatcherImpl.include(RequestDispatcherImpl.java:485)rn at com.caucho.server.webapp.RequestDispatcherImpl.include(RequestDispatcherImpl.java:350)rn at com.caucho.jsp.PageContextImpl.include(PageContextImpl.java:926)rn at _jsp._index__jsp._jspService(index.jsp:4)rn at com.caucho.jsp.JavaPage.service(JavaPage.java:60)rn at com.caucho.jsp.Page.pageservice(Page.java:570)rn at com.caucho.server.dispatch.PageFilterChain.doFilter(PageFilterChain.java:175)rn at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:178)rn at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:229)rn at com.caucho.server.hmux.HmuxRequest.handleRequest(HmuxRequest.java:419)rn at com.caucho.server.port.TcpConnection.run(TcpConnection.java:389)rn at com.caucho.util.ThreadPool.runTasks(ThreadPool.java:507)rn at com.caucho.util.ThreadPool.run(ThreadPool.java:433)rn at java.lang.Thread.run(Thread.java:534)rnrn

没有更多推荐了,返回首页