8000 beginTransaction(): Reconnect if connection is lost · laravel/framework@695e4c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 695e4c0

Browse files
committed
beginTransaction(): Reconnect if connection is lost
If we're beginning a new transaction, we should check if the exception was caused by a lost connection. If so, then try again (like we do when when running a query/statement). We won't do a reconnect if it is a nested transaction though, because if the connection has been lost then the transaction has already been rolled back. (Queries/statements already won't try to reconnect if they're inside a transaction, either.)
1 parent eff1791 commit 695e4c0

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/Illuminate/Database/Connection.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -498,26 +498,30 @@ public function transaction(Closure $callback)
498498
* Start a new database transaction.
499499
*
500500
* @return void
501-
* @throws Exception
501+
*
502+
* @throws \Exception
502503
*/
503504
public function beginTransaction()
504505
{
505-
++$this->transactions;
506-
507-
if ($this->transactions == 1) {
506+
if ($this->transactions == 0) {
508507
try {
509-
$this->pdo->beginTransaction();
508+
$this->getPdo()->beginTransaction();
510509
} catch (Exception $e) {
511-
--$this->transactions;
512-
513-
throw $e;
510+
if ($this->causedByLostConnection($e)) {
511+
$this->reconnect();
512+
$this->getPdo()->beginTransaction();
513+
} else {
514+
throw $e;
515+
}
514516
}
515-
} elseif ($this->transactions > 1 && $this->queryGrammar->supportsSavepoints()) {
517+
} elseif ($this->transactions >= 1 && $this->queryGrammar->supportsSavepoints()) {
516518
$this->pdo->exec(
517-
$this->queryGrammar->compileSavepoint('trans'.$this->transactions)
519+
$this->queryGrammar->compileSavepoint('trans'.($this->transactions + 1))
518520
);
519521
}
520522

523+
++$this->transactions;
524+
521525
$this->fireConnectionEvent('beganTransaction');
522526
}
523527

@@ -866,6 +870,8 @@ public function getReadPdo()
866870
*
867871
* @param \PDO|null $pdo
868872
* @return $this
873+
*
874+
* @throws \RuntimeException
869875
*/
870876
public function setPdo($pdo)
871877
{

tests/Database/DatabaseConnectionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ public function testAffectingStatementProperlyCallsPDO()
111111
$this->assertTrue(is_numeric($log[0]['time']));
112112
}
113113

114-
public function testTransactionsDecrementedOnTransactionException()
114+
public function testTransactionLevelNotIncrementedOnTransactionException()
115115
{
116116
$pdo = $this->getMock('DatabaseConnectionTestMockPDO');
117-
$pdo->expects($this->once())->method('beginTransaction')->will($this->throwException(new ErrorException('MySQL server has gone away')));
117+
$pdo->expects($this->once())->method('beginTransaction')->will($this->throwException(new Exception));
118118
$connection = $this->getMockConnection([], $pdo);
119119
try {
120120
$connection->beginTransaction();
121-
} catch (ErrorException $e) {
121+
} catch (Exception $e) {
122122
$this->assertEquals(0, $connection->transactionLevel());
123123
}
124124
}

0 commit comments

Comments
 (0)
0