在异步方法中回滚,调用另一个事务方法

时间:2016-04-06 19:38:32

标签: java spring asynchronous transactions

我有一个非事务性和异步方法,它调用同一服务的其他事务方法。

如果发生某些事情,我会发现异常,如果发生了某些事情,我会保存错误,但回滚不起作用。

在此示例中,要调用方法保存播放器服务,该事务将保存播放器。

@Service
@Transactional(readOnly = true)
public class PlayerServiceImpl implements PlayerService {

    @Inject
    PlayerRepository playerRepository;

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void save(PlayerEntity player) {
        //more code here
        playerRepository.save(player);
    }

    //other methods
}

我的其他服务:

    @Service
    public class TeamServiceImpl implements TeamService {

        @Inject
        TeamRepository teamRepository;

        @Inject
        MessageRepository messageRepository;

        @Inject
        ErrorRepository errorRepository;    

        @Inject
        PlayerService playerService;    


        @Async("asyncExecutor")
        @Override
        public void saveWithPlayersAsync(TeamEntity team, User user) {

            MessageEntity message = new MessageEntity();

            try {

    //I want rollback this if something wrong happens
                this.savePlayersA(team);
                this.savePlayersB(team);
                message.setContent("Ok !");
            } catch (TeamException e) {
                //e.printStackTrace();
                message.setContent("Fail !");           
                message.setUser(user)
//I save the error for audit
                errorRepository.save(new Error("Fail", user.getCode()));
            } finally {
//always save message for user than execute de function
                messageRepository.save(message);
            }

        }   

        @Transactional(readOnly = false, rollbackFor = TeamException.class)
        private void savePlayersA(TeamEntity team) throws TeamException {
            PlayerEntity p1 = new PlayerEntity();
            p1.setName("Name 1");
            p1.setSurname("Surname 1");
            p1.setTeam(team);
            playerService.save(p1);

            PlayerEntity p2 = new PlayerEntity();
            p2.setName("Name 2");
            p2.setSurname("Surname 2");
            p2.setTeam(team);
            playerService.save(p2);
        }

        @Transactional(readOnly = false, rollbackFor = TeamException.class)
        private void savePlayersB(TeamEntity team) throws TeamException {
            PlayerEntity p3 = new PlayerEntity();
            p3.setName("Name 3");
            p3.setSurname("Surname 3");
            p3.setTeam(team);
            playerService.save(p3);

            PlayerEntity p4 = new PlayerEntity();
            p4.setName("Name 4");
            p4.setSurname("Surname 4");
            p4.setTeam(team);
            playerService.save(p4);

            // here something happens and throw my custom exception
            if (true) {
                throw new TeamException("Fail!");
            }
        }   
    }

为什么不回滚?我将异常类型放在rollbackFor。

1 个答案:

答案 0 :(得分:1)

这不是为您回滚的原因是因为您调用事务方法的异步方法属于同一个类。使用@Transaction或@Async注释方法时,注释代码由spring创建的代理处理。当方法注释在同一个类中时,代理无法捕获对它们的调用以通过spring处理附加请求,因此该方法的注释是多余的。你可以通过将savePlayer方法放在一个单独的类中并在那里使用事务处理方法来实现。