Merge pull request #48 from dhensby/pulls/nested-transactions

FIX Support nested transactions
This commit is contained in:
Damian Mooyman 2018-02-15 14:21:52 +13:00 committed by GitHub
commit 23d4614204
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -78,6 +78,11 @@ class MSSQLDatabase extends Database
*/ */
protected $fullTextEnabled = null; protected $fullTextEnabled = null;
/**
* @var bool
*/
protected $transactionNesting = 0;
/** /**
* Set the default collation of the MSSQL nvarchar fields that we create. * 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. * We don't apply this to the database as a whole, so that we can use unicode collations.
@ -453,11 +458,14 @@ class MSSQLDatabase extends Database
*/ */
public function transactionStart($transactionMode = false, $sessionCharacteristics = false) 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(); $this->connector->transactionStart();
} else { } else {
$this->query('BEGIN TRANSACTION'); $this->query('BEGIN TRANSACTION');
} }
++$this->transactionNesting;
} }
public function transactionSavepoint($savepoint) public function transactionSavepoint($savepoint)
@ -469,19 +477,28 @@ class MSSQLDatabase extends Database
{ {
if ($savepoint) { if ($savepoint) {
$this->query("ROLLBACK TRANSACTION \"$savepoint\""); $this->query("ROLLBACK TRANSACTION \"$savepoint\"");
} elseif ($this->connector instanceof SQLServerConnector) {
$this->connector->transactionRollback();
} else { } 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) public function transactionEnd($chain = false)
{ {
if ($this->connector instanceof SQLServerConnector) { --$this->transactionNesting;
$this->connector->transactionEnd(); if ($this->transactionNesting <= 0) {
} else { $this->transactionNesting = 0;
$this->query('COMMIT TRANSACTION'); if ($this->connector instanceof SQLServerConnector) {
$this->connector->transactionEnd();
} else {
$this->query('COMMIT TRANSACTION');
}
} }
} }