使用hibernate连接数据库,服务器运行时间长了后就会出现
org.apache.jasper.JasperException: could not execute query
java.net.SocketException: Broken pipe
这写异常,重新启动tomcat后正常但是时间长还是会出问题
原因
1. Mysql在经过8小时不使用后会自动关闭已打开的连接
I have a servlet/application that works fine for a day, and then stops working overnight
MySQL closes connections after 8 hours of inactivity. You either need to use a connection pool that handles stale connections or use the "autoReconnect" parameter (see "Developing Applications with MySQL Connector/J").
Also, you should be catching SQLExceptions in your application and dealing with them, rather than propagating them all the way until your application exits, this is just good programming practice. MySQL Connector/J will set the SQLState (see java.sql.SQLException.getSQLState() in your APIDOCS) to "08S01" when it encounters network-connectivity issues during the processing of a query. Your application code should then attempt to re-connect to MySQL at this point.
2. Should the driver try to re-establish stale or dead connections? If enabled the driver will throw an exception for a queries issued on a stale or dead connection, which belong to the current transaction, but will attempt reconnect before the next query issued on the connection in a new transaction. The use of this feature is not recommended, because it has side effects related to session state and data consistency when applications don'thandle SQLExceptions properly, and is only designed to be used when you are unable to configure your application to handle SQLExceptions resulting from dead andstale connections properly. Alternatively, investigate setting the MySQL wait_timeout system variable to some high value rather than the default of 8 hours.
链接
http://dev.mysql.com/doc/refman/5.0/en/cj-configuration-properties.html
好象用了autoReconnect以后会自动重新连接的
虽然第一次执行失败了,
但是以后会自动重新连接的
3. 在这个页面找到了第三种方法,即不使用Hibernate内置的连接池(Hibernate强烈推荐不使用但我以前一直在用),改用C3P0连接池,这个连接池会自动处理数据库连接被关闭的情况。要使用C3P0很简单,先从Hibernate里把c3p0-0.8.3.jar复制到项目的lib目录中,再在 hibernate.properties里去掉hibernate.c3p0开头的那些属性的注释(使用缺省值或自己需要的数值),这样 Hibernate就会自动使用C3P0代替内置的连接池了。到目前为止前面的问题没有再出现过。
以前对Hibernate警告不要使用内置连接池作产品用途没有太放在心上,这次是一个教训,所以不论从稳定还是性能的考虑,都应该选择相对更加成熟的连接池。
4. 在服务器上过期的‘Wait—timeout’基本上是服务器给与的强制性网络故障。你通过把‘Wait—timeout’设置的高点儿就可以改正它,尽管如此,作为一个开发者,你的代码应该包含相关的异常处理并采取适当的恢复措施,不要都把它们传到调用堆中。
在转贴一下在hibernate中设置c3p0数据源的方法,原文地址:http://www.lifevv.com/tenyo/doc/20070605102040991.html
内容如下:
c3p0为open source的JDBC连接池,随hibernate一起发布。本文介绍了怎么在hibernate里配置使用c3p0。
c3p0连接池的配置非常简单,只需要在hibernate.cfg.xml里增加:
<!-- configuration pool -->
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">100</property> <!-- seconds -->
<property name="c3p0.max_size">5</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.min_size">2</property>
<property name="c3p0.timeout">90</property> <!-- seconds -->
另外,还需要在CLASS_PATH里加上c3p0-x.x.x.jar文件(x.x.x为版本号),c3p0-x.x.x.jar文件随hibernate一起发布,你可以在其lib目录下找到该文件。
配置好之后,hibernate便会自动使用c3p0的连接池:C3P0ConnectionProvider
换个连接池试试看会不会发生了
分享到:
相关推荐
但这个连接池性能不佳,且存在诸多BUG(笔者就曾在Mysql环境下被八小时连接悬空问题困扰过),因此官方也只是建议仅在开发环境下使用。 三、Hibernate2和Hibernate3的命名空间有所变化。例如,配置C3P0时的provider...
hibernate 连接池配置详解,四种连接方式:第一种方案,Hibernate默认连接池;第二种方案是:C3P0;第三种方案是:dbcp;第四种方案是:Proxool (推荐)
Hibernate连接池的三种配置方式......
java hibernate 的连接池简单介绍.不错的源代码,链接数据库的代码,可以下载试试
hibernate连接池配置的三种方式。
在Hibernate连接池配置时需要注意的有三点: 一、Apche的DBCP在Hibernate2中受支持,但在Hibernate3中已经不再推荐使用,官方的解释是这个连接池存在缺陷。如果你因为某种原因需要在Hibernate3中使用DBCP,建议采用...
hibernate配置连接池大全 hibernate配置连接池大全
hibernate连接池驱动程序 c3p0 proxool
包括了四种常用连接池的配置以及对应的支持包
整合网络上关于数据库连接池的资料,结合hibernate对常用数据库连接池进行整合
介绍了Hibernate中几种连接池以及他们怎么使用
Hibernate2和Hibernate3连接池配置.doc
Hibernate连接池的各种配置,主要针对proxool的设置
Hibernate的三种连接池设置C3P0、Proxool和DBCP. 详细说明及配置方法 Hibernate支持第三方的连接池,官方推荐的连接池是C3P0,Proxool,以及DBCP.
hibernate的学习资料,以及连接池和二级缓存
hibernate配置数据库连接池的三种方法(数据库连接池的概念)
不用说就知道这是干什么的吧hibernate使用c3p0连接池的资料
资源包括hibernate Sql server2005连接池配置源码,及sqlserverjdbc.jar jTDS3.jar jtds-1.2.jar ojdbc14.jar proxool-0.9.0RC3.jar 等jar包,希望能帮大家一些忙