/*!
\note Transaction unsafe. If you call several transaction unsafe methods you must enclose
the calls within a db transaction; thus within db->begin and db->commit.
505796
static function clear( $node_id )
    {
        $counter = eZViewCounter::fetch( $node_id );
        if ( $counter != null )
        {
            $counter->setAttribute( 'count', 0 );
            $counter->store();
        }
    }
247073
private function commit()
    {
        if ($this->inTransaction) {
            try {
                // commit read-write transaction which also releases the lock
                if ('sqlite' === $this->driver) {
                    $this->pdo->exec('COMMIT');
                } else {
                    $this->pdo->commit();
                }
                $this->inTransaction = false;
            } catch (\PDOException $e) {
                $this->rollback();

                throw $e;
            }
        }
    }
371097
public function begin() {
        if($this->isInTransaction()) {
            throw new \Exception('You cannot start a new transaction once you are already in one (Originally started at: ' . self::$stackTrace . ')');
        } else {
            $e = new \Exception();
            self::$stackTrace = $e->getTraceAsString();
        }
        $transactionManager = new TransactionManager();
        $transactionManager->setDbService($this->getDbService());
        $this->setTransaction($transactionManager->get());
    }
1158535
protected function begin_transaction() {
        if (!$this->transactions_supported()) {
            return;
        }

        $sql = "SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED";
        $this->query_start($sql, NULL, SQL_QUERY_AUX);
        $result = $this->mysqli->query($sql);
        $this->query_end($result);

        $sql = "START TRANSACTION";
        $this->query_start($sql, NULL, SQL_QUERY_AUX);
        $result = $this->mysqli->query($sql);
        $this->query_end($result);
    }
1064463
public function commit() {
		if (!$this->inTransaction) {
			return false;
		}

		$this->inTransaction = false;
		return $this->pdo->commit();
	}
541713
private function dbBeginTransaction()
    {
        if ($this->pendingConnection) {
            $this->connect();
        }

        $this->pendingTransactionBegin[$this->transLevel] = false;

        if ($this->dbTransLevel == 0) {
            //Top level transaction
            $res = pg_query($this->pgConn, 'begin');
        } else {
            $res = pg_query(
                $this->pgConn,
                'savepoint level' . $this->dbTransLevel . 'begin'
            );
        }

        if ($res === false) {
            $this->cleanAfterCommit();
            throw $this->createException();
        }

        $this->dbTransLevel++;

        return $res;
    }
1076224
protected function begin_transaction() {
        $this->savepointpresent = true;
        $sql = "BEGIN ISOLATION LEVEL READ COMMITTED; SAVEPOINT moodle_pg_savepoint";
        $this->query_start($sql, null, SQL_QUERY_AUX);
        $result = pg_query($this->pgsql, $sql);
        $this->query_end($result);

        pg_free_result($result);
    }
1242788
function close()
    {
        foreach ($this->cache as $key => $data) {
            // The key has been used
            if ($this->cache_changes[$key]) {
                // Make sure we're not going to write unchanged data
                // by comparing current md5 sum with the sum calculated on DB read
                $data = $this->serialize($data);

                if (!$this->cache_sums[$key] || $this->cache_sums[$key] != md5($data)) {
                    $this->write_record($key, $data);
                }
            }
        }

        if ($this->index_changed) {
            $this->write_index();
        }

        // reset internal cache index, thanks to this we can force index reload
        $this->index         = null;
        $this->index_changed = false;
        $this->cache         = array();
        $this->cache_sums    = array();
        $this->cache_changes = array();
    }
299556
public static function cleanup_instances() {
        global $DB;
        self::init_levels();

        $sqls = array();
        foreach (self::$alllevels as $level=>$classname) {
            $sqls[] = $classname::get_cleanup_sql();
        }

        $sql = implode(" UNION ", $sqls);

        // it is probably better to use transactions, it might be faster too
        $transaction = $DB->start_delegated_transaction();

        $rs = $DB->get_recordset_sql($sql);
        foreach ($rs as $record) {
            $context = context::create_instance_from_record($record);
            $context->delete();
        }
        $rs->close();

        $transaction->allow_commit();
    }
969477
public function beginTransaction()
    {
        ++$this->transactions;

        if ($this->transactions == 1) {
            $this->pdo->beginTransaction();
        }

        $this->fireConnectionEvent('beganTransaction');
    }
903466
public function dbInsert() {
		BLog::addToLog('[Items.ItemTree]: Inserting data...');
		if (!$db = \Brilliant\BFactory::getDBO()) {
			return false;
			}
		$closeTransaction=false;

		if(!$this->isRoot){
			$db->startTransaction();
			$closeTransaction = true;
			//---------------------------------------------------
			// Try to get parent / root element, create root
			// if we does not have root element.
			//---------------------------------------------------
			BLog::addToLog('[Items.ItemTree.'.$this->tableName.']: Try to get parent / root element.');
			$parent = $this->getParentOrRoot();
			if (empty($parent)) {
				$className = get_class($this);
				if(DEBUG_LOG_BITEMS){
					BLog::addToLog('[Items.ItemTree.'.$this->tableName.']: Parent is empty. Need to insert parent element ('.$className.')');
					}
				$parent = new $className();
				$parent->{$this->groupKeyName} = $this->{$this->groupKeyName};
				$parent->{$this->leftKeyName} = 1;
				$parent->{$this->rightKeyName} = 4;
				$parent->{$this->levelKeyName} = 1;
				$parent->isRoot = true;
				$r = $parent->saveToDB();
				if(empty($r)){
					return false;
					}
				//
				$this->{$this->parentKeyName} = $parent->{$this->primaryKeyName};
				$this->{$this->groupKeyName} = $parent->{$this->groupKeyName};
				$this->{$this->leftKeyName} = 2;
				$this->{$this->rightKeyName} = 3;
				$this->{$this->levelKeyName} = 2;
				} else {
				if(DEBUG_LOG_BITEMS){
					BLog::addToLog('[Items.ItemTree.'.$this->tableName.']: Parent is not empty.');
					}
				$this->{$this->parentKeyName} = $parent->{$this->primaryKeyName};
				$this->{$this->groupKeyName} = $parent->{$this->groupKeyName};
				$this->{$this->leftKeyName} = (int)$parent->{$this->rightKeyName};
				$this->{$this->rightKeyName} = (int)$parent->{$this->rightKeyName} + 1;
				$this->{$this->levelKeyName} = (int)$parent->{$this->levelKeyName} + 1;
				$qr = 'UPDATE `' . $this->tableName . '` SET ';
				$qr .= '`' . $this->rightKeyName . '`=`' . $this->rightKeyName . '`+2, ';
				$qr .= '`' . $this->leftKeyName . '` = IF(`' . $this->leftKeyName . '` > ' . $parent->{$this->rightKeyName} . ', `' . $this->leftKeyName . '`+2, `' . $this->leftKeyName . '`)';
				$qr .= ' WHERE ((`' . $this->groupKeyName . '` = ' . $this->{$this->groupKeyName} . ') AND (`' . $this->rightKeyName . '`>=' . $parent->{$this->rightKeyName} . '))';
				$q = $db->query($qr);
				if (empty($q)) {
					$db->rollback();
					return false;
					}
				//Because some elements in our internal cache was changed - need to flush it.
				$collection=$this->getCollection();
				$collection->flushInternalCache();
				}
			}
		//---------------------------------------------------
		//Forming query...
		//---------------------------------------------------
		$this->modified = new \DateTime();
		if(empty($this->created)){
			$this->created=new \DateTime();
			}
		$qr = $this->dbInsertQuery();
		//Running query...
		$q = $db->query($qr);
		if (empty($q)) {
			if ($closeTransaction) {
				$db->rollback();
				}
			return false;
			}
		$this->{$this->primaryKeyName} = $db->insertId();
		if ($closeTransaction) {
			$db->commit();
			}
		//Updating cache...
		$this->updateCache();
		//Return result
		return true;
	}
1168446
private static function commit()
    {
        $transaction = env('SIMPLES_TRANSACTION_CLASS', '\\Simples\\Persistence\\Transaction');
        if (!class_exists($transaction)) {
            return;
        }

        if (!method_exists($transaction, 'commit')) {
            throw new ErrorException("The transaction commit method was not found");
        }

        /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection, PhpUndefinedMethodInspection */
        if (!$transaction::commit()) {
            throw new ErrorException("Transaction can't commit the changes");
        }
    }
