diff --git a/src/DbManager.php b/src/DbManager.php index 6f36d1f..a29654c 100644 --- a/src/DbManager.php +++ b/src/DbManager.php @@ -11,6 +11,7 @@ use think\db\ConnectionInterface; use think\db\BaseQuery; use think\db\Query; +use support\Log; /** * Class DbManager. @@ -75,7 +76,16 @@ protected function instance(array|string|null $name = null, bool $force = false) } finally { Context::onDestroy(function () use ($connection, $name) { try { - $connection && static::$pools[$name]->put($connection); + if ($connection) { + if (method_exists($connection, 'getPdo')) { + $pdo = $connection->getPdo(); + if ($pdo && $pdo->inTransaction()) { + $connection->rollBack(); + Log::error("Uncommitted transaction found and try to rollback for connection: $name"); + } + } + static::$pools[$name]->put($connection); + } } catch (Throwable) { // ignore } @@ -131,6 +141,17 @@ private function getHeartbeatSql(string $type): string */ protected function closeConnection(ConnectionInterface $connection, ?string $name = null): void { + if (method_exists($connection, 'getPdo')) { + try { + $pdo = $connection->getPdo(); + if ($pdo && $pdo->inTransaction()) { + $connection->rollBack(); + Log::error("Uncommitted transaction found and try to rollback for connection: $name"); + } + } catch (Throwable) { + // ignore + } + } $connection->close(); $clearProperties = function () { $this->db = null;