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,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');
}
} }
} }