A method with REQUIRES_NEW was meant to save an audit row even when the main transaction fails. When does it work, and when does it deadlock?
REQUIRES_NEW suspends the caller's transaction and commits the audit on a second connection, so the row survives a later rollback if the call goes through the Spring proxy. It hangs when the audit touches rows the suspended transaction has locked, or when every thread holds one pooled connection and waits for another.
Answer
Short answer
It works when the audit method lives on a different Spring bean. The call then goes through the transaction proxy, the caller's transaction is suspended, and the audit commits on its own connection before the main work fails. With that transaction propagation, the row survives the outer rollback. It hangs in two cases. The first is when the audit needs a row lock the suspended outer transaction still holds. The second is when the connection pool has no second connection to give, because every thread is holding one.
Was this answer helpful?
Likely follow-ups
- Why does catching an exception thrown by the REQUIRES_NEW audit not mark the outer transaction rollback-only?
- How would you write the audit from an after-rollback event listener, and which propagation does that listener need?
- Your audit entity references an order the outer transaction inserted a moment ago. What happens when the inner transaction commits?
- Under JTA, what does the transaction manager need before REQUIRES_NEW can suspend anything?
Related questions
- The inner service threw, you caught the exception in the caller, and then the commit failed with UnexpectedRollbackException. Why?hardAlso on spring and transactions5 min
- Two doctors can go off call only if at least one remains. Both check the rule at the same moment, both see two doctors on call, and both go off call. What isolation problem is this, and what actually fixes it?hardAlso on transactions6 min
- You are getting a handful of deadlock errors an hour under load. How do you find the cause, and how do you stop them?hardAlso on transactions7 min
- Walk me through the transaction isolation levels and which anomalies each one permits.hardAlso on transactions6 min