Skip to content

Commit

Permalink
Implementing transaction nesting, this will allow to open multiple tr…
Browse files Browse the repository at this point in the history
…ansactions that will only be commited if all transactions succesfully calls commit()
  • Loading branch information
lorenzo committed Nov 17, 2010
1 parent f2b707a commit 1326707
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions cake/libs/model/datasources/dbo_source.php
Expand Up @@ -1895,14 +1895,14 @@ public function truncate($table) {
/**
* Begin a transaction
*
* @param model $model
* @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions,
* or a transaction has not started).
*/
public function begin(&$model) {
if (parent::begin($model) && $this->execute($this->_commands['begin'])) {
public function begin() {
if ($this->_transactionStarted || $this->_connection->beginTransaction()) {
$this->_transactionStarted = true;
$this->_transactionNesting++;
return true;
}
return false;
Expand All @@ -1911,14 +1911,18 @@ public function begin(&$model) {
/**
* Commit a transaction
*
* @param model $model
* @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions,
* or a transaction has not started).
*/
public function commit(&$model) {
if (parent::commit($model) && $this->execute($this->_commands['commit'])) {
$this->_transactionStarted = false;
public function commit() {
if ($this->_transactionStarted) {
$this->_transactionNesting--;
if ($this->_transactionNesting <= 0) {
$this->_transactionStarted = false;
$this->_transactionNesting = 0;
return $this->_connection->commit();
}
return true;
}
return false;
Expand All @@ -1927,14 +1931,14 @@ public function commit(&$model) {
/**
* Rollback a transaction
*
* @param model $model
* @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions,
* or a transaction has not started).
*/
public function rollback(&$model) {
if (parent::rollback($model) && $this->execute($this->_commands['rollback'])) {
public function rollback() {
if ($this->_transactionStarted && $this->_connection->rollBack()) {
$this->_transactionStarted = false;
$this->_transactionNesting = 0;
return true;
}
return false;
Expand Down

0 comments on commit 1326707

Please sign in to comment.