FIX Support nested transactions

This commit is contained in:
Daniel Hensby 2018-02-08 21:37:59 +00:00
parent 406fcee3cd
commit 6b3959ba48
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E

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,21 +477,30 @@ class MSSQLDatabase extends Database
{ {
if ($savepoint) { if ($savepoint) {
$this->query("ROLLBACK TRANSACTION \"$savepoint\""); $this->query("ROLLBACK TRANSACTION \"$savepoint\"");
} else {
--$this->transactionNesting;
if ($this->transactionNesting > 0) {
$this->transactionRollback('NESTEDTRANSACTION' . $this->transactionNesting);
} elseif ($this->connector instanceof SQLServerConnector) { } elseif ($this->connector instanceof SQLServerConnector) {
$this->connector->transactionRollback(); $this->connector->transactionRollback();
} else { } else {
$this->query('ROLLBACK TRANSACTION'); $this->query('ROLLBACK TRANSACTION');
} }
} }
}
public function transactionEnd($chain = false) public function transactionEnd($chain = false)
{ {
--$this->transactionNesting;
if ($this->transactionNesting <= 0) {
$this->transactionNesting = 0;
if ($this->connector instanceof SQLServerConnector) { if ($this->connector instanceof SQLServerConnector) {
$this->connector->transactionEnd(); $this->connector->transactionEnd();
} else { } else {
$this->query('COMMIT TRANSACTION'); $this->query('COMMIT TRANSACTION');
} }
} }
}
public function comparisonClause($field, $value, $exact = false, $negate = false, $caseSensitive = null, $parameterised = false) public function comparisonClause($field, $value, $exact = false, $negate = false, $caseSensitive = null, $parameterised = false)
{ {