gpt4 book ai didi

java - Spring : @Transactional @Scheduled method throws TransactionException

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:07:56 26 4
gpt4 key购买 nike

(对 Spring 还是有点陌生​​)

我需要一个服务方法,它同时是 @Scheduled@Transactional,这样我就可以在其中调用 DAO。

声明式事务已启用,事务管理器是一个基于 hibernate session 工厂的 org.springframework.orm.hibernate3.HibernateTransactionManager

服务类不实现任何接口(interface),因此使用 CGLIB 代理。

此设置通常工作正常(从 Web 堆栈调用的方法,即 Struts)但此方法在被调度程序调用时引发异常。

以下是相关的代码:

服务方法(类名为ClientWakeAndTerminateManager):

@Scheduled(initialDelay = 5000, fixedRateString = "${rmi.server.threads.clientsScheduleManagement.rate}")
@Transactional(readOnly = true)
public void runCheck(){

//Call a read-only DAO method (the DAO is @Autowired as a class field)

//do some stuff with the data loaded from DB

}

我的应用程序上下文的相关部分:

<!-- switch on the transactional infrastructure -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

<!-- Utility class to execute transactional code where use of annotation is not possible -->
<bean class="org.springframework.transaction.support.TransactionTemplate" id="txTemplate">
<constructor-arg name="transactionManager" ref="transactionManager"/>
</bean>

<!-- Transaction manager based on Hibernate -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernateSessionFactory"/>
</bean>

异常堆栈跟踪:

[ERROR] : Unexpected error occurred in scheduled task.
org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:661)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
at ch.unine.sitel.lis.rmi.shared.ClientWakeAndTerminateManager$$EnhancerByCGLIB$$d8be4f34.runCheck(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:64)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:53)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:351)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Caused by: org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:127)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:657)
... 22 more

堆栈跟踪似乎告诉我确实使用了事务代理,所以我不明白这个异常。帮助!

编辑:

我试图将 @Transactional@Scheduled 注释分开:

  • 创建一个包含@Scheduled 方法的新类/bean
  • 将我的服务注入(inject)这个 bean
  • 从我原来的方法中删除了 @Scheduled 但保留了 @Transactional

但我仍然遇到同样的异常。我还尝试将 @Transactional 放在我的 DAO 方法上并将其从我的服务方法中删除:结果相同。

最佳答案

使用带有 @Transactional 注释的方法创建单独的类,并在您的 @Scheduled 注释方法中调用此方法。 spring 将通过代理进行调用并正确处理 @Transactional

编辑:还要查看您的 DAO 方法并确保它不会手动提交或回滚事务

关于java - Spring : @Transactional @Scheduled method throws TransactionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18399006/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com