# 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`
- **Domain:** java
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Spring Boot 2.7.x | active | — | — |
| Spring Boot 3.2.x | active | — | — |
| Hibernate 5.6.x | active | — | — |
| Hibernate 6.3.x | active | — | — |

## Workarounds

1. **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.** (90% success)
   ```
   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.
   ```
2. **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()`.** (85% success)
   ```
   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()`.
   ```
3. **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); }`.** (80% success)
   ```
   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); }`.
   ```

## Dead Ends

- **Adding @Transactional(rollbackFor = Exception.class) to all methods** — 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. (80% fail)
- **Catching the exception in the outer method and ignoring it** — The transaction is already marked rollback-only by the inner method; ignoring the exception does not reset the transaction status. (90% fail)
- **Increasing the transaction timeout to avoid premature rollback** — The rollback is due to a data error (e.g., constraint violation), not a timeout. Increasing timeout does not address the root cause. (70% fail)
