Implementing transaction nesting, this will allow to open multiple transactions that will only be commited if all transactions succesfully calls commit()

This commit is contained in:
José Lorenzo Rodríguez 2010-11-16 22:56:23 -04:30
parent f2b707ac0b
commit 1326707c9d

View file

@ -1895,14 +1895,14 @@ class DboSource extends DataSource {
/** /**
* Begin a transaction * Begin a transaction
* *
* @param model $model
* @return boolean True on success, false on fail * @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions, * (i.e. if the database/model does not support transactions,
* or a transaction has not started). * or a transaction has not started).
*/ */
public function begin(&$model) { public function begin() {
if (parent::begin($model) && $this->execute($this->_commands['begin'])) { if ($this->_transactionStarted || $this->_connection->beginTransaction()) {
$this->_transactionStarted = true; $this->_transactionStarted = true;
$this->_transactionNesting++;
return true; return true;
} }
return false; return false;
@ -1911,14 +1911,18 @@ class DboSource extends DataSource {
/** /**
* Commit a transaction * Commit a transaction
* *
* @param model $model
* @return boolean True on success, false on fail * @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions, * (i.e. if the database/model does not support transactions,
* or a transaction has not started). * or a transaction has not started).
*/ */
public function commit(&$model) { public function commit() {
if (parent::commit($model) && $this->execute($this->_commands['commit'])) { if ($this->_transactionStarted) {
$this->_transactionStarted = false; $this->_transactionNesting--;
if ($this->_transactionNesting <= 0) {
$this->_transactionStarted = false;
$this->_transactionNesting = 0;
return $this->_connection->commit();
}
return true; return true;
} }
return false; return false;
@ -1927,14 +1931,14 @@ class DboSource extends DataSource {
/** /**
* Rollback a transaction * Rollback a transaction
* *
* @param model $model
* @return boolean True on success, false on fail * @return boolean True on success, false on fail
* (i.e. if the database/model does not support transactions, * (i.e. if the database/model does not support transactions,
* or a transaction has not started). * or a transaction has not started).
*/ */
public function rollback(&$model) { public function rollback() {
if (parent::rollback($model) && $this->execute($this->_commands['rollback'])) { if ($this->_transactionStarted && $this->_connection->rollBack()) {
$this->_transactionStarted = false; $this->_transactionStarted = false;
$this->_transactionNesting = 0;
return true; return true;
} }
return false; return false;