Chinaunix首页 | 论坛 | 博客
  • 博客访问: 398085
  • 博文数量: 93
  • 博客积分: 3006
  • 博客等级: 少校
  • 技术积分: 998
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-02 17:23
文章分类

全部博文(93)

文章存档

2011年(12)

2010年(38)

2009年(20)

2008年(23)

分类: Mysql/postgreSQL

2009-06-14 21:31:23

    前段时间刚完成一个招聘网站的项目,数据库为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.CommunicationsException
MESSAGE: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.io.EOFException

STACKTRACE:

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

STACKTRACE:

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)
当时也搞不清楚为什么,但重启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配置文件:


org.hibernate.connection.ProxoolConnectionProvider
proxool.xml
mysql

false
org.hibernate.dialect.MySQLDialect


...

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

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





mysql



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


com.mysql.jdbc.Driver






90000


20


3


20


3


true
true


SELECT CURRENT_USER


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

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

阅读(3767) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~