Could not obtain transaction-synchronized Session for current thread原因及解决方案

问题

sessionFactory.getCurrentSession()这样获取session,抛了这异常。
Could not obtain transaction-synchronized Session for current thread原因及解决方案

解决

  1. 必须要将使用了sessionFactory.getCurrentSession()获取session的代码所在的方法加入到事务管理器中;否则获取不到session了。
  2. sessionFactory.getCurrentSession()是要基于事务的,才能实现session生命周期的管理。所以查询方法上用只读事务就ok了。

参考:
Could not obtain transaction-synchronized Session for current thread原因及解决方案