Skip to content

feat: add database transaction state check#10152

Merged
michalsn merged 2 commits into
codeigniter4:4.8from
memleakd:feat/database-in-transaction
May 6, 2026
Merged

feat: add database transaction state check#10152
michalsn merged 2 commits into
codeigniter4:4.8from
memleakd:feat/database-in-transaction

Conversation

@memleakd

@memleakd memleakd commented May 2, 2026

Copy link
Copy Markdown
Contributor

Description

This PR proposes adding inTransaction() to database connections so code can check whether the connection is currently inside an active CodeIgniter-managed transaction.

The method provides a small public API for transaction state without exposing transaction depth or requiring userland code to rely on internal connection properties. This is useful for reusable services, libraries, and framework-adjacent integrations that need to adapt when they are called from inside an existing transaction.

For example, a package may need to decide whether to perform work immediately, join caller-managed transactional behavior, or defer side effects through transaction callbacks.

Behavior

  • Returns true while the connection is inside a CodeIgniter-managed transaction.
  • Returns false when no CodeIgniter-managed transaction is active.
  • Returns false when transactions are disabled.
  • Reflects nested transaction state through the existing transaction depth behavior.
  • Returns false inside afterCommit() and afterRollback() callbacks because the transaction has already reached its terminal state.

This is intentionally a boolean state check only. It does not expose the transaction depth, and it reflects transactions started through CodeIgniter’s transaction APIs rather than raw SQL or driver-specific transaction handling.

This adds inTransaction() to ConnectionInterface, so custom implementations will need to add the new method. The changelog lists this under interface changes.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label May 2, 2026
@michalsn michalsn added the breaking change Pull requests that may break existing functionalities label May 4, 2026
Comment thread user_guide_src/source/database/transactions.rst Outdated
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
@michalsn michalsn merged commit 053da6d into codeigniter4:4.8 May 6, 2026
56 checks passed
@memleakd memleakd deleted the feat/database-in-transaction branch May 6, 2026 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch. breaking change Pull requests that may break existing functionalities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants