首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Hibernate SessionFactory中openSession跟getCurrentSession方法的区别

2012-11-20 
Hibernate SessionFactory中openSession和getCurrentSession方法的区别1. 前者打开一个新的,后者当前有ses

Hibernate SessionFactory中openSession和getCurrentSession方法的区别

1. 前者打开一个新的,后者当前有session的话,则是使用当前的session,没有的话则创建一个新的;


2. 如果使用前者获得一个session的话,需要手动关闭session,使用后者,当事务提交,session会自动关闭,如果再关闭session则会报如下异常:Session has already closed;


3. sf.getCurrentSession方法需要在hibernate.cfg.xml做如下配置:


<propertyname="current_session_context_class" >thread</property>


没有配置的话会报如下异常:org.hibernate.HibernateException:No CurrentSessionContext configured!

 

热点排行