You are here

class Transaction in Transaction 6

Same name and namespace in other branches
  1. 5 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 13
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 static function rollbackIfFalse($var) {
    if ($var === FALSE) {
      self::rollback();
    }
  }

  /**
   * Force this transaction to roll back when finished.
   */
  public static 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 static 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 static function Force this transaction to roll back when finished.
Transaction::rollbackIfFalse public static function If the given value is FALSE (value AND type), roll back the current transaction.
Transaction::willRollback public static 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.