Verified Commit 8d78571b authored by Andrei Mateescu's avatar Andrei Mateescu
Browse files

task: #3406985 Convert all transactions in core to use explicit ::commitOrRelease()

By: mondrake
By: mradcliffe
By: ghost of drupal past
By: godotislate
By: amateescu
parent 44856397
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ public function getStorage() {
    // Wrapping the queries in a transaction for performance gain.
    $transaction = $this->connection->startTransaction();
    self::replaceStorageContents($this->active, $this->storage);
    unset($transaction);
    $transaction->commitOrRelease();

    $this->eventDispatcher->dispatch(new StorageTransformEvent($this->storage), ConfigEvents::STORAGE_TRANSFORM_EXPORT);

+1 −1
Original line number Diff line number Diff line
@@ -117,7 +117,7 @@ public function transform(StorageInterface $storage) {
    // Wrapping the queries in a transaction for performance gain.
    $transaction = $this->connection->startTransaction();
    self::replaceStorageContents($storage, $mutable);
    unset($transaction);
    $transaction->commitOrRelease();

    // Dispatch the event so that event listeners can alter the configuration.
    $this->eventDispatcher->dispatch(new StorageTransformEvent($mutable), ConfigEvents::STORAGE_TRANSFORM_IMPORT);
+1 −0
Original line number Diff line number Diff line
@@ -84,6 +84,7 @@ public function execute() {
        $stmt->execute($insert_values, $this->queryOptions);
        $last_insert_id = $this->connection->lastInsertId();
      }
      $transaction->commitOrRelease();
    }
    catch (\Exception $e) {
      if (isset($transaction)) {
+7 −1
Original line number Diff line number Diff line
@@ -345,6 +345,12 @@ public function unpile(string $name, string $id): void {
      return;
    }

    // If the client transaction was already committed, there's no longer
    // anything to do on an explicit commit/savepoint release.
    if ($this->getConnectionTransactionState() === ClientConnectionTransactionState::Committed) {
      return;
    }

    // If there is no $id to commit, or if $id does not correspond to the one
    // in the stack for that $name, the commit is out of order.
    if (!isset($this->stack()[$id]) || $this->stack()[$id]->name !== $name) {
@@ -408,7 +414,7 @@ public function rollback(string $name, string $id): void {
    // If the transaction was voided, we cannot rollback. Fail silently but
    // trigger a user warning.
    if ($this->getConnectionTransactionState() === ClientConnectionTransactionState::Voided) {
      $this->connectionTransactionState = ClientConnectionTransactionState::RollbackFailed;
      $this->setConnectionTransactionState(ClientConnectionTransactionState::RollbackFailed);
      trigger_error('Transaction::rollBack() failed because of a prior execution of a DDL statement.', E_USER_WARNING);
      return;
    }
+23 −3
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Database\SchemaException;
use Drupal\Core\Database\Statement\FetchAs;
use Drupal\Core\Database\TransactionOutOfOrderException;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\ContentEntityStorageBase;
use Drupal\Core\Entity\ContentEntityTypeInterface;
@@ -764,14 +765,20 @@ public function delete(array $entities) {
    try {
      $transaction = $this->database->startTransaction();
      parent::delete($entities);
      $transaction->commitOrRelease();

      // Ignore replica server temporarily.
      \Drupal::service('database.replica_kill_switch')->trigger();
    }
    catch (\Exception $e) {
      if (isset($transaction)) {
        try {
          $transaction->rollBack();
        }
        catch (TransactionOutOfOrderException $rollbackException) {
          Error::logException(\Drupal::logger($this->entityTypeId), $rollbackException);
        }
      }
      Error::logException(\Drupal::logger($this->entityTypeId), $e);
      throw new EntityStorageException($e->getMessage(), $e->getCode(), $e);
    }
@@ -815,6 +822,7 @@ public function save(EntityInterface $entity) {
    try {
      $transaction = $this->database->startTransaction();
      $return = parent::save($entity);
      $transaction->commitOrRelease();

      // Ignore replica server temporarily.
      \Drupal::service('database.replica_kill_switch')->trigger();
@@ -822,8 +830,13 @@ public function save(EntityInterface $entity) {
    }
    catch (\Exception $e) {
      if (isset($transaction)) {
        try {
          $transaction->rollBack();
        }
        catch (TransactionOutOfOrderException $rollbackException) {
          Error::logException(\Drupal::logger($this->entityTypeId), $rollbackException);
        }
      }
      Error::logException(\Drupal::logger($this->entityTypeId), $e);
      throw new EntityStorageException($e->getMessage(), $e->getCode(), $e);
    }
@@ -866,13 +879,20 @@ public function restore(EntityInterface $entity) {
      // Insert the entity data in the dedicated tables.
      $this->saveToDedicatedTables($entity, FALSE, []);

      $transaction->commitOrRelease();

      // Ignore replica server temporarily.
      \Drupal::service('database.replica_kill_switch')->trigger();
    }
    catch (\Exception $e) {
      if (isset($transaction)) {
        try {
          $transaction->rollBack();
        }
        catch (TransactionOutOfOrderException $rollbackException) {
          Error::logException(\Drupal::logger($this->entityTypeId), $rollbackException);
        }
      }
      Error::logException(\Drupal::logger($this->entityTypeId), $e);
      throw new EntityStorageException($e->getMessage(), $e->getCode(), $e);
    }
Loading