报错 no currentsessioncontext configured!

no currentsessioncontext configured!
使用hibernate框架报错
配置了session工厂类,使用getCurrentSession();时候引起的,原因是currentSession配置错误

这里写图片描述

当我们使用currentSession的时候需要在hibernate.cfg.xml中进行如下事务配置:

  1、如果是本地事务:

    <property name="hibernate.current_session_context_class">thread</property>

  2、如果是全局事务

  <property name="hibernate.current_session_context_class">jta</property>

如果获得session 对象使用 sessionFactory.openSession()就不会出现这个错误了。

openSession()是不管任何情况都重新开启一个Session,而getCurrentSession();相对的增加了一个判断,在有Session的情况下就会直接去调用,没有session的话才会创建,如果有事务操作的话getCurrentSession();更好一些,很容易的使得一个线程只有一个session对象。

猜你喜欢

转载自blog.csdn.net/aoman_hao/article/details/80093852