You are here

class Transaction in Transaction 5

Same name and namespace in other branches
  1. 6 transaction.module \Transaction

Provides a nestable transaction system for handling commits and rollbacks.

Although transactions can be nested, a rollback or commit from a nested transaction will cause it to propagate upwards.

Hierarchy

Expanded class hierarchy of Transaction

File

./transaction.module, line 14
Provides a database transaction system for use with InnoDB tables in MySQL.

View source
class Transaction {

  /** The number of transactions that have been nested within this one. */
  private static $Layers = 0;

  /** Will the commit be allowed? */
  private static $AllowCommit = TRUE;

  /**
   * Create a new transaction.
   */
  public function __construct() {
    if (self::$Layers == 0) {
      db_query('BEGIN');
    }
    self::$Layers++;
  }

  /**
   * Complete the transaction by either performing a commit or a rollback
   * depending on the state of the object's allow_commit property.
   */
  public function __destruct() {
    self::$Layers--;
    if (self::$Layers == 0) {
      if (self::$AllowCommit) {
        db_query('COMMIT');
      }
      else {
        db_query('ROLLBACK');
      }

      // Reset the ROLLBACK propagator
      self::$AllowCommit = TRUE;
    }
  }

  /**
   * If the given value is FALSE (value AND type), roll back the current
   * transaction.
   *
   * @param boolean $var Roll back the current transaction iff TRUE.
   */
  public function rollbackIfFalse($var) {
    if ($var === FALSE) {
      $this
        ->rollback();
    }
  }

  /**
   * Force this transaction to roll back when finished.
   */
  public function rollback() {
    self::$AllowCommit = FALSE;
  }

  /**
   * Will this transaction roll back instead of committing?
   *
   * @return boolean TRUE if the transaction will roll back, FALSE if it
   *         will commit.
   */
  public function willRollback() {
    return !self::$AllowCommit;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Transaction::$AllowCommit private static property Will the commit be allowed?
Transaction::$Layers private static property The number of transactions that have been nested within this one.
Transaction::rollback public function Force this transaction to roll back when finished.
Transaction::rollbackIfFalse public function If the given value is FALSE (value AND type), roll back the current transaction.
Transaction::willRollback public function Will this transaction roll back instead of committing?
Transaction::__construct public function Create a new transaction.
Transaction::__destruct public function Complete the transaction by either performing a commit or a rollback depending on the state of the object's allow_commit property.