Transaction in MongoDB 4.2 with new PHP Driver

帅比萌擦擦* 提交于 2020-05-14 09:02:48

问题


I am new to MongoDB as I was a SuperFan of MySQL before. I recently moved to this NoSQL thing and loved it but now I am badly trapped at Transactions in MongoDB.

I found some related questions on SO but with no answers or obsolete which does not work with new MongoDB PHP Driver as there are many changes in syntax/functions and I could see many newbie like me are confused between MongoDB Docs and PHP Driver.

I found this way of committing transactions in MongoDB Docs

$client = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
$callback = function (\MongoDB\Driver\Session $session) use ($client) 
{
    $client->selectCollection('mydb1', 'foo')->insertOne(['abc' => 1], ['session' => $session]);
    $client->selectCollection('mydb2', 'bar')->insertOne(['xyz' => 999], ['session' => $session]);
};

// Step 2: Start a client session.
$session = $client->startSession();

// Step 3: Use with_transaction to start a transaction, execute the callback, and commit

$transactionOptions = 
[
   'readConcern' => new \MongoDB\Driver\ReadConcern(\MongoDB\Driver\ReadConcern::LOCAL),
   'writeConcern' => new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000),
   'readPreference' => new \MongoDB\Driver\ReadPreference(\MongoDB\Driver\ReadPreference::RP_PRIMARY),
];

\MongoDB\with_transaction($session, $callback, $transactionOptions);

but this syntax/functions are obsolete for new PHP Driver and it gives following error

Call to undefined function MongoDB\with_transaction()

According to PHP Docs, the new PHP Driver for MongoDB provides these options to commit transaction but I don't understand how? because there is no example given in docs.

https://www.php.net/manual/en/mongodb-driver-manager.startsession.php

https://www.php.net/manual/en/mongodb-driver-session.starttransaction.php

https://www.php.net/manual/en/mongodb-driver-session.committransaction.php

My Question is, How can I update the above code with New PHP Driver's functions? I believe to use

MongoDB\Driver\Manager::startSession
MongoDB\Driver\Session::startTransaction
MongoDB\Driver\Session::commitTransaction 

but I don't understand what their syntax is or their arguments etc because of incomplete documentation and no examples. Thanking you in anticipation for your time and support.


回答1:


Ok, So, I found the answer to my question and I thought it can be helpful for some others

using Core Mongo Extension

$connection =  new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");

$session = $connection->startSession();
$session->startTransaction();

$bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);
$bulk->insert(['x' => 1]);
$bulk->insert(['x' => 2]);
$bulk->insert(['x' => 3]);
$result = $connection->executeBulkWrite('db.users', $bulk, ['session' => $session]);

$session->commitTransaction();

using PHP Library

$session = $client->startSession();
$session->startTransaction();
try {
    // Perform actions.
    //insertOne(['abc' => 1], ['session' => $session]); <- Note Session
    $session->commitTransaction();
} catch(Exception $e) {
   $session->abortTransaction();
}

Note: To make the answer short and to the point, I have omitted some of the optional parameters and used a dummy insert data etc without any try-catch.


If you are running MongoDB instance as standalone version that is for development or testing purpose then you might get error something like

transaction numbers are only allowed on a replica set member or mongos

Then you can enable Replica on a standalone instance following this guide https://docs.mongodb.com/manual/tutorial/convert-standalone-to-replica-set/



来源:https://stackoverflow.com/questions/60809844/transaction-in-mongodb-4-2-with-new-php-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!