From e20bd1b463abc3d03fcf140f101b964ebe76a223 Mon Sep 17 00:00:00 2001 From: Matthieu Aubry <matt@piwik.org> Date: Mon, 2 Jun 2014 16:40:23 +1200 Subject: [PATCH] Only use transactions for bulk requests (more than one request) --- core/Tracker.php | 49 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/core/Tracker.php b/core/Tracker.php index af6bd1db8a..1d9b9a66a5 100644 --- a/core/Tracker.php +++ b/core/Tracker.php @@ -235,17 +235,17 @@ class Tracker $this->initOutputBuffer(); if (!empty($this->requests)) { - $xid = self::getDatabase()->beginTransaction(); + $this->beginTransaction(); try { foreach ($this->requests as $params) { $isAuthenticated = $this->trackRequest($params, $tokenAuth); } $this->runScheduledTasksIfAllowed($isAuthenticated); - self::getDatabase()->commit($xid); + $this->commitTransaction(); } catch(DbException $e) { Common::printDebug($e->getMessage()); - self::getDatabase()->rollback($xid); + $this->rollbackTransaction(); } } else { @@ -272,6 +272,47 @@ class Tracker return ob_get_contents(); } + protected function beginTransaction() + { + $this->transactionId = null; + if(!$this->shouldUseTransactions()) { + return; + } + $this->transactionId = self::getDatabase()->beginTransaction(); + } + + protected function commitTransaction() + { + if(empty($this->transactionId)) { + return; + } + self::getDatabase()->commit($this->transactionId); + } + + protected function rollbackTransaction() + { + if(empty($this->transactionId)) { + return; + } + self::getDatabase()->rollback($this->transactionId); + } + + /** + * @return bool + */ + protected function shouldUseTransactions() + { + $isBulkRequest = count($this->requests) > 1; + return $isBulkRequest && $this->isTransactionSupported(); + } + + /** + * @return bool + */ + protected function isTransactionSupported() + { + return (bool) Config::getInstance()->Tracker['bulk_requests_use_transaction']; + } protected function shouldRunScheduledTasks() { @@ -869,4 +910,6 @@ class Tracker { return file_get_contents("php://input"); } + + } -- GitLab