1292672
public function beginTransaction()
	{
		if( !$this->dbc )
			throw new RuntimeException( 'Database not conntected' );

		$this->openTransactions ++;
		if( $this->openTransactions == 1 )
		{
			if( self::$autoCommit )
				$this->execute( 'SET AUTOCOMMIT=0' );
			$this->execute( 'START TRANSACTION' );
		}
	}
824999
static function transaction(Closure $closure, $name = NULL, $mode = NULL)
	{
		try {
			$transactionStarted = static::begin($name, $mode);
			$result = call_user_func($closure);
			if ($transactionStarted) static::commit($name);

			return $result;
		} catch (Exception $e) {
			if (isset($transactionStarted) && $transactionStarted) static::rollback($name);
			throw $e;
		}
	}
127863
public function commit()
    {
        switch ($this->mode) {

            case "mysql":
                $result = $this->server->commit();
                break;

            case "postgres":
            case "redshift":
                $result = $this->query("COMMIT");
                break;

            case "odbc":
                $result = odbc_commit($this->server);
                break;

            default:
                throw new \Exception("commit() not supported in this mode (" . $this->mode . ")");
        }

        if (!$result) {
            $this->error();
        }

        return true;
    }
1095711
public function commitTransaction()
    {
        if (!self::$transaction) {
            throw new TransactionException("There is no Active Transaction");
        }

        self::$transaction = false;
        foreach (self::$connectionList as $dbDriver) {
            $dbDriver->commitTransaction();
        }
    }
78099
protected function commit_transaction() {
        if (!$this->transactions_supported()) {
            return;
        }

        $sql = "COMMIT";
        $this->query_start($sql, NULL, SQL_QUERY_AUX);
        $result = $this->mysqli->query($sql);
        $this->query_end($result);
    }