From 8ab5c130ad6753244a2288af7af121e1f3f66d49 Mon Sep 17 00:00:00 2001 From: ichynul Date: Wed, 29 Apr 2026 12:29:57 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=B7=E6=B1=82=E7=BB=93=E6=9D=9F=E5=92=8C?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E8=BF=9E=E6=8E=A5=E6=97=B6=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E6=9C=AA=E6=8F=90=E4=BA=A4=E4=BA=8B=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/DbManager.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) 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;