session的openSession()方法与getCurrentSession()方法的区别

来源:互联网 发布:公路预算软件 编辑:程序博客网 时间:2024/06/05 17:06

         session的openSession()方法和getCurrentSession()方法有很大区别,用的时候要分清。

    在hibernate中,当要保存记录时,我们要用到session的save()方法,所以需要得到session,得到session

要用到openSession()方法或getCurrentSession()方法。

openSession()方法:每次得到的session都是新的,而且在用完后需要调用session的close()方法关闭。

getCurrentSession()方法:是从上下文中找,如果存在session,则就用旧的;如果没有,就会建新的session。

调用getCurrentSession()方法不需要close,因为在事务提交时已经自动close。




——空气还在,希望还在。