org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread

  1. org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread





原因不详:

解决方法:
一是在web.xml添加:
  1. <filter>
  2. <filter-name>hibernateFilter</filter-name>
  3. <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
  4. <init-param>
  5. <param-name>singleSession</param-name>
  6. <param-value>true</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>flushMode</param-name>
  10. <param-value>AUTO</param-value>
  11. </init-param>
  12. </filter>
  13. <filter-mapping>
  14. <filter-name>hibernateFilter</filter-name>
  15. <url-pattern>/*</url-pattern>
  16. </filter-mapping>


还没那么深入了解,对这种方法表示像喝白开水,不感冒。


还是方法二好理解点:


1
2
3
添加:
 
@Transactional(propagation = Propagation.REQUIRED)

不过,记得还要在spring的配置文件上添加

1
<tx:annotation-driven transaction-manager="transactionManager"/>

不然单单添加     @Transactional也没有用的!!


​例如:







原文地址:https://www.cnblogs.com/share2015/p/5321822.html