4.openSession() 、 getCurrentSession()与session上下文

openSession()每次都打开一个新的session,用了openSession(),要记得close()掉

 

getCurrentSession()在session上下文(hibernate配置文件中的current_session_context_class有设定)找session,如果有,则返回该session,否则会新建一个session。不需要调用close(),transaction commit后会自动关闭。openSession()与getCurrentSession()不能混用!

 

session上下文在hibernate配置文件中(hiberbate.cfg.xml)设置,<property name="current_session_context_class">thread/jta</property>
事务可以简单分为两种:connection事务和jta事务。
thread是通过connection对象来管理事务,不能管理分布式的事务,属于connection事务
jta(Java Transaction API)可以管理分布式事务。它需要用到多个connection对象,与不同的数据库系统进行交互,此时jta采用第三方的事务管理器(transaction manager)统一管理不同的connection事务。

posted @ 2015-06-27 14:19  cbirdq  阅读(168)  评论(0编辑  收藏  举报