1

I need to use transactions on MongoDB in Laravel.

I downloaded the php MongoDB driver 1.6 and copied and pasted php_mongodb.dll into the php/ext folder.

I also installed the php mongo library through

composer require mongodb/mongodb

Now, when I try to use transactions according to this, it doesn't rollback when an error occurs.

   $client = new Client($url);
    $callback = function (\MongoDB\Driver\Session $session) use ($client) {
        $data = [
            "name" => "Tommy",
                     ];
        $collection = $client->db1->users;
        $user = $collection->updateOne(
            ['mobile' => '*'],
            ['$set' => $data],
            [$session]
        );
        $data = [
            "activate" => 1,
        ];
        $collection = $client->db1->wallets;
        $wallet1 = $collection->updateOne(
            ['_id' => 100],
            ['set' => $data],
            [$session]
        );

    };

    $session = $client->startSession();
    $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),
    ];

    try{
        \MongoDB\with_transaction($session, $callback, $transactionOptions);
        return true;

    }catch (\Exception $e)
    {
        return false;
    }

The session class in MongoDB\Driver\Session is unknown in laravel while this class is for mongodb>=1.4.0. I don't know what is wrong. Can anyone help me?

Nice91
  • 91
  • 9
  • 2
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Salim Djerbouh Nov 05 '19 at 10:08
  • 1
    @Saly3301 100% agree that this message is a lot clearer in intent than the currently changed "close reason" for what is essentially the same selection. This question just basically says *"My code does not work.."* - Yet there is no code present in the question for anyone to be able to see why it does not work. The changed message from what used to be the MCVE response does not clearly convey that point. The question basically needs to present the code believed to be responsible and that does in fact **reproduce the problem**. Without that, it's very difficult to help here. – Neil Lunn Nov 05 '19 at 10:11
  • 1
    A lot of these messages has changed and old versions are better, am glad I kept a copy [here](https://gist.github.com/CaddyDz/7869ae0b6ab52d32f9594458cab36762) – Salim Djerbouh Nov 05 '19 at 10:14
  • Note to the author who is still editing the question ( but not with the detail they are being asked to do ). **External Links** are not an acceptable point of posting code to reproduce a problem. We need a **minimal** listing here. And you are asked to produce a **minimal** listing also in the hope that this *small* sample of code ( as a aside from your whole application ) will actually highlight the problem to yourself without the need for other help. If you did not spot it though, then others will. With a **small** sample. – Neil Lunn Nov 05 '19 at 10:15
  • @Neil Lunn I just edited the question, please reconsider it. – Nice91 Nov 05 '19 at 10:25
  • Note that there is a duplicate for this: [Example of a transaction in MongoDB 4.0 using PHP](https://stackoverflow.com/questions/53545582/example-of-a-transaction-in-mongodb-4-0-using-php). It's not the best or clearest example in answer, but the documentation link is there and the documentation itself is very clear. I think your problem in translation is more related to what I described above. – Neil Lunn Nov 05 '19 at 10:34

1 Answers1

0

Transaction is little different in NoSQL from Sql DBs.

In mongodb if you want hass transaction u must open transaction in session of database just like bellow:

public function create()
{
    $client = DB::connection('mongodb')->getMongoClient();
    $transaction = $client->startSession();
    $transaction->startTransaction([]);
    try {
        //Some Code
        $transaction->commitTransaction();
    enter code here
    } catch (Exception $e) {
        $transaction->abortTransaction();
    }
}

Mehran
  • 41
  • 4