I realise that MongoDB, by it\'s very nature, doesn\'t and probably never will support these kinds of transactions. However, I have found that I do need to use them in a so
MongoDB 4.0 adds support for multi-document ACID transactions.
Java Example:
try (ClientSession clientSession = client.startSession()) {
clientSession.startTransaction();
collection.insertOne(clientSession, docOne);
collection.insertOne(clientSession, docTwo);
clientSession.commitTransaction();
}
Note, it works for replica set. You can still have a replica set with one node and run it on local machine.
As a generic response multi-document commits on MongoDB can be performed as two phase commits, which have been somewhat extensively documented in the manual (See: http://docs.mongodb.org/manual/tutorial/perform-two-phase-commits/).
The pattern suggested by the manual is briefly to following:
transactions collection, that includes target document, source document, value and state (of the transaction)initial as the statestate to pendingcommitteddoneIn addition:
state value cancelingSome specific notes for your implementation:
lock_status, data_old, data_new into source/target documents. These should be properties of the transactions, not the documents themselves. DBrefs: http://www.mongodb.org/display/DOCS/Database+Referencesdone seems like a better idea since this allows you to later debug and find out what kind of transactions have been performed. I'm pretty sure you won't run out of disk space either (and for this there are solutions as well).MongoDB 4.0 is adding (multi-collection) multi-document transactions: link