解决问题 Could not obtain transaction-synchronized Session for current thread

一、问题现象

在使用Hibernate的项目中,在一个定时任务中,执行某段代码,满足条件后,更新表数据。

程序在执行到更新表数据的时候,报错如下:

Could not obtain transaction-synchronized Session for current thread

解决问题 Could not obtain transaction-synchronized Session for current thread_第1张图片


二、解决方案

spring-config.xml 配置事务 切入点切入方法


    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    bean>
    
    
     <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、 表示对dispose 开头的方法有效;
2、.(…))” /> 可以模糊匹配 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

解决问题 Could not obtain transaction-synchronized Session for current thread_第2张图片


你可能感兴趣的:(JAVA,spring,java,hibernate)