在使用Hibernate的项目中,在一个定时任务中,执行某段代码,满足条件后,更新表数据。
程序在执行到更新表数据的时候,报错如下:
Could not obtain transaction-synchronized Session for current thread
在 spring-config.xml 配置事务 切入点 和 切入方法
<!-- 配置事务管理 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 指定使用cglib --> <aop:aspectj-autoproxy proxy-target-class="true" /> <!-- 配置事务的传播特性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="dispose*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 配置事务的切入点 --> <aop:config> <aop:pointcut id="targetMethodSchedule" expression="execution(* com.miracle.luna.mock.schedule.*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="targetMethodSchedule" /> </aop:config>
[说明]
1、<tx:method name="dispose*" propagation="REQUIRED" /> 表示对dispose 开头的方法有效
2、<aop:pointcut id="targetMethodSchedule" expression="execution(* com.miracle.luna.mock.schedule.*.*(..))" /> 可以模糊匹配 schedule 目录下的多个服务层
注意此处,不管schedule目录下下有没有细分 service 和 impl层,必须是 schedule.*.*(..)) ,否则会报如下错误。
Caused by: java.lang.IllegalArgumentException: warning no match for this type name: com.miracle.luna.mock.schedule [Xlint:invalidAbsoluteTypeName] at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:315) at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:217) at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:190) at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:169) at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:220) at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:279) at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:311) at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:119) at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:89) at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:70) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:346) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:298) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:423) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1633) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ... 149 more