yiisoft / yii2-mongodb

Yii 2 MongoDB extension
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
325 stars 191 forks source link

Why we not implement db transaction to mongodb? #29

Closed zinzinday closed 9 years ago

cebe commented 9 years ago

because mongodb does not support transactions.

TheHett commented 9 years ago

@cebe transaction is possible on the client side, when we started to write Yii2, it had declared similar feature

cebe commented 9 years ago

can you point to some docs about that? I could not find any...

TheHett commented 9 years ago

here https://github.com/yiisoft/yii2/issues/11

cebe commented 9 years ago

I do not see anything about transactions in the linked docs...

TheHett commented 9 years ago

hmm, maybe I misunderstood

image

cebe commented 9 years ago

The list is copy from SQL layer, the question mark indicates that there was no information on whether such feature is provided by mongo or not. And as far as I know there is no such feature in mongodb. If you find it, point us to the docs and we can try implement it.

TheHett commented 9 years ago

Hm, maybe only Two Phase Commits http://docs.mongodb.org/manual/tutorial/perform-two-phase-commits/ but it not easy, i think

sebathi commented 6 years ago

https://techcrunch.com/2018/02/15/mongodb-gets-support-for-multi-document-acid-transactions/

sebathi commented 6 years ago

It is totally suported now.

We should be able to use them ;)

https://www.mongodb.com/transactions

waterproofy commented 5 years ago

It's very important update.

devkdouglass commented 4 years ago

Adding some examples in case anyone else runs into issues. I got hung up while trying to add the session to the $options parameter in the yii\mongodb\Collection methods.

With Yi2 Mongodb Extension

$client = Yii::$app->mongodb;
$session = $client->manager->startSession();
$session->startTransaction();

try {
    $client->createCommand()
           ->addInsert(['key' => 'value'])
           ->setWriteConcern(['session' => $session])
           ->executeBatch('COLLECTION');

    // Uncomment to prevent transaction
    // throw new \Exception('Force fail. Stop transaction.');

    $session->commitTransaction();

} catch (\Exception $error) {
    $session->abortTransaction();
    print_r($error->getMessage());
} 

Without Yii2 Mongodb Exension

$client = Yii::$app->mongodb->manager;
$session = $client->startSession();
$session->startTransaction();

try {
    $bulk = new \MongoDB\Driver\BulkWrite;
    $bulk->insert(['key' => 'value']);
    $client->executeBulkWrite('DB.COLLECTION', $bulk, ['session' => $session]);

    // Uncomment to prevent transaction
    /// throw new \Exception('Force fail. Stop transaction.');

    $session->commitTransaction();

} catch (\Exception $error) {
    $session->abortTransaction();
     print_r($error->getMessage());
}
maharjanmilan commented 4 years ago

$client = Yii::$app->mongodb; $session = $client->manager->startSession();

Manger is null here. Whats the issue?

devkdouglass commented 4 years ago

If I remember correctly and depending on your environment, you may need to install ext-mongodb to add the core API functionality, along with the MongoDB Driver. Make sure the mongodb.so extension exists in your PHP.ini file.

Also, I believe transactions are only available with Replicas(at least with Mongo Atlas) and MongoDB 4.2.