我见过很多类似的问题,但它们通常与我的问题无关。
这是我的代码。
@Override //class implements interface
//can also add "rollbackFor = OutOfMemoryError.class" here, to no avail
@Transactional(noRollbackFor = IllegalArgumentException.class)
public ReportTemplate getByCode(final String code)
//throws IllegalArgumentException - see question text
{
if(code == null) {
throw new IllegalArgumentException("ss");
}
}
消费者(非事务性)通过自动装配接口调用此方法。
尽管 noRollbackFor 事务(仅由此方法组成)会回滚。
好吧,我理解有人说“默认情况下,对于任何未选中的情况都是回滚,因此由于默认值没有被覆盖,该规则仍然适用”。但这不应该是真的,因为这意味着 noRollbackFor 是无用的:)我们可以尝试指定 rollbackFor 如上面的评论,希望它会取消“默认”rollbackFor - 但这没有帮助:回滚仍然会发生;看起来这个设置只是添加到“默认”中。最糟糕的是,当 noRollbackFor 和 rollbackFor 覆盖相同的后代异常时,我找不到关于它应该如何工作的精确规范(就像在我的例子中,RuntimeException 与 IllegalArgumentException,谁赢了?)。
好吧,我找到了但官方有一个澄清它说“最强的匹配规则获胜” - 但这似乎并不普遍正确,就像我的情况一样。另外,出于某种原因,他们过于具体:他们说 no-rollback-for="InstrumentNotFoundException" 的字面意思是“除了 InstrumentNotFoundException 之外的任何异常”都将被回滚(但是 InstrumentNotFoundException 的后代呢?)无论如何,它已经过时了:2.5.x,而较新的文档只是说“rollbackFor 列出了要回滚的异常,而 noRollbackFor 列出了不回滚的异常”,我想说,这是一种非常矛盾的方式。
更有趣的是,如果我在其“throws”子句中指定 IllegalArgumentException,上面的代码将停止回滚。是的,我不必这样做,因为它未经检查 - 但如果我这样做, noRollbackFor 似乎终于注意到它并表现得正常!有谁知道什么手册说这应该发生?或者也许这是一个错误?
UPDATE这是spring事务日志(在我的实际代码YargReportTemplateDao$TemplateNotFoundException中到处都是而不是IllegalArgumentException)
1)与“投掷”
[qtp22373939-44] TRACE org.springframework.transaction.interceptor.TransactionInterceptor - Completing transaction for [ru.it.p321.dao.YargReportTemplateDaoImpl.getByCode] after exception: ru.it.p321.dao.YargReportTemplateDaoImpl$1: Yarg template was not found for given code: ITORG_REJECT_NOTIFICATION
[pool-28-thread-1] DEBUG org.springframework.transaction.jta.JtaTransactionManager - Initiating transaction commit
[qtp22373939-44] TRACE org.springframework.transaction.interceptor.RuleBasedTransactionAttribute - Applying rules to determine whether transaction should rollback on ru.it.p321.dao.YargReportTemplateDaoImpl$1: Yarg template was not found for given code: ITORG_REJECT_NOTIFICATION
[qtp22373939-44] TRACE org.springframework.transaction.interceptor.RuleBasedTransactionAttribute - Winning rollback rule is: NoRollbackRuleAttribute with pattern [ru.it.p321.dao.YargReportTemplateDao$TemplateNotFoundException]
2)没有“抛出”
[qtp21176461-48] TRACE org.springframework.transaction.interceptor.TransactionInterceptor - Completing transaction for [ru.it.p321.dao.YargReportTemplateDaoImpl.getByCode] after exception: org.springframework.dao.InvalidDataAccessApiUsageException: Yarg template was not found for given code: ITORG_REJECT_NOTIFICATION; nested exception is ru.it.p321.dao.YargReportTemplateDaoImpl$1: Yarg template was not found for given code: ITORG_REJECT_NOTIFICATION
[qtp21176461-48] TRACE org.springframework.transaction.interceptor.RuleBasedTransactionAttribute - Applying rules to determine whether transaction should rollback on org.springframework.dao.InvalidDataAccessApiUsageException: Yarg template was not found for given code: ITORG_REJECT_NOTIFICATION; nested exception is ru.it.p321.dao.YargReportTemplateDaoImpl$1: Yarg template was not found for given code: ITORG_REJECT_NOTIFICATION
[qtp21176461-48] TRACE org.springframework.transaction.interceptor.RuleBasedTransactionAttribute - Winning rollback rule is: null
[qtp21176461-48] TRACE org.springframework.transaction.interceptor.RuleBasedTransactionAttribute - No relevant rollback rule found: applying default rules