public AdminForm add(AdminForm vo) {
Session session=null;
Transaction t=null;
try {
session=HibernateSessionFactory.getSession();
t=session.beginTransaction();
............
t.commit();
return vo;
} catch (Exception e) {
if(t!=null){
t.rollback();
}
System.out.println(System.getProperty("user.dir")+"---"+this.getClass().getPackage()+"."+new Exception().getStackTrace()[0].getMethodName()+" Err:"+e.toString());
return null;
}finally{
session.flush();
HibernateSessionFactory.closeSession();
}
}

解决方案 »

  1.   

    有没有多余,比如session.flush();有没有必要。
    在close之前要不要flush?我看网上都flush了一次。
    查了一下API,commit()会自动调用flush(),如果不需要commit我想也没必要flush
      

  2.   

    s.flush不是必须的,s.close()会调用一次s.flush() s.close()正常情况下应该关闭,除非你是用ThreadLocal管理Session。 
      

  3.   

    代码不会报错,但是你的资源没关public AdminForm add(AdminForm vo) {
            Session session=null;
            Transaction t=null;
            try {
                session=HibernateSessionFactory.getSession();
                t=session.beginTransaction();        
                ............
                t.commit();
            } catch (Exception e) {
                if(t!=null){
                    t.rollback();
                }
                System.out.println(System.getProperty("user.dir")+"---"+this.getClass().getPackage()+"."+new Exception().getStackTrace()[0].getMethodName()+" Err:"+e.toString());
               vo = null;
            }finally{
                session.flush();
                HibernateSessionFactory.closeSession();
            }
           return vo;
        }
      

  4.   

    finally{
              session.close();
        }
    这样就可以了
      

  5.   

    finally{ 
              HibernateSessionFactory.closeSession(); 
        } 
    我是用的myeclipse自动生成的HibernateSessionFactory类控制的