You are here

class DatabaseTransaction_sqlsrv in Drupal driver for SQL Server and SQL Azure 7.3

Same name and namespace in other branches
  1. 7 sqlsrv/database.inc \DatabaseTransaction_sqlsrv
  2. 7.2 sqlsrv/transaction.inc \DatabaseTransaction_sqlsrv

Overriden to bring some commonsense to transaction management.

The "sane" approach is to require explicit transaction commits!

Drupal uses the other way round, commits are implicit unless you explicitly rollback.

Hierarchy

Expanded class hierarchy of DatabaseTransaction_sqlsrv

File

sqlsrv/transaction.inc, line 11

View source
class DatabaseTransaction_sqlsrv 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 DatabaseConnection $connection
   * @param mixed $name
   * @param mixed $sane
   */
  public function __construct(DatabaseConnection $connection, $name = NULL, $settings = NULL) {
    $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;
    }
    $this->connection
      ->pushTransaction($this->name, $settings);
  }

  /**
   * Overriden __desctur to provide some mental health.
   */
  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();
      }
    }
  }

  /**
   * The "sane" behaviour requires explicit commits.
   *
   * @throws DatabaseTransactionExplicitCommitNotAllowedException
   */
  public function commit() {
    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;
    $this->connection
      ->popTransaction($this->name);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatabaseTransaction::$connection protected property The connection object for this transaction.
DatabaseTransaction::$name protected property The name of the transaction.
DatabaseTransaction::$rolledBack protected property A boolean value to indicate whether this transaction has been rolled back.
DatabaseTransaction::name public function Retrieves the name of the transaction or savepoint.
DatabaseTransaction::rollback public function Rolls back the current transaction.
DatabaseTransaction_sqlsrv::$commited protected property A boolean value to indicate whether this transaction has been commited.
DatabaseTransaction_sqlsrv::$settings protected property A boolean to indicate if the transaction scope should behave sanely.
DatabaseTransaction_sqlsrv::commit public function The "sane" behaviour requires explicit commits.
DatabaseTransaction_sqlsrv::__construct public function Overriden to add settings. Overrides DatabaseTransaction::__construct
DatabaseTransaction_sqlsrv::__destruct public function Overriden __desctur to provide some mental health. Overrides DatabaseTransaction::__destruct