Skip to content

feat: Add a callback to be called on transaction failure #55338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Illuminate/Database/Concerns/ManagesTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ trait ManagesTransactions
*
* @param (\Closure(static): TReturn) $callback
* @param int $attempts
* @param Closure|null $onFailure
* @return TReturn
*
* @throws \Throwable
*/
public function transaction(Closure $callback, $attempts = 1)
public function transaction(Closure $callback, $attempts = 1, ?Closure $onFailure = null)
{
for ($currentAttempt = 1; $currentAttempt <= $attempts; $currentAttempt++) {
$this->beginTransaction();
Expand All @@ -37,7 +38,7 @@ public function transaction(Closure $callback, $attempts = 1)
// exception back out, and let the developer handle an uncaught exception.
catch (Throwable $e) {
$this->handleTransactionException(
$e, $currentAttempt, $attempts
$e, $currentAttempt, $attempts, $onFailure
);

continue;
Expand Down Expand Up @@ -78,11 +79,12 @@ public function transaction(Closure $callback, $attempts = 1)
* @param \Throwable $e
* @param int $currentAttempt
* @param int $maxAttempts
* @param Closure|null $onFailure
* @return void
*
* @throws \Throwable
*/
protected function handleTransactionException(Throwable $e, $currentAttempt, $maxAttempts)
protected function handleTransactionException(Throwable $e, $currentAttempt, $maxAttempts, ?Closure $onFailure)
{
// On a deadlock, MySQL rolls back the entire transaction so we can't just
// retry the query. We have to throw this exception all the way out and
Expand All @@ -108,6 +110,10 @@ protected function handleTransactionException(Throwable $e, $currentAttempt, $ma
return;
}

if ($onFailure !== null) {
$onFailure($e);
}

throw $e;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Illuminate/Database/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,12 @@ public function prepareBindings(array $bindings);
*
* @param \Closure $callback
* @param int $attempts
* @param Closure|null $onFailure
* @return mixed
*
* @throws \Throwable
*/
public function transaction(Closure $callback, $attempts = 1);
public function transaction(Closure $callback, $attempts = 1, ?Closure $onFailure = null);

/**
* Start a new database transaction.
Expand Down
9 changes: 7 additions & 2 deletions src/Illuminate/Database/SqlServerConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ public function getDriverTitle()
*
* @param \Closure $callback
* @param int $attempts
* @param Closure|null $onFailure
* @return mixed
*
* @throws \Throwable
*/
public function transaction(Closure $callback, $attempts = 1)
public function transaction(Closure $callback, $attempts = 1, ?Closure $onFailure = null)
{
for ($a = 1; $a <= $attempts; $a++) {
if ($this->getDriverName() === 'sqlsrv') {
return parent::transaction($callback, $attempts);
return parent::transaction($callback, $attempts, $onFailure);
}

$this->getPdo()->exec('BEGIN TRAN');
Expand All @@ -55,6 +56,10 @@ public function transaction(Closure $callback, $attempts = 1)
catch (Throwable $e) {
$this->getPdo()->exec('ROLLBACK TRAN');

if ($a === $attempts && $onFailure !== null) {
$onFailure($e);
}

throw $e;
}

Expand Down
36 changes: 36 additions & 0 deletions tests/Integration/Database/DatabaseTransactionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,42 @@ public function testTransactionsDoNotAffectDifferentConnections()
$this->assertTrue($secondObject->ran);
$this->assertFalse($thirdObject->ran);
}

public function testOnErrorCallbackIsCalled()
{
$executed = false;
try {
DB::transaction(function () {
throw new \Exception;
}, 1, function () use (&$executed) {
$executed = true;
});
} catch (\Throwable) {
// Ignore the exception
}

$this->assertTrue($executed);
}

public function testOnErrorCallbackIsCalledWithDeadlockRetry()
{
$executed = false;
$attempts = 0;

try {
DB::transaction(function () use (&$attempts) {
$attempts += 1;
throw new \Exception('has been chosen as the deadlock victim');
}, 3, function () use (&$executed) {
$executed = true;
});
} catch (\Throwable) {
// Ignore the exception
}

$this->assertSame(3, $attempts);
$this->assertTrue($executed);
}
}

class TestObjectForTransactions
Expand Down
Loading