gpt4 book ai didi

java - 如何从 @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class) 中排除特定异常?

转载 作者:行者123 更新时间:2023-11-30 05:29:41 24 4
gpt4 key购买 nike

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public void abc()
{
try{
//some Code
}
catch(DataIntegrityViolationException ex)
{
//some Code
}
}

在上面的代码中,如何从回滚此事务中排除 DataIntegrityViolationException?

最佳答案

您应该使用noRollbackFor 属性。示例:

@Transactional(noRollbackFor = DataIntegrityViolationException.class)

默认情况下,@Transactional 仅适用于 RuntimeExceptionError,但不适用于已检查的异常。

因此,如果您确实想要针对除 DataIntegrityViolationException 之外的每个异常回滚事务,您还应该指定 rollbackFor 属性:

@Transactional(rollbackFor = Throwable.class, noRollbackFor = DataIntegrityViolationException.class)

关于java - 如何从 @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class) 中排除特定异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57817300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com