Skip to content

feat: add database transaction state check#10152

Open
memleakd wants to merge 1 commit intocodeigniter4:4.8from
memleakd:feat/database-in-transaction
Open

feat: add database transaction state check#10152
memleakd wants to merge 1 commit intocodeigniter4:4.8from
memleakd:feat/database-in-transaction

Conversation

@memleakd
Copy link
Copy Markdown
Contributor

@memleakd memleakd commented May 2, 2026

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
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants