10000 Add tests for beginTransaction() retrying · laravel/framework@33c6f9a · GitHub
[go: up one dir, main page]

Skip to content

Commit 33c6f9a

Browse files
committed
Add tests for beginTransaction() retrying
1 parent 695e4c0 commit 33c6f9a

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/Illuminate/Database/Connection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,11 +505,11 @@ public function beginTransaction()
505505
{
506506
if ($this->transactions == 0) {
507507
try {
508-
$this->getPdo()->beginTransaction();
508+
$this->pdo->beginTransaction();
509509
} catch (Exception $e) {
510510
if ($this->causedByLostConnection($e)) {
511511
$this->reconnect();
512-
$this->getPdo()->beginTransaction();
512+
$this->pdo->beginTransaction();
513513
} else {
514514
throw $e;
515515
}

tests/Database/DatabaseConnectionTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,36 @@ public function testTransactionLevelNotIncrementedOnTransactionException()
123123
}
124124
}
125125

126+
public function testBeginTransactionMethodRetriesOnFailure()
127+
{
128+
$pdo = $this->getMock('DatabaseConnectionTestMockPDO');
129+
$pdo->expects($this->exactly(2))->method('beginTransaction');
130+
$pdo->expects($this->at(0))->method('beginTransaction')->will($this->throwException(new ErrorException('server has gone away')));
131+
$connection = $this->getMockConnection(['reconnect'], $pdo);
132+
$connection->expects($this->once())->method('reconnect');
133+
$connection->beginTransaction();
134+
$this->assertEquals(1, $connection->transactionLevel());
135+
}
136+
137+
public function testBeginTransactionMethodNeverRetriesIfWithinTransaction()
138+
{
139+
$pdo = $this->getMock('DatabaseConnectionTestMockPDO');
140+
$pdo->expects($this->once())->method('beginTransaction');
141+
$pdo->expects($this->once())->method('exec')->will($this->throwException(new Exception));
142+
$connection = $this->getMockConnection([], $pdo);
143+
$queryGrammar = $this->getMock('Illuminate\Database\Query\Grammars\Grammar');
144+
$queryGrammar->expects($this->once())->method('supportsSavepoints')->will($this->returnValue(true));
145+
$connection->setQueryGrammar($queryGrammar);
146+
$connection->expects($this->never())->method('reconnect');
147+
$connection->beginTransaction();
148+
$this->assertEquals(1, $connection->transactionLeve 7D7D l());
149+
try {
150+
$connection->beginTransaction();
151+
} catch (Exception $e) {
152+
$this->assertEquals(1, $connection->transactionLevel());
153+
}
154+
}
155+
126156
/**
127157
* @expectedException RuntimeException
128158
*/

0 commit comments

Comments
 (0)
0