From 9a9b43756272b5153a639d94758ec30e06b3c2d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Sat, 16 Dec 2023 13:08:33 +0100 Subject: [PATCH] feat: Add logging for transaction time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/private/DB/Connection.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php index e3fda3e464fdb..d3cc93cdbe009 100644 --- a/lib/private/DB/Connection.php +++ b/lib/private/DB/Connection.php @@ -79,6 +79,8 @@ class Connection extends PrimaryReadReplicaConnection { /** @var DbDataCollector|null */ protected $dbDataCollector = null; + protected ?float $transactionActiveSince = null; + /** * Initializes a new instance of the Connection class. * @@ -306,6 +308,7 @@ protected function logQueryToFile(string $sql): void { // FIXME: Improve to log the actual target db host $isPrimary = $this->connections['primary'] === $this->_conn; $prefix .= ' ' . ($isPrimary === true ? 'primary' : 'replica') . ' '; + $prefix .= ' ' . $this->getTransactionNestingLevel() . ' '; file_put_contents( $this->systemConfig->getValue('query_log_file', ''), @@ -618,4 +621,31 @@ protected function performConnect(?string $connectionName = null): bool { } return $result; } + + public function beginTransaction() { + if (!$this->inTransaction()) { + $this->transactionActiveSince = microtime(true); + } + return parent::beginTransaction(); + } + + public function commit() { + $result = parent::commit(); + if ($this->getTransactionNestingLevel() === 0) { + $timeTook = microtime(true) - $this->transactionActiveSince; + $this->transactionActiveSince = null; + if ($timeTook > 1) { + $this->logger->warning('Transaction took longer then 1s: ' . $timeTook, ['exception' => new \Exception('Long running transaction')]); + } + } + return $result; + } + + public function rollBack() { + $result = parent::rollBack(); + if ($this->getTransactionNestingLevel() === 0) { + $this->transactionActiveSince = null; + } + return $result; + } }