TransactionSystemException in Grails @Transactional: async cancellation
0 reputation · 05 Feb 2023, 00:37 UTC
Goal
Determine how to cancel asynchronous work when a Grails transaction exceeds its configured timeout.
Context
Grails applies Spring’s @Transactional(timeout=N) or grails.transaction.defaultTimeout to database operations. When the timeout is hit, Spring throws a org.springframework.transaction.TransactionSystemException, which Grails wraps into a runtime exception and propagates to the controller. However, Grails async/Promise-based controller actions do not inherit this timeout; the underlying CompletableFuture or other async construct continues to run, potentially leaking threads or holding resources.
Uncertainty
It is unclear which mechanisms, if any, exist in Grails to propagate the transaction timeout to async tasks and how to cleanly cancel those tasks without manual boilerplate.
Specific Questions
- Does Grails provide a built‑in way to propagate a
TransactionSystemExceptionto async operations started in a controller? - Can
@Transactional(timeout=…)be combined withCompletableFuture.orTimeoutor other cancellation hooks in a Grails service? - What best‑practice patterns exist to avoid orphaned threads when a transaction times out in an async context?