diff options
author | Julius Härtl <jus@bitgrid.net> | 2023-12-16 13:08:33 +0100 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2024-01-12 11:02:50 +0100 |
commit | 3e60092b7d0e051f1384fcae172b9e4f1b30c137 (patch) | |
tree | 38bfc2ee12a63705ffcc73a436602f37fffaefef /lib/private/DB/Connection.php | |
parent | 239213abfe59dc749129d4b0728c191dcc636513 (diff) | |
download | nextcloud-server-3e60092b7d0e051f1384fcae172b9e4f1b30c137.tar.gz nextcloud-server-3e60092b7d0e051f1384fcae172b9e4f1b30c137.zip |
feat: Add logging for transaction time
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'lib/private/DB/Connection.php')
-rw-r--r-- | lib/private/DB/Connection.php | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php index e3fda3e464f..1ea2eda9d01 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 @@ class Connection extends PrimaryReadReplicaConnection { // 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,35 @@ class Connection extends PrimaryReadReplicaConnection { } 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 than 1s: ' . $timeTook, ['exception' => new \Exception('Long running transaction')]); + } + } + return $result; + } + + public function rollBack() { + $result = parent::rollBack(); + if ($this->getTransactionNestingLevel() === 0) { + $timeTook = microtime(true) - $this->transactionActiveSince; + $this->transactionActiveSince = null; + if ($timeTook > 1) { + $this->logger->warning('Transaction rollback took longer than 1s: ' . $timeTook, ['exception' => new \Exception('Long running transaction rollback')]); + } + } + return $result; + } } |