You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 5d509dc
Browse filesBrowse the repository at this point in the historyBrowse files
Add transaction() method and support nested transactions
transaction() runs a function in a transaction, which is committed if
the function returns (returning its value), or rolled back if it throws
an exception (which is rethrown).
Transactions can now be nested: if a transaction has already been
started (including directly with PDO), begin() creates a savepoint
instead of failing, and the matching commit() or rollback() releases or
rolls back to it. So a nested transaction() call which throws only rolls
back its own changes, and the outer transaction can continue. This
matters especially with PostgreSQL, which otherwise rejects further
queries in a transaction after an error. Like Doctrine and Laravel, the
nesting level is tracked by the PeachySql instance, so only one instance
should be used for each connection.
If an error rolled back the entire transaction (e.g. a deadlock with SQL
Server or MySQL), the savepoint no longer exists, so the nested commit()
or rollback() throws a TransactionRolledBackException (like Laravel's
DeadlockException), which transaction() wraps around the original
exception. This is detected by the savepoint statement failing rather
than by specific error codes, so with PostgreSQL, where a deadlock only
aborts the transaction and the savepoint can still be rolled back to,
the outer transaction can continue. SqlException now accepts an optional
previous exception.
The savepoint statements are executed directly, since MySQL doesn't
support them in prepared statements. A failed rollback in transaction()
doesn't replace the original exception. Batched inserts, updates, and
deletes now use transaction(), replacing the private runInTransaction()
method.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
0 commit comments