原来在做jbpm工作流时老是出现session的问题出现异常,算是办法也简单session.close(),原因是我在sevice层openSession而忘记关闭,每写一个方法都必须这样。而采用getCurrentSession的话就不需要了,现总结两者间的区别:
1. 如果使用的是getCurrentSession来创建session的话,在commit后,session就自动被关闭了, 也就是不用再session.close()了。但是如果使用的是openSession方法创建的session的话,那么必须显示的关闭session,也就是调用session.close()方法。这样commit后,session并没有关闭。
2. 使用SessionFactory.getCurrentSession()需要在hibernate.cfg.xml中如下配置:
* 如果采用jdbc独立引用程序配置如下:
<property name="hibernate.current_session_context_class">thread</property>
* 如果采用了JTA事务配置如下
<property name="hibernate.current_session_context_class">jta</property>