解决mysql自动断掉连接的问题

这两周做了个手机的后台程序,数据库为MySQL5.0,持久层使用Hibernate 3.0,没有使用额外的连接池,那么Hibernate会默认使用它自带的一个默认连接池,也就是DriverManagerConnectionProvider部署到服务器上,没发现什么问题。等到第二天,同事调用接口时,提示错误,但是重启tomcat后问题就解决了。前两天没有时间,今天又出现问题了,问题如下图所示.

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.hibernate.exception.JDBCConnectionException: could not execute query
	org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:97)
	org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
	org.hibernate.loader.Loader.doList(Loader.java:2235)
	org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
	org.hibernate.loader.Loader.list(Loader.java:2124)
	org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
	org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
	org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
	org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
	org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
	com.swift.softwarestore.service.IConDownloadAction.doPost(IConDownloadAction.java:100)
	com.swift.softwarestore.service.IConDownloadAction.doGet(IConDownloadAction.java:76)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after connection closed.Connection was implicitly closed due to underlying exception/error:


** BEGIN NESTED EXCEPTION ** 

com.mysql.jdbc.CommunicationsException
MESSAGE: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: 断开的管道

STACKTRACE:

java.net.SocketException: 断开的管道
	at java.net.SocketOutputStream.socketWrite0(Native Method)
	at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
	at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
	at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
	at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
	at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2744)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1612)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
	at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
	at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1467)
	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 com.swift.softwarestore.service.SoftwareInfoAction.doprint(SoftwareInfoAction.java:146)
	at com.swift.softwarestore.service.SoftwareInfoAction.doPost(SoftwareInfoAction.java:86)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
	at java.lang.Thread.run(Thread.java:722)


** END NESTED EXCEPTION **



Last packet sent to the server was 1 ms ago.

STACKTRACE:

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: 断开的管道

STACKTRACE:

java.net.SocketException: 断开的管道
	at java.net.SocketOutputStream.socketWrite0(Native Method)
	at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
	at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
	at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
	at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
	at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2744)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1612)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
	at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
	at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1467)
	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 com.swift.softwarestore.service.SoftwareInfoAction.doprint(SoftwareInfoAction.java:146)
	at com.swift.softwarestore.service.SoftwareInfoAction.doPost(SoftwareInfoAction.java:86)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
	at java.lang.Thread.run(Thread.java:722)


** END NESTED EXCEPTION **



Last packet sent to the server was 1 ms ago.
	at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2759)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1612)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
	at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
	at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1467)
	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 com.swift.softwarestore.service.SoftwareInfoAction.doprint(SoftwareInfoAction.java:146)
	at com.swift.softwarestore.service.SoftwareInfoAction.doPost(SoftwareInfoAction.java:86)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
	at java.lang.Thread.run(Thread.java:722)


** END NESTED EXCEPTION **


	com.mysql.jdbc.SQLError.createSQLException(SQLError.java:888)
	com.mysql.jdbc.Connection.checkClosed(Connection.java:1932)
	com.mysql.jdbc.Connection.prepareStatement(Connection.java:4768)
	com.mysql.jdbc.Connection.prepareStatement(Connection.java:4734)
	org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
	org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
	org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
	org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1577)
	org.hibernate.loader.Loader.doQuery(Loader.java:696)
	org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
	org.hibernate.loader.Loader.doList(Loader.java:2232)
	org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
	org.hibernate.loader.Loader.list(Loader.java:2124)
	org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
	org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
	org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
	org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
	org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
	com.swift.softwarestore.service.IConDownloadAction.doPost(IConDownloadAction.java:100)
	com.swift.softwarestore.service.IConDownloadAction.doGet(IConDownloadAction.java:76)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs.

在网上查了查,说是需要修改mysql的配置文件my.cnf.原因是mysql默认8个小时内无请求就断掉连接。需要在mysqld后添加wait_timeout=172800,interactive_timeout=172800这两行配置参数,但是后来发现这样也不能除根啊。

在网上找的解决方案:

