Repository.Save() 内部调度不起作用
Repository.Save() inside scheduling is not working
我有一个在调度程序中调用的 repository.save() 方法。但它没有将任何内容保存到数据库中。
以下是我的调度程序
@Component
@Transactional
@Slf4j
public class WomConditionActionJob {
@Autowired
private Environment env;
@Autowired
private ECCRepository eCCRepository;
@Autowired
private WOCRepository wOCRepository;
@Autowired
private PSRepository pSRepository;
@Scheduled(fixedDelayString = "${wCATrigger.polling.frequency}", initialDelayString = "${wCATrigger.initial.delay}")
public void execute() {
try {
final PauseStatus pause = pSRepository.findByPSName(PSName.PAUSE);
pauseCondition(pause,threshold);
} catch (Exception e) {
log.error("Exception Occured {}", e);
}
}
private void pauseCondition(final PauseStatus pause, final Integer threshold) {
WOTCondition wotCId = workOrderConditionRepository.findById(1).get();
wotCId.setPauseStatus(pause);
wotCId.setIsUserAction(Boolean.FALSE);
workOrderConditionRepository.save(wotConditionbyId);
conditionCount.setErrorCount(0);
errorConditionCountRepository.save(conditionCount);
}
}
我尝试使用 saveAndFlush() 但那次我遇到了以下错误
[pool-2-thread-1]|ERROR|[o.s.s.s.TaskUtils$LoggingErrorHandler.handleError(96)]|Unexpected error occurred in scheduled task.
org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:873)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:710)
添加这个解决了我的问题
@Transactional(propagation=Propagation.REQUIRES_NEW)
例子
@Scheduled(cron = "0/5 * * * * *")
@Transactional(propagation=Propagation.REQUIRES_NEW)
@Override
public void scheduleJob() {
Message message = new Message();
message.setMessageId(UUID.randomUUID().toString());
message.setAccountId("accountId");
message.setSent(2L);
message.setFailed(2L);
message.setDelivered(2L);
// saves or update message_report table
messageRepository.save(message);
}
我有一个在调度程序中调用的 repository.save() 方法。但它没有将任何内容保存到数据库中。 以下是我的调度程序
@Component
@Transactional
@Slf4j
public class WomConditionActionJob {
@Autowired
private Environment env;
@Autowired
private ECCRepository eCCRepository;
@Autowired
private WOCRepository wOCRepository;
@Autowired
private PSRepository pSRepository;
@Scheduled(fixedDelayString = "${wCATrigger.polling.frequency}", initialDelayString = "${wCATrigger.initial.delay}")
public void execute() {
try {
final PauseStatus pause = pSRepository.findByPSName(PSName.PAUSE);
pauseCondition(pause,threshold);
} catch (Exception e) {
log.error("Exception Occured {}", e);
}
}
private void pauseCondition(final PauseStatus pause, final Integer threshold) {
WOTCondition wotCId = workOrderConditionRepository.findById(1).get();
wotCId.setPauseStatus(pause);
wotCId.setIsUserAction(Boolean.FALSE);
workOrderConditionRepository.save(wotConditionbyId);
conditionCount.setErrorCount(0);
errorConditionCountRepository.save(conditionCount);
}
}
我尝试使用 saveAndFlush() 但那次我遇到了以下错误
[pool-2-thread-1]|ERROR|[o.s.s.s.TaskUtils$LoggingErrorHandler.handleError(96)]|Unexpected error occurred in scheduled task. org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:873) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:710)
添加这个解决了我的问题
@Transactional(propagation=Propagation.REQUIRES_NEW)
例子
@Scheduled(cron = "0/5 * * * * *")
@Transactional(propagation=Propagation.REQUIRES_NEW)
@Override
public void scheduleJob() {
Message message = new Message();
message.setMessageId(UUID.randomUUID().toString());
message.setAccountId("accountId");
message.setSent(2L);
message.setFailed(2L);
message.setDelivered(2L);
// saves or update message_report table
messageRepository.save(message);
}