Skip to content

Commit

Permalink
Merge pull request #48 from dhensby/pulls/nested-transactions
Browse files Browse the repository at this point in the history
FIX Support nested transactions
  • Loading branch information
Damian Mooyman authored Feb 15, 2018
2 parents 406fcee + 6b3959b commit 23d4614
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions code/MSSQLDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ class MSSQLDatabase extends Database
*/
protected $fullTextEnabled = null;

/**
* @var bool
*/
protected $transactionNesting = 0;

/**
* Set the default collation of the MSSQL nvarchar fields that we create.
* We don't apply this to the database as a whole, so that we can use unicode collations.
Expand Down Expand Up @@ -453,11 +458,14 @@ public function supportsExtensions($extensions = array('partitions', 'tablespace
*/
public function transactionStart($transactionMode = false, $sessionCharacteristics = false)
{
if ($this->connector instanceof SQLServerConnector) {
if ($this->transactionNesting > 0) {
$this->transactionSavepoint('NESTEDTRANSACTION' . $this->transactionNesting);
} elseif ($this->connector instanceof SQLServerConnector) {
$this->connector->transactionStart();
} else {
$this->query('BEGIN TRANSACTION');
}
++$this->transactionNesting;
}

public function transactionSavepoint($savepoint)
Expand All @@ -469,19 +477,28 @@ public function transactionRollback($savepoint = false)
{
if ($savepoint) {
$this->query("ROLLBACK TRANSACTION \"$savepoint\"");
} elseif ($this->connector instanceof SQLServerConnector) {
$this->connector->transactionRollback();
} else {
$this->query('ROLLBACK TRANSACTION');
--$this->transactionNesting;
if ($this->transactionNesting > 0) {
$this->transactionRollback('NESTEDTRANSACTION' . $this->transactionNesting);
} elseif ($this->connector instanceof SQLServerConnector) {
$this->connector->transactionRollback();
} else {
$this->query('ROLLBACK TRANSACTION');
}
}
}

public function transactionEnd($chain = false)
{
if ($this->connector instanceof SQLServerConnector) {
$this->connector->transactionEnd();
} else {
$this->query('COMMIT TRANSACTION');
--$this->transactionNesting;
if ($this->transactionNesting <= 0) {
$this->transactionNesting = 0;
if ($this->connector instanceof SQLServerConnector) {
$this->connector->transactionEnd();
} else {
$this->query('COMMIT TRANSACTION');
}
}
}

Expand Down

0 comments on commit 23d4614

Please sign in to comment.