Skip to content

feat: Add transactional method to manage closures within transactions#99

Merged
marcreichel merged 5 commits into
4.0.xfrom
feat/transaction-closure
Jun 9, 2026
Merged

feat: Add transactional method to manage closures within transactions#99
marcreichel merged 5 commits into
4.0.xfrom
feat/transaction-closure

Conversation

@marcreichel

@marcreichel marcreichel commented Jun 7, 2026

Copy link
Copy Markdown
Member

Summary

Adds a transactional() method to Connection that runs a Closure inside a database transaction, committing on success and rolling back on any Throwable before rethrowing. This removes the need for callers to manually pair beginTransaction() / commit() / rollBack() around their logic.

Changes

  • src/Connection.php: Implements transactional(Closure $callback): mixed. Begins a transaction, invokes the callback, rolls back and rethrows on Throwable, otherwise commits and returns the callback's result.
  • src/DoctrineConnectionInterface.php: Adds the transactional() method to the interface with a generic @template TReturn annotation so the return type is preserved for static analysis.
  • src/MockConnection.php: Provides a no-op implementation that simply invokes the callback (no real transaction handling needed in tests).

Usage

$result = $connection->transactional(function () use ($connection) {
    $connection->insert(...);
    $connection->update(...);

    return $someValue;
});

@marcreichel marcreichel requested review from romankonz and sidler June 8, 2026 09:10
@marcreichel marcreichel marked this pull request as ready for review June 8, 2026 09:10
sidler
sidler previously approved these changes Jun 8, 2026
@marcreichel marcreichel enabled auto-merge June 8, 2026 12:04
Comment thread src/Connection.php Outdated
romankonz
romankonz previously approved these changes Jun 9, 2026
@marcreichel marcreichel dismissed stale reviews from romankonz and sidler via 5d24c80 June 9, 2026 11:08
@marcreichel marcreichel requested review from romankonz and sidler June 9, 2026 11:13
@marcreichel marcreichel changed the title feat: Add transaction method to manage closures within transactions feat: Add transactional method to manage closures within transactions Jun 9, 2026
@marcreichel marcreichel disabled auto-merge June 9, 2026 11:48
@marcreichel marcreichel merged commit 34605d1 into 4.0.x Jun 9, 2026
10 checks passed
@marcreichel marcreichel deleted the feat/transaction-closure branch June 9, 2026 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants