在我的春天启动的项目,我已经实现了下列服务方法:春嵌套事务
@Transactional
public boolean validateBoard(Board board) {
boolean result = false;
if (inProgress(board)) {
if (!canPlayWithCurrentBoard(board)) {
update(board, new Date(), Board.AFK);
throw new InvalidStateException(ErrorMessage.BOARD_TIMEOUT_REACHED);
}
if (!canSelectCards(board)) {
update(board, new Date(), Board.COMPLETED);
throw new InvalidStateException(ErrorMessage.ALL_BOARD_CARDS_ALREADY_SELECTED);
}
result = true;
}
return result;
}
这个方法我用另一种服务方法被称为update
内:
@Transactional(propagation = Propagation.REQUIRES_NEW)
public Board update(Board board, Date finishedDate, Integer status) {
board.setStatus(status);
board.setFinishedDate(finishedDate);
return boardRepository.save(board);
}
我需要提交在update
方法中独立于在validateBoard
方法中启动的所有者事务而更改数据库。现在,任何异常都会回滚。
即使与@Transactional(propagation = Propagation.REQUIRES_NEW)
它不起作用。
如何正确使用Spring做到这一点,并允许嵌套事务?
显然你正在调用同一个类中的一个方法,所以Spring不能拦截这个调用并应用一个事务代理('REQUIRES_NEW'传播被忽略)。您应该将'update'方法迁移到另一个Spring bean, –
谢谢,现在一切按预期工作 – alexanoid