org.springframework.transaction.TransactionSystemException:无法提交JPA事务;嵌套异常为javax.persistence.RollbackException:事务已回滚,因为它被标记为仅回滚
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction was rolled back because it has been marked as rollback-only
ID: java/transaction-rolled-back-deadlock
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Spring Boot 2.7.x | active | — | — | — |
| Spring Boot 3.2.x | active | — | — | — |
| Hibernate 5.6.x | active | — | — | — |
| Hibernate 6.3.x | active | — | — | — |
根因分析
当事务被标记为仅回滚(例如由于数据完整性违规或乐观锁定失败),但外部事务上下文尝试提交它时发生,通常在嵌套事务或服务层中捕获并吞没了异常。
English
This error occurs when a transaction is marked for rollback-only (e.g., due to a data integrity violation or optimistic locking failure) but the outer transaction context attempts to commit it, typically in a nested transaction or service layer where an exception was caught and swallowed.
官方文档
https://docs.spring.io/spring-framework/reference/data-access/transaction/declarative/tx-propagation.html解决方案
-
Use REQUIRES_NEW propagation for the inner method to create a separate transaction: `@Transactional(propagation = Propagation.REQUIRES_NEW)` so that the inner transaction's rollback does not affect the outer transaction.
-
Check for rollback-only status before committing: `TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();` and then handle the error gracefully in the outer method by calling `TransactionAspectSupport.currentTransactionStatus().flush()`.
-
Refactor the code to avoid swallowing exceptions: ensure that any exception that triggers a rollback is rethrown to the outer transactional boundary, e.g., `catch (DataIntegrityViolationException e) { throw new RuntimeException(e); }`.
无效尝试
常见但无效的做法:
-
Adding @Transactional(rollbackFor = Exception.class) to all methods
80% 失败
This does not prevent the rollback-only marking; it only ensures exceptions trigger rollback. The issue is that an exception was caught and the transaction was marked rollback-only, then a commit is attempted.
-
Catching the exception in the outer method and ignoring it
90% 失败
The transaction is already marked rollback-only by the inner method; ignoring the exception does not reset the transaction status.
-
Increasing the transaction timeout to avoid premature rollback
70% 失败
The rollback is due to a data error (e.g., constraint violation), not a timeout. Increasing timeout does not address the root cause.