`
shazhifeng
  • 浏览: 125196 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

MySQL+Hibernate下连接空闲8小时自动断开问题解决方案

阅读更多

 前段时间刚完成一个家教网项目,数据库为MySQL5.0,持久层使用Hibernate 3.1,没有使用额外的连接池,那么Hibernate会默认使用它自带的一个默认连接池,也就是DriverManagerConnectionProvider。

  先在本机上调试都毫无问题,于是部署到服务器上,也都没什么问题。由于这是新网站,根本还没正式对外发布和宣传,所以头两天根本没人访问。等到第二天,我再次访问网站时,问题就出现了,错误信息如下:

root cause

javax.servlet.ServletException: org.hibernate.exception.JDBCConnectionException: could not execute query org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535) org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433) org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414) javax.servlet.http.HttpServlet.service(HttpServlet.java:690) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jsp.index_jsp._jspService(index_jsp.java:57) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) com.lab1000.jcom.system.filter.RequestEncodingFilter.doFilter(RequestEncodingFilter.java:30)

root cause

org.hibernate.exception.JDBCConnectionException: could not execute query org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74) org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) org.hibernate.loader.Loader.doList(Loader.java:2148) org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029) org.hibernate.loader.Loader.list(Loader.java:2024) org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94) org.hibernate.impl.SessionImpl.list(SessionImpl.java:1533) org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283) com.lab1000.jcom.dao.ArticleDAO.getArticlesUnder(ArticleDAO.java:163) com.lab1000.jcom.struts.action.HomeAction.execute(HomeAction.java:40) org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414) javax.servlet.http.HttpServlet.service(HttpServlet.java:690) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jsp.index_jsp._jspService(index_jsp.java:57) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) com.lab1000.jcom.system.filter.RequestEncodingFilter.doFilter(RequestEncodingFilter.java:30)

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.CommunicationsExceptionMESSAGE: Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** java.io.EOFExceptionSTACKTRACE:java.io.EOFException at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1963) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2375) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2874) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715) at com.mysql.jdbc.Connection.execSQL(Connection.java:3249) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1403) at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139) at org.hibernate.loader.Loader.getResultSet(Loader.java:1669) at org.hibernate.loader.Loader.doQuery(Loader.java:662) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224) at org.hibernate.loader.Loader.doList(Loader.java:2145) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029) at org.hibernate.loader.Loader.list(Loader.java:2024) at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1533) at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283) at com.lab1000.jcom.dao.ArticleDAO.getArticlesUnder(ArticleDAO.java:163) at com.lab1000.jcom.struts.action.HomeAction.execute(HomeAction.java:40) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414) at javax.servlet.http.HttpServlet.service(HttpServlet.java:690) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302) at org.apache.jsp.index_jsp._jspService(index_jsp.java:57) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at com.lab1000.jcom.system.filter.RequestEncodingFilter.doFilter(RequestEncodingFilter.java:30) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286) at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190) at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:283) at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:767) at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:697) at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:889) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690) at java.lang.Thread.run(Unknown Source)** END NESTED EXCEPTION **Last packet sent to the server was 15 ms ago.STACKTRACE:com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** java.io.EOFExceptionSTACKTRACE:java.io.EOFException at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1963) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2375) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2874) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715) at com.mysql.jdbc.Connection.execSQL(Connection.java:3249) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1403) at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139) at org.hibernate.loader.Loader.getResultSet(Loader.java:1669) at org.hibernate.loader.Loader.doQuery(Loader.java:662) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224) at org.hibernate.loader.Loader.doList(Loader.java:2145) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029) at org.hibernate.loader.Loader.list(Loader.java:2024) at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1533) at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283) at com.lab1000.jcom.dao.ArticleDAO.getArticlesUnder(ArticleDAO.java:163) at com.lab1000.jcom.struts.action.HomeAction.execute(HomeAction.java:40) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414) at javax.servlet.http.HttpServlet.service(HttpServlet.java:690) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302) at org.apache.jsp.index_jsp._jspService(index_jsp.java:57) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at com.lab1000.jcom.system.filter.RequestEncodingFilter.doFilter(RequestEncodingFilter.java:30) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286) at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190) at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:283) at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:767) at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:697) at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:889) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690) at java.lang.Thread.run(Unknown Source)** END NESTED EXCEPTION **Last packet sent to the server was 15 ms ago. at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2586) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2874) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715) at com.mysql.jdbc.Connection.execSQL(Connection.java:3249) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1403) at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139) at org.hibernate.loader.Loader.getResultSet(Loader.java:1669) at org.hibernate.loader.Loader.doQuery(Loader.java:662) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224) at org.hibernate.loader.Loader.doList(Loader.java:2145) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029) at org.hibernate.loader.Loader.list(Loader.java:2024) at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1533) at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283) at com.lab1000.jcom.dao.ArticleDAO.getArticlesUnder(ArticleDAO.java:163) at com.lab1000.jcom.struts.action.HomeAction.execute(HomeAction.java:40) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414) at javax.servlet.http.HttpServlet.service(HttpServlet.java:690) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630) at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436) at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374) at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302) at org.apache.jsp.index_jsp._jspService(index_jsp.java:57) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at com.lab1000.jcom.system.filter.RequestEncodingFilter.doFilter(RequestEncodingFilter.java:30) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286) at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190) at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:283) at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:767) at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:697) at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:889) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690) at java.lang.Thread.run(Unknown Source)** END NESTED EXCEPTION ** com.mysql.jdbc.SQLError.createSQLException(SQLError.java:888) com.mysql.jdbc.Connection.checkClosed(Connection.java:1931) com.mysql.jdbc.Connection.prepareStatement(Connection.java:4705) com.mysql.jdbc.Connection.prepareStatement(Connection.java:4671) org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:442) org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:368) org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:105) org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1561) org.hibernate.loader.Loader.doQuery(Loader.java:661) org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224) org.hibernate.loader.Loader.doList(Loader.java:2145) org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029) org.hibernate.loader.Loader.list(Loader.java:2024) org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94) org.hibernate.impl.SessionImpl.list(SessionImpl.java:1533) org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283) com.lab1000.jcom.dao.ArticleDAO.getArticlesUnder(ArticleDAO.java:163) com.lab1000.jcom.struts.action.HomeAction.execute(HomeAction.java:40) org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431) org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236) org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196) org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414) javax.servlet.http.HttpServlet.service(HttpServlet.java:690) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jsp.index_jsp._jspService(index_jsp.java:57) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) com.lab1000.jcom.system.filter.RequestEncodingFilter.doFilter(RequestEncodingFilter.java:30)

  当时也搞不清楚为什么,但重启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:代表在受管理环境中由容器提供的数据源

  其中,默认连接池并不支持在分配一个连接时,测试其有效与否的功能,而C3P0、Proxool、DBCP都提供了这样的功能,正好可以解决上述问题。综合考虑各个连接池的效率、稳定性、易用性,决定换用Proxool,它确实在各方面表现优良,方便配置。

  下面就看看如何配置Proxool:

  1、Hibernate配置文件:

<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别名。

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

<!---->

<!---->

<something-else-entirely>

<proxool>

<!---->

<alias>mysql</alias>

<!---->

<driver-url>

jdbc:mysql://localhost/yourDatebase?useUnicode=true&characterEncoding=UTF-8

</driver-url>

<!---->

<driver-class>com.mysql.jdbc.Driver</driver-class>

<!---->

<driver-properties>

<property name="user" value="root">

<property name="password" value="password">

</driver-properties>

<!---->

<house-keeping-sleep-time>90000</house-keeping-sleep-time>

<!---->

<maximum-new-connections>20</maximum-new-connections>

<!---->

<prototype-count>3</prototype-count>

<!---->

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

<!---->

<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,再次做上述测试,问题解决。

  此外,如果要使用C3P0或DHCP,可以参考以下资料:

  http://blog.csdn.net/lip8654/archive/2008/02/26/2121387.aspx

  http://azi.iteye.com/blog/182146

  http://fishyych.iteye.com/blog/90793

分享到:
评论

相关推荐

    MySQL+Hibernate下连接空闲8小时自动断开问题解决方案.pdf

    解决 MySQL+Hibernate 连接空闲8小时自动断开问题 概述: 在使用 MySQL 和 Hibernate 进行数据库持久层开发时,可能会遇到连接空闲8小时自动断开的问题。本文将讨论该问题的解决方案,并对相关的知识点进行详细...

    MySQL+Hibernate下连接空闲8小时自动断开问题解决方案.docx

    MySQL+Hibernate 连接空闲8小时自动断开问题解决方案 知识点1: Hibernate 的连接池机制 在 Hibernate 中,默认情况下 使用的是 DriverManagerConnectionProvider,这是一个简单的连接池机制。它将打开的连接缓存在...

    hibernate+proxool解决mysql8小时问题(附带jar包)

    MySQL数据库在长时间无操作的情况下,为了节省资源,会自动关闭闲置的连接,这通常被称为“8小时问题”。为了解决这个问题,开发者通常需要采取特定的策略,比如使用连接池。在本案例中,我们看到一种解决方案,即...

    hibernate与连接池

    它能够有效地管理预定义好的JDBC连接,并且能够根据应用的要求自动地创建和断开这些连接。 #### Hibernate与C3P0的结合 Hibernate与C3P0结合使用可以极大地提高应用效率。通过C3P0,可以管理Hibernate使用的数据库...

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

    当应用程序遇到这种问题时,一种常见的解决方案是在数据库连接字符串中添加"autoReconnect=true"选项,试图在断开连接后自动重新连接。然而,对于MySQL 4.0之前的版本,这种方法可能有效,但在较新的MySQL版本中,...

    Tomcat6连接池配置详解(自动重连)

    此时,Tomcat将能够正确地管理数据库连接,并在连接断开时自动尝试重新建立连接,大大提高了应用的稳定性和可用性。 #### 八、总结 通过上述步骤,我们详细介绍了如何在Tomcat6中配置连接池,并实现了数据库连接的...

    JDBC实现Mysql自动重连机制的方法详解

    默认情况下,如果一个连接在8小时内未有任何活动,MySQL会自动关闭这个连接。为了解决这个问题,我们可以采取以下几种方法: 1. **配置URL参数**:如果你不使用任何连接池,可以在JDBC连接URL中添加`autoReconnect=...

    c3p0连接池jar包

    5. **异常处理**:C3P0提供了强大的异常处理机制,当数据库连接出现问题时,如超时、断开等,它会自动尝试恢复连接,或者在无法恢复时关闭有问题的连接。 6. **JNDI集成**:C3P0可以方便地与Java Naming and ...

    c3p0 jar 包

    3. **自动重试**:如果数据库连接因某种原因断开,c3p0 将尝试自动重连,提供一定的容错能力。 4. **空闲连接检测与回收**:定期检查并回收空闲时间过长的连接,避免长时间未使用的连接占用资源。 5. **线程安全**...

    SpringJDBC笔记

    此外,还可以考虑其他高性能的数据源解决方案,例如 HikariCP 等。 #### 四、总结 Spring JDBC 不仅简化了对 JDBC 的使用,还通过集成 Apache DBCP 和 C3P0 两种数据源实现提供了丰富的配置选项。开发人员可以根据...

    c3p0所需jar包

    2. **自动检测和回收**:c3p0可以定期检测连接的有效性,如果发现连接已断开,会自动关闭并重新创建新的连接。同时,它还可以根据预设的策略回收长时间未使用的空闲连接。 3. **连接池参数自定义**:通过`c3p0-...

Global site tag (gtag.js) - Google Analytics