diff options
author | mattab <matthieu.aubry@gmail.com> | 2014-06-02 11:28:15 +0400 |
---|---|---|
committer | mattab <matthieu.aubry@gmail.com> | 2014-06-02 11:28:15 +0400 |
commit | d9062d3ed16d5240addc6a558192e04747d1c999 (patch) | |
tree | 292b848e93bea9b1c91c56a9c7d43515195ebca9 /core | |
parent | 1c85b8aa050e085ec94f2a2d8d0920f4e9ea69f6 (diff) | |
parent | 657a35e182b2b1441af80649695a10cd689eff1f (diff) |
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'core')
-rw-r--r-- | core/Tracker.php | 49 | ||||
-rw-r--r-- | core/Tracker/Db/Mysqli.php | 56 | ||||
-rw-r--r-- | core/Tracker/Db/Pdo/Mysql.php | 53 |
3 files changed, 157 insertions, 1 deletions
diff --git a/core/Tracker.php b/core/Tracker.php index 421d81c3f1..48851007f1 100644 --- a/core/Tracker.php +++ b/core/Tracker.php @@ -193,7 +193,6 @@ class Tracker } if (!empty($this->requests)) { - foreach ($this->requests as &$request) { // if a string is sent, we assume its a URL and try to parse it if (is_string($request)) { @@ -236,18 +235,23 @@ class Tracker $this->initOutputBuffer(); if (!empty($this->requests)) { + $this->beginTransaction(); try { foreach ($this->requests as $params) { $isAuthenticated = $this->trackRequest($params, $tokenAuth); } $this->runScheduledTasksIfAllowed($isAuthenticated); + $this->commitTransaction(); } catch(DbException $e) { Common::printDebug($e->getMessage()); + $this->rollbackTransaction(); } + } else { $this->handleEmptyRequest(new Request($_GET + $_POST)); } + $this->end(); $this->flushOutputBuffer(); @@ -268,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() { @@ -865,4 +910,6 @@ class Tracker { return file_get_contents("php://input"); } + + } diff --git a/core/Tracker/Db/Mysqli.php b/core/Tracker/Db/Mysqli.php index bc1d4bddd3..a815636047 100644 --- a/core/Tracker/Db/Mysqli.php +++ b/core/Tracker/Db/Mysqli.php @@ -25,6 +25,8 @@ class Mysqli extends Db protected $username; protected $password; protected $charset; + protected $activeTransaction = false; + /** * Builds the DB object @@ -276,4 +278,58 @@ class Mysqli extends Db { return mysqli_affected_rows($this->connection); } + + /** + * Start Transaction + * @return string TransactionID + */ + + public function beginTransaction() + { + if(!$this->activeTransaction === false ) { + return; + } + + if( $this->connection->autocommit(false) ) { + $this->activeTransaction = uniqid(); + return $this->activeTransaction; + } + } + + /** + * Commit Transaction + * @param string TransactionID from beginTransaction + */ + + public function commit($xid) + { + if($this->activeTransaction != $xid || $this->activeTransaction === false ) { + + return; + } + $this->activeTransaction = false; + + if(!$this->connection->commit() ) { + throw new DbException("Commit failed"); + } + $this->connection->autocommit(true); + } + + /** + * Rollback Transaction + * @param string TransactionID from beginTransaction + */ + + public function rollBack($xid) + { + if($this->activeTransaction != $xid || $this->activeTransaction === false ) { + return; + } + $this->activeTransaction = false; + + if(!$this->connection->rollback() ) { + throw new DbException("Rollback failed"); + } + $this->connection->autocommit(true); + } } diff --git a/core/Tracker/Db/Pdo/Mysql.php b/core/Tracker/Db/Pdo/Mysql.php index 3a2e04abfd..4fc78527ac 100644 --- a/core/Tracker/Db/Pdo/Mysql.php +++ b/core/Tracker/Db/Pdo/Mysql.php @@ -30,6 +30,8 @@ class Mysql extends Db protected $password; protected $charset; + protected $activeTransaction = false; + /** * Builds the DB object * @@ -234,4 +236,55 @@ class Mysql extends Db { return $queryResult->rowCount(); } + + /** + * Start Transaction + * @return string TransactionID + */ + + public function beginTransaction() + { + if(!$this->activeTransaction === false ) { + return; + } + + if( $this->connection->beginTransaction() ) { + $this->activeTransaction = uniqid(); + return $this->activeTransaction; + } + } + + /** + * Commit Transaction + * @param string TransactionID from beginTransaction + */ + + public function commit($xid) + { + if($this->activeTransaction != $xid || $this->activeTransaction === false ) { + return; + } + $this->activeTransaction = false; + + if(!$this->connection->commit() ) { + throw new DbException("Commit failed"); + } + } + + /** + * Rollback Transaction + * @param string TransactionID from beginTransaction + */ + + public function rollBack($xid) + { + if($this->activeTransaction != $xid || $this->activeTransaction === false ) { + return; + } + $this->activeTransaction = false; + + if(!$this->connection->rollBack() ) { + throw new DbException("Rollback failed"); + } + } } |