public class HibernateSessionFactory {
private static SessionFactory factory=null;
private static Session session=null;
static {
Configuration conf=new Configuration().configure();
factory=conf.buildSessionFactory();
}
public static SessionFactory getFactory() {
return factory;
}
public static Session getSession(){
session=factory.openSession();
return session;
}
public static void closeSession(){
if(session!=null){
session.close();
}
}

}public abstract class BaseHibernateDAO {

private Session session=null;
protected Object get(Class clz,java.io.Serializable id){
Object ret=null;
Session session=this.getSession();
try{
ret=session.get(clz, id);
}catch (Exception e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return ret;
}
protected int add(Object item){
int num=0;
Transaction tx=null;
    Session session=this.getSession();
    try {
tx=session.beginTransaction();
session.save(item);
tx.commit();

} catch (Exception e) {
++num;
if(tx!=null){tx.rollback();}
e.printStackTrace();
}finally{
this.closeSession();
}
return num;
}
protected void update(Object item){
Transaction tx=null;
    Session session=this.getSession();
    try {
tx=session.beginTransaction();
session.update(item);
tx.commit();

} catch (Exception e) {
if(tx!=null){tx.rollback();}
e.printStackTrace();
}finally{
this.closeSession();
}
}
protected int delete(Class clz,java.io.Serializable id){
int num=0;
Transaction tx=null;
    Session session=this.getSession();
    try {
tx=session.beginTransaction();
session.delete(this.get(clz, id));
tx.commit();

} catch (Exception e) {
++num;
tx.rollback();
e.printStackTrace();
}finally{
this.closeSession();
}
return num;
}
protected List search(Class clazz,Object condition){ 
try {
List results=this.getSession().createCriteria(clazz).add(Example.create(condition)).list();
    return results;
} catch (RuntimeException e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return null;
}
protected List search(Class clazz){ 
try {
List results=this.getSession().createCriteria(clazz).list();
    return results;
} catch (RuntimeException e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return null;
}

protected Session getSession() {
 this.session=HibernateSessionFactory.getSession();
 return this.session;
}
public void setSession(Session session) {
this.session = session;
}
protected void closeSession(){
this.session=null;
HibernateSessionFactory.closeSession();
}



}
public abstract class BaseHibernateDAO {

private Session session=null;
protected Object get(Class clz,java.io.Serializable id){
Object ret=null;
Session session=this.getSession();
try{
ret=session.get(clz, id);
}catch (Exception e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return ret;
}
protected int add(Object item){
int num=0;
Transaction tx=null;
    Session session=this.getSession();
    try {
tx=session.beginTransaction();
session.save(item);
tx.commit();

} catch (Exception e) {
++num;
if(tx!=null){tx.rollback();}
e.printStackTrace();
}finally{
this.closeSession();
}
return num;
}
protected void update(Object item){
Transaction tx=null;
    Session session=this.getSession();
    try {
tx=session.beginTransaction();
session.update(item);
tx.commit();

} catch (Exception e) {
if(tx!=null){tx.rollback();}
e.printStackTrace();
}finally{
this.closeSession();
}
}
protected int delete(Class clz,java.io.Serializable id){
int num=0;
Transaction tx=null;
    Session session=this.getSession();
    try {
tx=session.beginTransaction();
session.delete(this.get(clz, id));
tx.commit();

} catch (Exception e) {
++num;
tx.rollback();
e.printStackTrace();
}finally{
this.closeSession();
}
return num;
}
protected List search(Class clazz,Object condition){ 
try {
List results=this.getSession().createCriteria(clazz).add(Example.create(condition)).list();
    return results;
} catch (RuntimeException e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return null;
}
protected List search(Class clazz){ 
try {
List results=this.getSession().createCriteria(clazz).list();
    return results;
} catch (RuntimeException e) {
e.printStackTrace();
}finally{
this.closeSession();
}
return null;
}

protected Session getSession() {
 this.session=HibernateSessionFactory.getSession();
 return this.session;
}
public void setSession(Session session) {
this.session = session;
}
protected void closeSession(){
this.session=null;
HibernateSessionFactory.closeSession();
}



}我第一次执行的是search()方法,session在finally里就关闭了,但是在之后执行delete()方法是,他就报错说session is already closed,但是我在delete()方法里有getSession()呀
怪事!

解决方案 »

  1.   

    public static void closeSession(){
            if(session!=null){
            session.close();
            }
        }
    把这个方法改成传一个Session session参数进去,然后里面关闭的是传进去的参数,然后在程序中要关闭某一个session的时候就传入该session到这个close方法!
      

  2.   

    静态初始化session,导致每次getSession()时得到是同一个session引用
      

  3.   

    Session是线程不安全的,你每次用的时候再获取。而不要把session作为成员变量!
      

  4.   

    另外再说点,SessionFactory 是线程安全的。
      

  5.   

    我也觉得 session什么时候用什么时候再去就就行吧  另外一定不要乱用session 不然好像是session会乱掉 我以前就遇到过奇怪的问题  听高人指点说是乱用session导致session乱掉
      

  6.   

    private Session session=null;
    不要定义成内部成员变量,应该在每次都需要获取的地方用getSession方法获得。
    关闭的时候也调用共同方法closeSession(),但是要讲session作为参数传过去。
      

  7.   

    private static Session session=null ??
    直接返回 factory.openSession() 试试吧
    单用hibernate最好还是用ThreadLocal方式获得session 
      

  8.   

    楼主的代码貌似源自 北大青鸟!       我在不进行hibernate关联映射的时候这样用是一点问题都没有,
    但是一涉及到一对多或者多对一的关联映射的配置的时候就出现session关闭问题!另外:你的代码发到帖子里面格式竟然还如在编辑器里面一模一样,不知道这个CSDN是怎么实现的!  我把代码粘贴到我的博客的时候发出来格式很难看,缩进对齐都不完整不知道怎么控制????