Skip to content

Commit 62a313a

Browse files
committed
revert on failure
1 parent f7f5215 commit 62a313a

File tree

4 files changed

+5
-52
lines changed

4 files changed

+5
-52
lines changed

src/Illuminate/Database/Concerns/ManagesTransactions.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function transaction(Closure $callback, $attempts = 1, ?Closure $onFailur
3838
// exception back out, and let the developer handle an uncaught exception.
3939
catch (Throwable $e) {
4040
$this->handleTransactionException(
41-
$e, $currentAttempt, $attempts, $onFailure
41+
$e, $currentAttempt, $attempts
4242
);
4343

4444
continue;
@@ -79,12 +79,11 @@ public function transaction(Closure $callback, $attempts = 1, ?Closure $onFailur
7979
* @param \Throwable $e
8080
* @param int $currentAttempt
8181
* @param int $maxAttempts
82-
* @param Closure|null $onFailure
8382
* @return void
8483
*
8584
* @throws \Throwable
8685
*/
87-
protected function handleTransactionException(Throwable $e, $currentAttempt, $maxAttempts, ?Closure $onFailure)
86+
protected function handleTransactionException(Throwable $e, $currentAttempt, $maxAttempts)
8887
{
8988
// On a deadlock, MySQL rolls back the entire transaction so we can't just
9089
// retry the query. We have to throw this exception all the way out and
@@ -110,10 +109,6 @@ protected function handleTransactionException(Throwable $e, $currentAttempt, $ma
110109
return;
111110
}
112111

113-
if ($onFailure !== null) {
114-
$onFailure($e);
115-
}
116-
117112
throw $e;
118113
}
119114

src/Illuminate/Database/ConnectionInterface.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,11 @@ public function prepareBindings(array $bindings);
131131
*
132132
* @param \Closure $callback
133133
* @param int $attempts
134-
* @param Closure|null $onFailure
135134
* @return mixed
136135
*
137136
* @throws \Throwable
138137
*/
139-
public function transaction(Closure $callback, $attempts = 1, ?Closure $onFailure = null);
138+
public function transaction(Closure $callback, $attempts = 1);
140139

141140
/**
142141
* Start a new database transaction.

src/Illuminate/Database/SqlServerConnection.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,15 @@ public function getDriverTitle()
2727
*
2828
* @param \Closure $callback
2929
* @param int $attempts
30-
* @param Closure|null $onFailure
3130
* @return mixed
3231
*
3332
* @throws \Throwable
3433
*/
35-
public function transaction(Closure $callback, $attempts = 1, ?Closure $onFailure = null)
34+
public function transaction(Closure $callback, $attempts = 1)
3635
{
3736
for ($a = 1; $a <= $attempts; $a++) {
3837
if ($this->getDriverName() === 'sqlsrv') {
39-
return parent::transaction($callback, $attempts, $onFailure);
38+
return parent::transaction($callback, $attempts);
4039
}
4140

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

59-
if ($a === $attempts && $onFailure !== null) {
60-
$onFailure($e);
61-
}
62-
6358
throw $e;
6459
}
6560

tests/Integration/Database/DatabaseTransactionsTest.php

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -105,42 +105,6 @@ public function testTransactionsDoNotAffectDifferentConnections()
105105
$this->assertTrue($secondObject->ran);
106106
$this->assertFalse($thirdObject->ran);
107107
}
108-
109-
public function testOnErrorCallbackIsCalled()
110-
{
111-
$executed = false;
112-
try {
113-
DB::transaction(function () {
114-
throw new \Exception;
115-
}, 1, function () use (&$executed) {
116-
$executed = true;
117-
});
118-
} catch (\Throwable) {
119-
// Ignore the exception
120-
}
121-
122-
$this->assertTrue($executed);
123-
}
124-
125-
public function testOnErrorCallbackIsCalledWithDeadlockRetry()
126-
{
127-
$executed = false;
128-
$attempts = 0;
129-
130-
try {
131-
DB::transaction(function () use (&$attempts) {
132-
$attempts += 1;
133-
throw new \Exception('has been chosen as the deadlock victim');
134-
}, 3, function () use (&$executed) {
135-
$executed = true;
136-
});
137-
} catch (\Throwable) {
138-
// Ignore the exception
139-
}
140-
141-
$this->assertSame(3, $attempts);
142-
$this->assertTrue($executed);
143-
}
144108
}
145109

146110
class TestObjectForTransactions

0 commit comments

Comments
 (0)