hibernate异常 Session was already closed
2017-07-19 22:55
246 查看
hibenate连接Sql Server异常:
运行报错:
出现以上原因是Session关闭
如果不是使用的SessionFactory.getSession()来获得Session。
所以不需要手动关闭。
public class TestMain { public static void main(String[] args) { // TODO Auto-generated method stub try { //对数据持久层操作 //该类主要是用于读取hibernate.cfg.xml Configuration cf=new Configuration().configure(); //创建一个会话工厂. SessionFactory sf=cf.buildSessionFactory(); //创建Session(会话)<->Connection Session s=sf.getCurrentSession(); //开始事务 Transaction ts=s.beginTransaction(); //加入一个对象(记录) Employe employe=new Employe(); //employe.setId(10); employe.setName("杨德成"); s.save(employe);//添加该对象到数据库 ts.commit(); s.close(); } catch (Exception e) { e.printStackTrace(); // TODO: handle exception } } }
运行报错:
出现以上原因是Session关闭
如果不是使用的SessionFactory.getSession()来获得Session。
而是使用SessionFactory.getCurrentSession()方法来获得Session时,当事务结束的时候,不管是提交还是回滚事务,hibernate会自动关闭Session的,
所以不需要手动关闭。
相关文章推荐
- org.hibernate.SessionException: Session was already closed异常解决办法
- hibernate的异常 Session was already closed
- hibernate的异常 Session was already closed
- Hibernate Session was already closed
- org.hibernate.SessionException: Session was already closed
- hibernate查询时的懒加载异常,……no session or session was closed
- 过滤器解决Hibernate中(no session or session was closed)异常
- Hibernate Session was already closed
- hibernate错误:org.hibernate.SessionException: Session was already closed
- 开发中遇到的hibernate异常:a different object with the same identifier value was already associated with the session
- ssh:org.hibernate.SessionException: Session was already closed
- Hibernate :A different object with the same identifier value was already associated with the session
- a different object with the same identifier value was already associated withthe session异常解决方案
- hibernate a different object with the same identifier value was already associated with the session
- LazyInitializationException no session or session was closed异常的解决思路
- Hibernate: Could not initialize proxy - the owning Session was closed
- a different object with the same identifier value was already associated withthe session异常解决方案
- a different object with the same identifier value was already associated withthe session异常解决方案
- 异常:could not initialize proxy - the owning Session was closed
- Hibernate:a different object with the same identifier value was already associated wit异常解决