You are here

class Transaction in Drupal driver for SQL Server and SQL Azure 8

Same name and namespace in other branches
  1. 8.2 drivers/lib/Drupal/Driver/Database/sqlsrv/Transaction.php \Drupal\Driver\Database\sqlsrv\Transaction
  2. 3.0.x drivers/lib/Drupal/Driver/Database/sqlsrv/Transaction.php \Drupal\Driver\Database\sqlsrv\Transaction

Hierarchy

Expanded class hierarchy of Transaction

File

drivers/lib/Drupal/Driver/Database/sqlsrv/Transaction.php, line 23
Definition of Drupal\Driver\Database\sqlsrv\Transaction

Namespace

Drupal\Driver\Database\sqlsrv
View source
class Transaction extends DatabaseTransaction {

  /**
   * A boolean value to indicate whether this transaction has been commited.
   *
   * @var Boolean
   */
  protected $commited = FALSE;

  /**
   * A boolean to indicate if the transaction scope should behave sanely.
   *
   * @var DatabaseTransactionSettings
   */
  protected $settings = FALSE;

  /**
   * Overriden to add settings.
   *
   * @param Connection $connection
   *
   * @param string $name
   *
   * @param DatabaseTransactionSettings $settings
   */
  public function __construct(Connection $connection, $name = NULL, DatabaseTransactionSettings $settings = NULL) {

    // Store connection and settings.
    $this->settings = $settings;
    $this->connection = $connection;

    // If there is no transaction depth, then no transaction has started. Name
    // the transaction 'drupal_transaction'.
    if (!($depth = $connection
      ->transactionDepth())) {
      $this->name = 'drupal_transaction';
    }
    elseif (empty($name)) {
      $this->name = 'savepoint_' . $depth;
    }
    else {
      $this->name = $name;
    }

    // Do not push the transaction if this features is not enable.
    if ($this->connection->driver_settings
      ->GetEnableTransactions() === FALSE) {
      return;
    }
    $this->connection
      ->pushTransaction($this->name, $settings);
  }

  /**
   * Overriden __destruct to provide some mental health (sane support).
   */
  public function __destruct() {
    if (!$this->settings
      ->Get_Sane()) {

      // If we rolled back then the transaction would have already been popped.
      if (!$this->rolledBack) {
        $this->connection
          ->popTransaction($this->name);
      }
    }
    else {

      // If we did not commit and did not rollback explicitly, rollback.
      // Rollbacks are not usually called explicitly by the user
      // but that could happen.
      if (!$this->commited && !$this->rolledBack) {
        $this
          ->rollback();
      }
    }
  }

  /**
   * Commits the transaction. Only available for SANE transactions.
   *
   * @throws DatabaseTransactionExplicitCommitNotAllowedException
   */
  public function commit() {

    // Insane transaction behaviour does not allow explicit commits.
    if (!$this->settings
      ->Get_Sane()) {
      throw new DatabaseTransactionExplicitCommitNotAllowedException();
    }

    // Cannot commit a rolledback transaction...
    if ($this->rolledBack) {
      throw new DatabaseTransactionCannotCommitAfterRollbackException();
    }

    // Mark as commited, and commit!
    $this->commited = TRUE;

    // Do not pop the transaction if this features is not enabled.
    if ($this->connection->driver_settings
      ->GetEnableTransactions() === FALSE) {
      return;
    }

    // Finally pop it!
    $this->connection
      ->popTransaction($this->name);
  }

  /**
   * {@inheritdoc}
   */
  public function rollback() {
    $this->rolledBack = TRUE;

    // Do not pop the transaction if this features is not enable.
    if ($this->connection->driver_settings
      ->GetEnableTransactions() === FALSE) {
      return;
    }
    $this->connection
      ->rollback($this->name);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Transaction::$commited protected property A boolean value to indicate whether this transaction has been commited.
Transaction::$connection protected property The connection object for this transaction.
Transaction::$name protected property The name of the transaction.
Transaction::$rolledBack protected property A boolean value to indicate whether this transaction has been rolled back.
Transaction::$settings protected property A boolean to indicate if the transaction scope should behave sanely.
Transaction::commit public function Commits the transaction. Only available for SANE transactions.
Transaction::name public function Retrieves the name of the transaction or savepoint.
Transaction::rollback public function
Transaction::rollBack public function Rolls back the current transaction.
Transaction::__construct public function Overriden to add settings. Overrides Transaction::__construct
Transaction::__destruct public function Overriden __destruct to provide some mental health (sane support). Overrides Transaction::__destruct