我有问题: 为什么当我们使用@Scheduled
和@Transaction
注释方法时,事务不起作用? 我知道@Scheduled
调用我的类而不是由Spring创建的代理类,但无法理解此行为。为什么@ Scheduled注释不能用@Transaction注解。 Spring Boot
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserServiceImpl implements UserService {
@Override
@Scheduled(fixedRateString = "${somestring}",initialDelayString = "${anotherstring}")
@Transactional
public void doSomething() {
}
}
我有这个问题的两个解决方案:从Scheduled
方法
呼叫代理。
实施
ConcurrentTaskScheduler
并与ScheduledMethodRunnable
与代理对象替换的ScheduledMethodRunnable
对象(即我的同班同学) 。
但是这种解决方案非常不方便。
你可以解释一下为什么@Scheduled
是这样的吗?
谢谢!
_doesn't_的意思是什么? –
我的意思是说,如果调度程序调用此方法,那么方法中没有事务,用@Transactional注释。因为调度程序调用我的类的方法而不是代理类的方法。 –
它可能工作时,你废除'实现UserService'部分,因为它应该触发不同类型的代理:https://stackoverflow.com/a/30489513/995891 – zapl