使用新的 PHP 驱动程序在 MongoDB 4.2 中进行事务 [英] Transaction in MongoDB 4.2 with new PHP Driver

查看:71
本文介绍了使用新的 PHP 驱动程序在 MongoDB 4.2 中进行事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 MongoDB 的新手,因为我之前是 MySQL 的超级粉丝.我最近转向了这个 NoSQL 并喜欢它,但现在我被 MongoDB 中的 Transactions 严重困住了.

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.

我在 SO 上发现了一些相关问题,但没有答案或已过时,这些问题不适用于新的 MongoDB PHP Driver,因为语法/功能有很多变化,我可以看到很多像我这样的新手在 MongoDB Docs 和 PHP Driver 之间混淆.

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.

我在 MongoDB Docs 中发现了这种提交事务的方式

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);

但此语法/函数对于新的 PHP 驱动程序已过时,并且会出现以下错误

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

Call to undefined function MongoDB\with_transaction()

根据 PHP 文档,MongoDB 的新 PHP 驱动程序提供了这些选项来提交事务,但我不明白如何?因为文档中没有给出示例.

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

我的问题是,如何使用新的 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.

推荐答案

好的,我找到了我的问题的答案,我认为它可以对其他人有所帮助

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

使用核心 Mongo 扩展

$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();

使用PHP 库

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

注意:为了使答案简短明了,我省略了一些可选参数,并使用了没有任何 try-catch 的虚拟插入数据等.

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.

如果您将 MongoDB 实例作为用于开发或测试目的的独立版本运行,那么您可能会遇到类似

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

然后您可以按照本指南在独立实例上启用副本 https://docs.mongodb.com/manual/tutorial/convert-standalone-to-replica-set/

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

这篇关于使用新的 PHP 驱动程序在 MongoDB 4.2 中进行事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