Class yii\mongodb\Transaction

Inheritanceyii\mongodb\Transaction » yii\base\BaseObject
Source Code https://github.com/yiisoft/yii2-mongodb/blob/master/src/Transaction.php

In MongoDB, an operation on a single document is atomic. Because you can use embedded documents and arrays to capture relationships between data in a single document structure instead of normalizing across multiple documents and collections, this single-document atomicity obviates the need for multi-document transactions for many practical use cases.

For situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions. With distributed transactions, transactions can be used across multiple operations, collections, databases, documents, and shards.

See also:

Public Properties

Hide inherited properties

Property Type Description Defined By
$clientSession \yii\mongodb\MongoDB\Driver\Session Class represents a client session and Commands, queries, and write operations may then be associated the session. yii\mongodb\Transaction
$isActive boolean Whether this transaction is active. yii\mongodb\Transaction

Public Methods

Hide inherited methods

Method Description Defined By
commit() Commit a transaction. yii\mongodb\Transaction
getIsActive() Returns a value indicating whether this transaction is active. yii\mongodb\Transaction
getState() Returns the transaction state. yii\mongodb\Transaction
rollBack() Rolls back a transaction. yii\mongodb\Transaction
start() Start a transaction if session is not in transaction process. yii\mongodb\Transaction

Protected Methods

Hide inherited methods

Method Description Defined By
yiiBeginProfile() Begin profile if enableProfiling property is enable in yii\mongodb\Connection yii\mongodb\Transaction
yiiDebug() Set debug message if enableLogging property is enable in yii\mongodb\Connection yii\mongodb\Transaction
yiiEndProfile() End profile if enableProfiling property is enable in yii\mongodb\Connection yii\mongodb\Transaction

Constants

Hide inherited constants

Constant Value Description Defined By
STATE_ABORTED 'aborted' yii\mongodb\Transaction
STATE_COMMITTED 'committed' yii\mongodb\Transaction
STATE_NONE 'none' yii\mongodb\Transaction
STATE_STARTING 'starting' yii\mongodb\Transaction

Property Details

Hide inherited properties

$clientSession public property

Class represents a client session and Commands, queries, and write operations may then be associated the session.

See also https://www.php.net/manual/en/class.mongodb-driver-session.php.

public \yii\mongodb\MongoDB\Driver\Session $clientSession null
$isActive public property

Whether this transaction is active. Only an active transaction can commit() or rollBack().

public boolean $isActive null

Method Details

Hide inherited methods

commit() public method
public void commit ( )

                public function commit()
{
    $this->yiiDebug('Committing mongodb transaction ...', __METHOD__);
    $this->clientSession->mongoSession->commitTransaction();
    $this->yiiEndProfile('mongodb > start transaction(session id => ' . $this->clientSession->getId() . ')');
    $this->yiiDebug('Commit mongodb transaction.', __METHOD__);
    $this->clientSession->db->trigger(Connection::EVENT_COMMIT_TRANSACTION);
}

            
getIsActive() public method

Returns a value indicating whether this transaction is active.

public boolean getIsActive ( )
return boolean

Whether this transaction is active. Only an active transaction can commit() or rollBack().

                public function getIsActive()
{
    return $this->clientSession->db->getIsActive() && $this->clientSession->getInTransaction();
}

            
getState() public method

Returns the transaction state.

public void getState ( )

                public function getState()
{
    return $this->clientSession->mongoSession->getTransactionState();
}

            
rollBack() public method
public void rollBack ( )

                public function rollBack()
{
    $this->yiiDebug('Rolling back mongodb transaction ...', __METHOD__);
    $this->clientSession->mongoSession->abortTransaction();
    $this->yiiEndProfile('mongodb > start transaction(session id => ' . $this->clientSession->getId() . ')');
    $this->yiiDebug('Roll back mongodb transaction.', __METHOD__);
    $this->clientSession->db->trigger(Connection::EVENT_ROLLBACK_TRANSACTION);
}

            
start() public method
public void start ( $transactionOptions = [] )
$transactionOptions array

Options can be passed as argument to this method. Each element in this options array overrides the corresponding option from the "sessionOptions" option, if set when starting the session with ClientSession::start().

                public function start($transactionOptions = [])
{
    Command::prepareManagerOptions($transactionOptions);
    $this->yiiDebug('Starting mongodb transaction ...', __METHOD__);
    if ($this->clientSession->getInTransaction()) {
        throw new Exception('Nested transaction not supported');
    }
    $this->clientSession->db->trigger(Connection::EVENT_START_TRANSACTION);
    $this->yiiBeginProfile('mongodb > start transaction(session id => ' . $this->clientSession->getId() . ')');
    $this->clientSession->mongoSession->startTransaction($transactionOptions);
    $this->yiiDebug('MongoDB transaction started.', __METHOD__);
}

            
yiiBeginProfile() protected method

Begin profile if enableProfiling property is enable in yii\mongodb\Connection

protected void yiiBeginProfile ( $token, $category 'mongodb' )
$token
$category

                protected function yiiBeginProfile($token, $category = 'mongodb')
{
    if ($this->clientSession->db->enableProfiling) {
        Yii::beginProfile($token,$category);
    }
}

            
yiiDebug() protected method

Set debug message if enableLogging property is enable in yii\mongodb\Connection

protected void yiiDebug ( $message, $category 'mongodb' )
$message
$category

                protected function yiiDebug($message, $category = 'mongodb')
{
    if ($this->clientSession->db->enableLogging) {
        Yii::debug($message,$category);
    }
}

            
yiiEndProfile() protected method

End profile if enableProfiling property is enable in yii\mongodb\Connection

protected void yiiEndProfile ( $token, $category 'mongodb' )
$token
$category

                protected function yiiEndProfile($token, $category = 'mongodb')
{
    if ($this->clientSession->db->enableProfiling) {
        Yii::endProfile($token,$category);
    }
}