diff options
author | Matthieu Aubry <matt@piwik.org> | 2014-06-02 08:40:23 +0400 |
---|---|---|
committer | Matthieu Aubry <matt@piwik.org> | 2014-06-02 08:40:23 +0400 |
commit | e20bd1b463abc3d03fcf140f101b964ebe76a223 (patch) | |
tree | da6aa826894f12d3cfd53dd5d4d03190f4f6d000 /core/Tracker.php | |
parent | e67b57caa5384866aa31565d66e2092839148db1 (diff) |
Only use transactions for bulk requests (more than one request)
Diffstat (limited to 'core/Tracker.php')
-rw-r--r-- | core/Tracker.php | 49 |
1 files 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"); } + + } |