spring3 与 Hibernate4集成报错 No Session found for current thread

在获得session的时候使用sessionFactory.getCurrentSession()方法的时候,报错No Session found for current thread;


首先要理解:

1:getCurrentSession把session和当前线程关联起来,若session存在就不创建了直接调用,openSession只是重新开启一个session

2:getCurrentSession()获得的session会在事务关闭或者回滚时会自动关闭,而openSession获得的session必须手动关闭


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

具体报错的原因如下:

报错原因一:

这是因为在hibernate中他是通过sessionFactory调用CurrentSessionContext的currentSession()方法获取session的,当你的session为空时,他会自动调用sessionFactory.openSession()来创建一个session,而在集成的时候,session是交给spring来管理的,而spring也有一个自己的CurrentSessionContext实现,这个实现并不会自动为你创建session,所以就会报那个错误


报错原因二:

如果没有在spring配置TransationMnager的情况下,没调用sessionFactory.openSession()就直接调用sessionFactory.getCurrentSession(),就会报No Session found for current thread,因为当前没有session,而getCurrentSession()又不会为你创建

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

通过spring解决:

在spring中配置<tx:annotation-driven transaction-manager="transactionManager"/>

在spring配置了TransationMnager的情况下,有两种方式可以解决这个问题(此时spring会通过aop的方式为当前线程创建session,就可以正常调用getCurrentSession):

①通过@Transactional注解在方法上

在spring添加事务属性和aop

<tx:advice id="advice">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="save*" propagation="REQUIRED" read-only="false" />
<tx:method name="*" isolation="READ_COMMITTED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.smartBank.*.*.*(..))" id="pc"/>
<aop:advisor advice-ref="advice" pointcut-ref="pc"/>
</aop:config>

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

通过hibernate解决:

在hibernate中配置<property name="current_session_context_class">thread</property>

这相当于不将session交给spring管理,原因是Hibernate在默认情况下会使用JTASessionContext来跟踪上下文的session,而Spring则提供了自己SpringSessionContext若没有配置,调用getCurrentSession()的效果完全依赖于spring的SpringSessionContext实现。

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

建议:

①如果我们配置了TransactionManager,那么我们就不应该调用sessionFactory的openSession()来获得Sessioin,因为这样获得的session并没有被事务管理。

当方法不需要事务支持的时候,sessionFactory.openSession()来获得Session对象。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值