feat: add transaction lifecycle callbacks#10144
Open
memleakd wants to merge 1 commit intocodeigniter4:4.8from
Open
feat: add transaction lifecycle callbacks#10144memleakd wants to merge 1 commit intocodeigniter4:4.8from
memleakd wants to merge 1 commit intocodeigniter4:4.8from
Conversation
- Add afterCommit() and afterRollback() callbacks to database connections - Run callbacks only after the outermost transaction commits or rolls back - Discard callbacks registered for the opposite transaction outcome - Document callback exception behavior and automatic rollback handling - Add live database coverage for callback ordering, nesting, and failures Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduction
Hello! I’d like to propose a small database-layer primitive for transaction-aware side effects.
This came up as a concrete need in one of my CodeIgniter 4 projects, where jobs, notifications, events, cache invalidation, and cleanup should happen only after a transaction has actually committed or rolled back.
I kept the implementation intentionally narrow: no queue integration, event dispatcher behavior, outbox abstraction, or configuration surface. Since I am new to contributing here, please treat this as an initial proposal, and apologies in advance if this has already been discussed, rejected, or does not fit the current roadmap.
Description
This PR adds database transaction lifecycle callbacks to database connections:
afterCommit()runs callbacks after the outermost transaction successfully commits.afterRollback()runs callbacks after the outermost transaction rolls back.afterCommit()runs immediately when no transaction is active.afterRollback()is a no-op when no transaction is active.Motivation
Application code can currently perform side effects inside a transaction, but that can be unsafe when the transaction later rolls back. For example, an application may dispatch a queued job, send a notification, publish an event, clear a cache entry, or trigger an external integration before the data it depends on is actually committed.
This can lead to race conditions or inconsistent behavior, such as:
By adding transaction lifecycle callbacks to the database layer, applications and future framework libraries can safely defer these side effects until after the outermost transaction commits or rolls back.
Use Cases
Common use cases include:
Behavior Notes
Callbacks are tied to the outermost transaction. Nested transaction callbacks do not run until the outermost transaction commits or rolls back.
Only callbacks for the final transaction outcome run: commit discards rollback callbacks, and rollback discards commit callbacks.
If a callback throws, the exception bubbles to the caller after the database transaction has already committed or rolled back, so the callback exception does not change the database outcome.
Testing
Added live database tests covering commit callbacks, rollback callbacks, nested transactions, callback exceptions, and automatic rollback behavior.
Checklist