小编Art*_*nov的帖子

为什么@Scheduled注释不适用于@Transaction批注.春季启动

我有一个问题:为什么当我们使用@Scheduledand 注释方法时@Transaction,事务不起作用?我知道@Scheduled调用我的类而不是Spring创建的代理类,但无法理解这种行为.

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() {

        }
    }
Run Code Online (Sandbox Code Playgroud)

我有两个解决这个问题的方法:

  1. Scheduled方法调用代理.

  2. 用代理ConcurrentTaskScheduler 对象实现和替换ScheduledMethodRunnable(与我的类一起)的对象ScheduledMethodRunnable.

但是这种解决方案非常不方便.

你能解释一下为什么会@Scheduled这样吗?

谢谢!

java spring scheduled-tasks spring-transactions spring-boot

9
推荐指数
1
解决办法
3143
查看次数