当时也搞不清楚为什么,但重启Tomcat后就一切回复正常了,但是又过了一天,问题再次出现。仔细分析错误信息,问题肯定出在数据库连接部分,在网上查阅大量信息后,明确了问题的原因:MySQL对所有连接的有效时间默认为28800秒,正好8小时,也就是说,如果一个连接8小时没有请求和操作,就会自动断开;但是对于Hibernate来说,它的连接池并不知道它所管理的连接中是否有被MySQL断开的。如果一个程序要使用数据库连接,而Hibernte的连接池分配一个已经被MySQL断开了的给程序使用,那么便会出现错误。

  为了证实确实是这个错误,我在本机上做了如下测试:首先启动Tomcat,网站能够正常打开;然后修改系统时间,往后调1天;然后再打开网站,同样的问题果然出现!MySQL + Hibernate架构相当普遍,所以这个问题也相当普遍,若读者也有这样的项目,建议做一下同样的测试,看看是否存在此问题!

  问题找到了,怎么解决呢?
  思路1:增大MySQL的连接有效时间;
  思路2:从程序自身出发,修改连接池的相关参数。
  显然,思路1是不可行的,一方面这个思路不可能保证这个问题绝对的解决,如果无访问时间够长,还是大于MySQL的连接有效时间,问题同样会发生;另一方面,一般情况下,我们都是将程序部署在别人服务器上,我们不可能去修改MySQL配置。那么只能从思路2入手了。
  经查资料发现,Hibernate支持如下的连接池:
   DriverManagerConnectionProvider:代表由Hibernate提供的默认的数据库连接池
   C3P0ConnectionProvider:代表C3P0连接池
   ProxoolConnectionProvider:代表Proxool连接池
   DBCPConnectionProvider:代表DBCP连接池
   DatasourceConnectionProvider:代表在受管理环境中由容器提供的数据源
  其中,默认连接池并不支持在分配一个连接时,测试其有效与否的功能,而C3P0ProxoolDBCP都提供了这样的功能,正好可以解决上述问题。综合考虑各个连接池的效率、稳定性、易用性,决定换用Proxool,它确实在各方面表现优良,方便配置。
  下面就看看如何配置Proxool
  1Hibernate配置文件:

<session-factory>
<property name="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property>
<property name="hibernate.proxool.xml">proxool.xml</property>
<property name="hibernate.proxool.pool_alias">mysql</property>

<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<mapping resource="com/lab1000/jcom/pojo/Admin.hbm.xml" />
...
</session-factory>

  其中各属性含义如下:
hibernate.connection.provider_class:指明使用Proxool连接池
hibernate.proxool.xml:指明Proxool配置文件所在位置,这里与Hibernate的配置文件在同一目录下
hibernate.proxool.pool_alias:指明要使用的proxool.xml中定义的proxool别名。

  2Proxool配置文件(proxool.xml):

<?xml version="1.0" encoding="UTF-8"?>
 <!-- the proxool configuration can be embedded within your own application's.
 Anything outside the "proxool" tag is ignored. -->
 <something-else-entirely>
 <proxool>

<!-- proxool别名 -->
<alias>mysql</alias>

<!-- 数据库连接Url -->
<driver-url>
 jdbc:mysql://localhost/yourDatebase?useUnicode=true&characterEncoding=UTF-8
</driver-url>

<!-- JDBC驱动名称 -->
<driver-class>com.mysql.jdbc.Driver</driver-class>

<!-- 数据库连接帐号 -->
<driver-properties>
 <property name="user" value="root" />
 <property name="password" value="password" />
</driver-properties>

<!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁 -->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>

<!-- 指因未有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的用户连接就不会被接受 -->
 <maximum-new-connections>20</maximum-new-connections>

<!-- 最少保持的空闲连接数 -->
<prototype-count>3</prototype-count>

<!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定 -->
<maximum-connection-count>20</maximum-connection-count>

<!-- 最小连接数 -->
<minimum-connection-count>3</minimum-connection-count>

<!-- 在分配连接前后是否进行有效性测试,这个是解决本问题的关键 -->
<test-before-use>true</test-before-use>
<test-after-use>true</test-after-use>

<!-- 用于测试的SQL语句 -->
<house-keeping-test-sql>SELECT CURRENT_USER</house-keeping-test-sql>

</proxool>
 </something-else-entirely>

  3、下载和安装Proxool的包文件
  下载地址:http://proxool.sourceforge.net/download.html
  下载后并解压后,将其中lib文件夹下的jar文件拷贝到你站点的WEB-INF/lib

  自此,Proxool配置成功。重新启动Tomcat,再次做上述测试,问题解决。
  此外,如果要使用C3P0DHCP,可以参考以下资料:
  http://blog.csdn.net/lip8654/archive/2008/02/26/2121387.aspx
  http://azi.javaeye.com/blog/182146
  http://fishyych.javaeye.com/blog/90793

其实还有一种解决方案,就是

修改如下JDBC连接的 URL: 

jdbc:mysql://hostaddress:3306/schemaname?autoReconnect=true 

//jdbc:mysql://192.168.1.110:3306/st_softwarestore_db?autoReconnect=true 

添加 autoReconnect=true 这个参数,即能解决这个问题。 


©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页