Hibernate中getCurrentSession()和openSession()方法有何区别?

版权声明:欢迎转载,评论 https://blog.csdn.net/qq_29645505/article/details/88369874

1.采用getCurrentSession()创建的Session会绑定到当前的线程中去,而采用OpenSession()则不会。

2.采用getCurrentSession()创建的Session在commit或rollback后会自动关闭,采用openSession()必须手动关闭。

3.采用getCurrentSession()需要在Hibernate.cfg.xml配置文件中加入如下配置:

1)如果是本地事物,及JDBC一个数据库:

<propety name=”Hibernate.current_session_context_class”>thread</propety>

2)如果是全局事物,及jta事物、多个数据库资源或事物资源:

<propety name=”Hibernate.current_session_context_class”>jta</propety>

猜你喜欢

转载自blog.csdn.net/qq_29645505/article/details/88369874
今日推荐