2

I need to showcase a transaction using mongoDB in PHP. In my example, I have "accounts" and "transfers". The first operation I am deducting balance from the "sender account" and attempt to send the money to "beneficiary account", but the latter cannot accept any transfers therefore, the transction should rollback i.e. the money should be returned on senders account.

I could not find any PHP specific documentation regarding transactions online and thus I do not really know how to go about it.

Thank you for your help!

Andra Zeberga
  • 207
  • 4
  • 14

2 Answers2

8

If you're using the PHP library that wraps the driver, after creating an instance of Client e.g. called $client, you can do the following:

$session = $client->startSession();
$session->startTransaction();
try {
    // Perform actions.
    $session->commitTransaction();
} catch(Exception $e) {
    $session->abortTransaction();
}

Unfortunately I couldn't find any relevant documentation in the PHP library reference after a cursory search, but I found examples in the PHP library's issues that suggest that creating a session from the client and using that session to start then either commit or abort the transaction is the appropriate procedure.

A couple of things to be aware of, however:

If you view the MongoDB docs (as linked above) you'll note that the requirement for a replica set to be in use is not particularly prominently displayed, being in under the third heading, and coming after all of the example code (which, if you're anything like me, will be the first thing you look for).

B. Fleming
  • 7,170
  • 1
  • 18
  • 36
  • You can also use `Transactions` on a standalone server if you convert it to replica set https://stackoverflow.com/questions/60809844/transaction-in-mongodb-4-2-with-new-php-driver – Airy Apr 11 '20 at 11:37
  • In this example need to close session manually? – rodigy Jan 09 '22 at 19:08
  • @rodigy If you encounter a database-related exception, then it's good practice to explicitly abort the transaction, especially if your code will perform additional queries before the request has finished processing. Even if your specific script does not perform additional database queries following an error, by explicitly aborting the transaction you will avoid any sloppy mistakes in the future in cases where you do need to perform additional queries. Always build good habits! – B. Fleming Jan 10 '22 at 01:41
2

According to the mongodb docs, the accepted answer leaves out one crucial part - you need to pass the session as parameter to any operations that should be part of the transaction. Otherwise they just behave as normal operations (i.e. won't be rolled back if the transaction is aborted) - it's not like a traditional RDBMS's transaction where it's just start transaction, everything after is by default part of the transaction until COMMIT (or ROLLBACK).

Having said as much, transactions are still quite new in mongo, and at I've not been able to get the been able to get even the sample PHP code working under mongo 4.0.12 w/ PHP MongoDB driver 1.5.5, so it may just be a bit on the bleeding edge still (or maybe just don't use that combination of mongo and driver).

Update: It appears you need a replica set, not a standalone server, in order to use transactions with mongodb.

Update: Accepted answer edited directly to include this information

Blair
  • 176
  • 12
  • You've brought up some incredibly important points here. The requirement to be running a replica set is one that wasn't immediately obvious even after digging into the documentation as it seems like they treated it as a given and didn't really emphasize it. Please feel free to edit my answer directly to incorporate this additional information. I would be more than happy to see an edit make my answer more accurate and helpful! – B. Fleming Oct 10 '19 at 02:22