如何使用事务注释进行回滚

时间:2020-07-14 03:29:06

标签: java spring jdbctemplate

我正在尝试使用我的方法进行事务回滚。我故意使插入物无法找到。但是我看不到它回滚了。请帮助我所缺少的。

private async Task ProcessCurrentVideoFrameAsync()
{
    const BitmapPixelFormat InputPixelFormat = BitmapPixelFormat.Nv12;
    var deviceController = this.mediaCapture.VideoDeviceController;
    this.videoProperties = deviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
    VideoFrame videoFrame = new VideoFrame(InputPixelFormat, (int)this.videoProperties.Width (int)this.videoProperties.Height);

//add this line code.
    await this.mediaCapture.GetPreviewFrameAsync(videoFrame);

在DAOImpl类中,我具有以下用于所有删除和插入的方法。

@Service
public class ModesService implements IModesService{

ChargeRuleDao chargeRuleDao;

public ModesService(ChargeRuleDao chargeRuleDao){
    this.chargeRuleDao = chargeRuleDao;
}

@Override
@Transactional(propagation = Propagation.REQUIRED)
public void process(ChargeRule chargeRule){
    chargeRuleDao.deleteShippingChargeAttr(shippingChargeRuleID);
    chargeRuleDao.deleteShippingCharge(shippingChargeRuleID);
    chargeRuleDao.deleteShippingChargeDest(shippingChargeRuleID);

    //Delete
    chargeRuleDao.insertShipChargeFeedRule(chargeRule);
        
}

2 个答案:

答案 0 :(得分:2)

您可以尝试@Transactional(rollbackFor = XYZException.class)。 XYZException应该是一个异常,该异常应该包装要回滚事务的所有异常/异常。

答案 1 :(得分:1)

默认情况下,每个未检查的异常都会发生回滚。这意味着您需要抛出一些类型未经检查的异常,例如

throw new NullPointerException();

在您的insertShipChargeFeedRule(chargeRule);

有关@Transactional的更多信息,请点击https://javamondays.com/spring-transactions-explained/