如何在一次大交易中管理两种服务方法?

时间:2019-01-19 11:17:26

标签: java spring hibernate jpa transactional

我有一种方法与其他两种方法:一种用于将实体保存到数据库中,另一种用于更新一个表中的某些行。这两个方法在服务中都用@Transactional注释。同样,包含这两个方法的主方法都使用@Transactional传播REQUIRES_NEW进行注释。 问题在于,如果更新某些记录的第二种方法失败,则之前的save方法不会回滚。如果两种方法之一失败,我想回滚。

Main method:
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    protected void updateFailedImsisForDelete(Date currentDate, Collection<Nto7433> failedGscsAtDelete) {
            Set<WmFailedDevice> failedDevices = new HashSet<>();
            WmFailedBatch wmFailedBatch = new WmFailedBatch(currentDate, BatchOperationType.DELETE);
            for (Nto7433 gsc : failedGscsAtDelete) {
                failedDevices.add(new WmFailedDevice(gsc.getIpAddress(), wmFailedBatch));
            }
            wmFailedBatch.setFailedDevices(failedDevices);

            WmFailedBatch returnedWmFailedBatch = wmFailedBatchService.save(wmFailedBatch);
            wmPushedImsiService.updateFailedDeleteImsisBatch(returnedWmFailedBatch.getId(), currentDate);
    }

Save method:
        @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public WmFailedBatch save(WmFailedBatch wmFailedBatch) {
        return wmFailedBatchRepository.save(wmFailedBatch);
    }

Update method:
@Query(value = "update TABLE wm_pushed_imsi set faileddeletebatchid = :batchid where expiredate < :expiredate and faileddeletebatchid is NULL ", nativeQuery = true)
    @Modifying
    void updateFailedDeleteImsisBatch(@Param("batchid") Long batchFailedDeleteId, @Param("expiredate") Date currentDate);

@Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateFailedDeleteImsisBatch(Long batchFailedDeleteId, Date currentDate) {
        wmPushedImsiRepository.updateFailedDeleteImsisBatch(batchFailedDeleteId, currentDate);
    }

因此,在updateFailedImsisForDelete中,如果updateFailedDeleteImsisBatch失败,则提交之前的保存,但我想回滚

0 个答案:

没有答案