org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
原因不詳:
解決方法:
一是在web.xml添加:
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
還沒那么深入了解,對這種方法表示像喝白開水,不感冒。
還是方法二好理解點:
|
1
2
3
|
添加:
@Transactional(propagation = Propagation.REQUIRED)
|
不過,記得還要在spring的配置文件上添加
|
1
|
<tx:annotation-driven transaction-manager=
"transactionManager"
/>
|
不然單單添加 @Transactional也沒有用的!!
例如:


