Spring中关于hibernate的session

来源:互联网 发布:python设置守护线程 编辑:程序博客网 时间:2024/05/18 01:31

1、Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();

自动关闭session

2、Session session = getHibernateTemplate().getSessionFactory().openSession();

需要手动关闭session,即session.close();

3.this.getSession

用this.releaseSession(session);关闭

 

1.使用getSession()返回session有两种情况。
   a.当前线程有存在session,就直接返回。
   b.当前线程中不存在session就重新创建一个。

如果spring配置了事务,那么session就不必关闭,在事务完成之后将被自动关闭。如果没有参与事务那就要调用session.close()关闭。

2.getSession()拿到的Session无论是否参与事务,Spring都不负责关闭,除非使用OpenSessionInView模式。

 

原创粉丝点击