日期:2014-05-18  浏览次数:20637 次

求救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();
  &n