Sequelize Nested Transactions: PostgreSQL and MySQL Savepoint Release Semantics
0 reputation · 07 Feb 2025, 01:59 UTC
0 reputation · 07 Feb 2025, 01:59 UTC
Sequelize emulates nested transactions using SQL savepoints across PostgreSQL, MySQL, and other dialects, but the exact release and cleanup behavior remains underspecified when errors occur after an inner transaction commits. The goal is to understand whether an outer transaction can reliably commit or roll back after a nested transaction throws an error following its savepoint release, given that Sequelize does not expose savepoint names and dialect-specific naming rules differ.
PostgreSQL requires savepoint names to be unique within a transaction, while MySQL permits reuse after release, creating a portability gap that the documentation does not address. Additionally, the internal naming strategy (e.g., sequelize_savepoint_1) is an implementation detail subject to change, preventing application-level recovery. The transaction API offers no hooks to inspect or manually manage the savepoint stack, so applications cannot compensate for dialect divergences.
RELEASE SAVEPOINT but before the outer transaction settles?37275 reputation · 07 Feb 2025, 11:48 UTC
Sequelize's nested transaction implementation does guarantee consistent savepoint cleanup across PostgreSQL and MySQL when an exception occurs after RELEASE SAVEPOINT but before the outer transaction settles. The released savepoint is already destroyed by the database dialect; Sequelize's error handler simply rolls back the outer transaction (or the next active savepoint) without attempting to reference the released name. No savepoint leakage occurs.
Mixing raw SQL savepoint commands with the Sequelize transaction API is unsafe and will corrupt the internal savepoint counter, causing duplicate-name errors in PostgreSQL or incorrect rollback targets in both dialects.
sequelize_savepoint_1, sequelize_savepoint_2 using an internal counter that increments per transaction.createTransaction() call.COMMIT on a nested transaction issues RELEASE SAVEPOINT; ROLLBACK issues ROLLBACK TO SAVEPOINT.RELEASE destroys the savepoint so the name cannot be reused. MySQL permits reuse after RELEASE, but Sequelize's incrementing counter never reuses names anyway.RELEASE, the released savepoint no longer exists in the database. Sequelize's catch handler issues a plain ROLLBACK (outer) or ROLLBACK TO SAVEPOINT for the next active savepoint—never referencing the released name.sequelize.transaction().outer.createTransaction().RELEASE SAVEPOINT).SAVEPOINT does not exist error appears in PostgreSQL logs.DEBUG=sequelize:sql) and verify the emitted sequence matches the expected nesting depth: SAVEPOINT → RELEASE SAVEPOINT → ROLLBACK.Transaction.prototype.savepointName getter to log generated names and confirm the counter increments monotonically.The behavior described applies to Sequelize 6.x. Version 7 may change savepoint naming or transaction manager internals. Connection pooling (HikariCP, etc.) can reuse connections with leftover savepoints if a transaction is abandoned without await—always await the transaction result. CLS (continuation-local-storage) propagation adds implicit nested transactions; test with and without CLS enabled.
Are you using CLS (cls-hooked / @sequelize/core) for automatic transaction propagation? This changes whether nested transactions are created implicitly and affects the savepoint stack depth you need to verify.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.