数据库休眠并导致异常
Posted
技术标签:
【中文标题】数据库休眠并导致异常【英文标题】:Database sleeping and causing exception 【发布时间】:2014-10-25 02:10:34 【问题描述】:当我有一段时间没有进行任何数据库查询时,我认为它必须休眠 - 如果您随后调用它,则会引发异常;
java.sql.SQLNonTransientConnectionException: Could not read resultset: unexpected end of stream, read 0bytes from 4
at org.mariadb.jdbc.internal.SQLExceptionMapper.get(SQLExceptionMapper.java:136)
at org.mariadb.jdbc.internal.SQLExceptionMapper.throwException(SQLExceptionMapper.java:106)
at org.mariadb.jdbc.mysqlStatement.executeQueryEpilog(MySQLStatement.java:264)
at org.mariadb.jdbc.MySQLStatement.execute(MySQLStatement.java:288)
at org.mariadb.jdbc.MySQLStatement.executeUpdate(MySQLStatement.java:317)
at org.mariadb.jdbc.MySQLPreparedStatement.executeUpdate(MySQLPreparedStatement.java:156)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at com.swifte.database.DBFactory.updateTimeForUser(DBFactory.java:302)
at com.swifte.database.DBFactory.addInsertToDB(DBFactory.java:275)
at com.swifte.service.ExposedFunctions.login(ExposedFunctions.java:135)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:167)
at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:269)
at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:227)
at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:216)
at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:542)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:524)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:126)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:231)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149)
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:926)
at java.lang.Thread.run(Thread.java:745)
有谁知道如何阻止这种情况发生 - 我已尝试将自动连接属性添加到 URL,但它似乎仍然会发生
谢谢
【问题讨论】:
嗨,Biscuit,您是否找到了解决方案或有关此现象的更多信息? jira.mariadb.org/browse/CONJ-348 当您遇到该异常时,请重新连接。可能是超时;可能是网络故障。 【参考方案1】:这可能会延长你的记忆力,但可能
一段时间
已经 8 个多小时了?如果是这样,很可能是MySQL inactivity timeout,它会在 28800 秒后断开连接。我最近遇到了这个问题,发现了一些关于它的 SO 问题 - this answer 包含解决问题的最佳简单块。简而言之,建议在 DBCP 连接池属性中添加 SELECT 1
的 validationQuery
并设置 testOnBorrow = true
。
【讨论】:
以上是关于数据库休眠并导致异常的主要内容,如果未能解决你的问题,请参考以下文章
休眠并在 CockroachDB 中使用 @GeneratedValue 会导致 SQLGrammarException