HibernateException: Could not obtain transaction-synchronized Session for current thread错误
业务场景:在job中执行Service中的方法(事务是在xml中配置,针对所有service的方法的)
我的springContext.xml配置如下:
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="NOT_SUPPORTED" read-only="true" />
<tx:method name="select*" propagation="NOT_SUPPORTED" read-only="true" />
<tx:method name="query*" propagation="NOT_SUPPORTED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception.class" />
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="txPointCut" expression="execution(* net.kkkk.*.*.service.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
</aop:config>
web.xml中配置:
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
<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>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
后来百度后,结合大牛的文章,springContext.xml做出如下调整:
<tx:annotation-driven proxy-target-class="true" />
<tx:annotation-driven transaction-manager="transactionManager" />
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="NOT_SUPPORTED" read-only="true" />
<tx:method name="select*" propagation="NOT_SUPPORTED" read-only="true" />
<tx:method name="query*" propagation="NOT_SUPPORTED" read-only="true" />
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception.class" />
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="txPointCut" expression="execution(* net.kkkk.*.*.service.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
</aop:config>
发现还是不行,依然报上面的错误!
后来在对应的Service的实现类中,增加@Transactional注解,解决上面的报错!