org.hibernate.HibernateException: No Hibernate Session bound to thread

来源:互联网 发布:海康威监控无网络视频 编辑:程序博客网 时间:2024/05/18 01:36

   项目启动时抛异常:org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here  

   出现此异常的情况或有多种:DataSource数据源、事务未正常运行、session获取失败。

   先说说spring如何对事务和hibernate session进行管理的:   

   1.getCurrentSession()是必须提交事务的。故使用getSessionFactory().getCurrentSession()时,这个方法一定是要配置声明式事务管理。
   2.openSession()恰恰是与以上的方法想法,它不需要提交事务。但是他的资源必须手动关闭。
   相对以上两点,解决方法有
   1.获得session时使用openSession()。
   2.若用到getCurrenctSession(),需在sessionFactory中配置(当然也可以使用注解,在service类加上事务@Transactional
<prop key="hibernate.current_session_context_class">thread</prop><prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>




   

0 0