项目中使用tomcat6.0+mysql5.1,struts+hibernate,其JDBC驱动为3.1.12版本,第一天运行调试一切正常。可是到了第二天,只要一登录就提示"No operations allowed after connection closed"异常。在这种情况下只要重新启动Tomcat就恢复正常,然而到了第二天问题依旧。原来Mysql在经过8小时不使用后会自动关闭已打开的连接。
解决方法之一是不使用Hibernate内置的连接池,改用C3P0连接池,这个连接池会自动处理数据库连接被关闭的情况。要使用C3P0很简单,先从Hibernate里把c3p0-0.8.3.jar复制到项目的lib目录中,再配置Hibernate配置文件。
Hibernate配置文件可以有两种格式,一种是hibernate.properties,另一种是hibernate.cfg.xml,后者稍微方便一些,当增加hbm映射文件的时候,可以直接在hibernate.cfg.xml里面增加,不必像hibernate.properties必须在初始化代码中加入。但不管怎么说,两种的配置项都是一样的。
hibernate.properties中的配置
hibernate.show_sql = true
hibernate.dialect = net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost/HibernateTest
hibernate.connection.username = root
hibernate.connection.password =
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.cfg.xml中的配置
<?xml version='1.0' encoding='big5'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/HibernateTest</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>
<mapping resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
解决方法之一是不使用Hibernate内置的连接池,改用C3P0连接池,这个连接池会自动处理数据库连接被关闭的情况。要使用C3P0很简单,先从Hibernate里把c3p0-0.8.3.jar复制到项目的lib目录中,再配置Hibernate配置文件。
Hibernate配置文件可以有两种格式,一种是hibernate.properties,另一种是hibernate.cfg.xml,后者稍微方便一些,当增加hbm映射文件的时候,可以直接在hibernate.cfg.xml里面增加,不必像hibernate.properties必须在初始化代码中加入。但不管怎么说,两种的配置项都是一样的。
hibernate.properties中的配置
hibernate.show_sql = true
hibernate.dialect = net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost/HibernateTest
hibernate.connection.username = root
hibernate.connection.password =
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.cfg.xml中的配置
<?xml version='1.0' encoding='big5'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/HibernateTest</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>
<mapping resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>