function TransactionTest::testReleaseIntermediateSavepoint

Tests releasing a savepoint before last is safe.

File

core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php, line 937

Class

TransactionTest
Tests the transactions, using the explicit ::commitOrRelease method.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testReleaseIntermediateSavepoint() : void {
  $transaction = $this->createRootTransaction();
  $savepoint1 = $this->createFirstSavepointTransaction('', FALSE);
  // Starts a savepoint transaction. Corresponds to 'SAVEPOINT savepoint_2'
  // on the database.
  $savepoint2 = $this->connection
    ->startTransaction();
  $this->assertSame(3, $this->connection
    ->transactionManager()
    ->stackDepth());
  // Starts a savepoint transaction. Corresponds to 'SAVEPOINT savepoint_3'
  // on the database.
  // phpcs:ignore DrupalPractice.CodeAnalysis.VariableAnalysis
  $savepoint3 = $this->connection
    ->startTransaction();
  $this->assertSame(4, $this->connection
    ->transactionManager()
    ->stackDepth());
  // Starts a savepoint transaction. Corresponds to 'SAVEPOINT savepoint_4'
  // on the database.
  // phpcs:ignore DrupalPractice.CodeAnalysis.VariableAnalysis
  $savepoint4 = $this->connection
    ->startTransaction();
  $this->assertSame(5, $this->connection
    ->transactionManager()
    ->stackDepth());
  $this->insertRow('row');
  // Release savepoint transaction. Corresponds to 'RELEASE SAVEPOINT
  // savepoint_2' on the database.
  $savepoint2->commitOrRelease();
  // Since we have committed an intermediate savepoint Transaction object,
  // the savepoints created later have been dropped by the database already.
  $this->assertSame(2, $this->connection
    ->transactionManager()
    ->stackDepth());
  $this->assertRowPresent('row');
  // Commit the remaining Transaction objects. The client transaction is
  // eventually committed.
  $savepoint1->commitOrRelease();
  $transaction->commitOrRelease();
  $this->assertFalse($this->connection
    ->inTransaction());
  $this->assertRowPresent('row');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.