Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Support nested transactions #48

Merged
merged 1 commit into from Feb 15, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 25 additions & 8 deletions code/MSSQLDatabase.php
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