您的位置:首页 > 编程语言 > Java开发

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

2014-06-19 00:40 369 查看
   项目启动时抛异常: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>


   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