u010031400
u010031400
2013-08-08 03:23

刚学的Hibernate求解session的关闭问题

  • hibernate

[code="java"]package com.pb.hibernate.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
private static Session session;

private HibernateUtil(){}

static{
    buildSession();
}

private static void buildSession(){
    Configuration config=new Configuration().configure();
    SessionFactory sf=config.buildSessionFactory();
    session=sf.openSession();
}

public static Session getSession(){
    if (session==null) {
        buildSession();
    }
    return session;
}

public static void closeSession(){
    if (session!=null) {
        session.close();
    }
}

}[/code] 这是我们老师写的代码,一个Hibernate的工具类,23行好像有问题,session关闭后不为空,就不会执行buildSession()这个方法,然后就一直报session is closed,求修正

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

6条回答