标签:hibernate
1. 使用Hibernate时出现Session was already closed异常
出现此异常的原因是Session已经被关闭
如果不是使用的SessionFactory.getSession()来获得Session。
而是使用SessionFactory.getCurrentSession()方法来获得Session时,当事务结束的时候,不管是提交还是回滚事务,hibernate会自动关闭Session的,
所以不需要手动关闭。
public boolean insert(LiftInfo liftInfo) {
Session session = HibernateUtil.currentSession();
Transaction tr = null;
try {
tr = session.beginTransaction();
session.save(liftInfo);
tr.commit();
System.out.println("添加成功..");
} catch (HibernateException e) {
// TODO Auto-generated catch block
tr.rollback();
System.out.println("添加失败");
e.printStackTrace();
return false;
} finally {
HibernateUtil.closeSession();//此处如果写上session.close()会出现错误
}
return true;
}J2EE编程心得-使用Hibernate出现的错误及解决方法 更新中...,布布扣,bubuko.com
J2EE编程心得-使用Hibernate出现的错误及解决方法 更新中...
标签:hibernate
原文地址:http://blog.csdn.net/yangqicong11/article/details/38662499