求救jsp+struts+hibernate的网站过一段时间就不能正常访问数据库
我用jsp+struts+hibernate做的网站,服务器用tomcat
下面是我的hibernate.cfg.xml配置文件
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN "
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name= "myeclipse.connection.profile "> csd </property>
<property name= "connection.url "> jdbc:microsoft:sqlserver://192.168.1.98:1433;DatabaseName=yj </property>
<property name= "connection.username "> sa </property>
<property name= "connection.password "> </property>
<property name= "connection.driver_class "> com.microsoft.jdbc.sqlserver.SQLServerDriver </property>
<property name= "dialect "> org.hibernate.dialect.SQLServerDialect </property>
<property name= "show_sql "> true </property>
<mapping resource= "com/news.hbm.xml " />
.
.
.
<mapping resource= "com/Notice.hbm.xml " />
</session-factory>
</hibernate-configuration>
方法一:
//////////////////////////////////////////////////////////////
Session session=HibernateSessionFactory.currentSession();
try{
Transaction tt=session.beginTransaction();
List ll=session.createQuery( "from News ").list();
tt.commit();
}catch(Exception e)
{
System.out.println(e);
}
HibernateSessionFactory.closeSession();
//////////////////////////////////////////////////////////////
其中下面是引用的HibernateSessionFactory.java这个类
package com;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml ";
private static final ThreadLocal threadLocal = new ThreadLocal();
private static final Configuration cfg = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println( "%%%% Error Creating SessionFactory %%%% ");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
private HibernateSessionFactory() {
}
}
//////////////////////////////////////////////////////////////
方法二:
/////////////////////////////////////////
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=null;
try{
session=sf.openSession();
Transaction tt=session.beginTransaction();
List ll=session.createQuery( "from News ").list();
tt.commit();
session.close();
}catch(Exception e)
{
System.out.println(e);
}
//////////////////////////////////////////
我用方法一的时候,启动tomcat后能正常读取数据库并显示,可是过了大概10个小时左右就不能正常访问数据库了,tomcat控制台出现hibernate异常can not execute query
于是我重新启动tomcat,便可以正常读取数据库并显示,过了大概10个小时左右依然出现上述异常......
后来试了一下方法二,就没什么异常情况,而且一直都能正常访问数据库。
请大家帮我看看为什么用‘方法一’会出现那种情况,是什么原因造成的呢,应该怎样处理呢?
[解决办法]
不要用hibernate自带的那个破连接池,加上common dbcp或者c3p0的试试看