Handling a Failed Transaction Before Continuing
After a statement failure, the transaction may need rollback before further ordinary work under the database's rules.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Trace a PostgreSQL example
An insert violates a known constraint within a transaction. Continuing without an applicable rollback can leave subsequent commands unable to proceed. A savepoint can support a deliberately recoverable sub-operation.
Choose the behavior near the failure
A surrounding handler should know whether to abort the whole write or use a defined recovery point. See small exception boundaries.[1]
Restore a usable transaction state before continuing
In PostgreSQL an error in a transaction generally leaves that transaction block failed until rollback, unless recovery occurs through an appropriate savepoint. A follow-up query alone does not repair the earlier error. Keep error scope visible in the application. See using savepoints.
Begin → attempt write → constraint error
Transaction marked failed
Roll back enclosing transaction or appropriate savepoint
Then decide if an operation should be retried
Preserve the first failure and its meaning
A later error about the failed transaction state can hide the original constraint violation. Record the original cause. Retry only when the underlying condition can change and the operation's contract supports it.