Skip to content

Commit

Permalink
Reset transaction level to zero, when reconnent to mysql server. (#1565)
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo committed Apr 15, 2020
1 parent 825f4ff commit 112e271
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -2,7 +2,8 @@

## Fixed

- [#1563](https://github.com/hyperf/hyperf/pull/1563) Fixed crontab's `onOneServer` option not resetting mutex on shutdown
- [#1563](https://github.com/hyperf/hyperf/pull/1563) Fixed crontab's `onOneServer` option not resetting mutex on shutdown.
- [#1565](https://github.com/hyperf/hyperf/pull/1565) Reset transaction level to zero, when reconnent to mysql server.

# v1.1.25 - 2020-04-09

Expand Down
4 changes: 4 additions & 0 deletions src/db/src/AbstractConnection.php
Expand Up @@ -58,6 +58,10 @@ public function getActiveConnection()

public function retry(\Throwable $throwable, $name, $arguments)
{
if ($this->transactionLevel() > 0) {
throw $throwable;
}

if ($this->causedByLostConnection($throwable)) {
try {
$this->reconnect();
Expand Down
2 changes: 1 addition & 1 deletion src/db/src/MySQLConnection.php
Expand Up @@ -78,7 +78,7 @@ public function reconnect(): bool

$this->connection = $connection;
$this->lastUseTime = microtime(true);

$this->transactions = 0;
return true;
}

Expand Down
1 change: 1 addition & 0 deletions src/db/src/PDOConnection.php
Expand Up @@ -93,6 +93,7 @@ public function reconnect(): bool

$this->connection = $pdo;
$this->lastUseTime = microtime(true);
$this->transactions = 0;
return true;
}

Expand Down
13 changes: 13 additions & 0 deletions src/db/tests/Cases/PDODriverTest.php
Expand Up @@ -115,4 +115,17 @@ public function testStaticCall()

$this->assertSame('Hyperf', $res['name']);
}

public function testTransactionLevelWhenReconnect()
{
$container = $this->getContainer();
$factory = $container->get(PoolFactory::class);
$pool = $factory->getPool('default');
$connection = $pool->get();
$this->assertSame(0, $connection->transactionLevel());
$connection->beginTransaction();
$this->assertSame(1, $connection->transactionLevel());
$connection->reconnect();
$this->assertSame(0, $connection->transactionLevel());
}
}

0 comments on commit 112e271

Please sign in to comment.