Mongodb needs extra collection for two phase lock.
The transaction is just created and waiting to be started.
Once the transaction is created, the
data field would contain all information. e.g. source, destination, cash amount.
The transaction has started but some collection(s) have not finished its operation.
When process pending transaction, there are always a series of operations on all related collections and documents. For example, apply a promotion code to an account needs change both promotion collection and user account collection. The changes can happen in parallel or sequence which do not matter.
The only important thing is the operation will only affect documents that has not this pending transaction and the operation on the document is done with pushing the pending transaction to the doc.
This will need following design:
- the collection should have a
pendingTransactionfield which record all current pending transactions on a document.
- Developer should use
updatemethod to update target fields and the pendingTransaction at the same time.
After all operations finished, transaction will be marked as
applied. If any problems happens during the transaction (e.g. system crashes), the next run through the transaction will ‘skip’ the documents that already being applied and continue applying other operations until the transaction is marked as
The transaction has made modifications to all related documents. This stage is to clear the pendingTransaction field of those documents ($pull the transaction id from the field.)
The transaction has completed
The lastModified field in transaction can be used to judge if a transaction needs to roll-back. (e.g. the lastModified was 5 mins ago but still not successful)
pending transactions need to be rolled-back as it means there are unfinished changes and some changes have been applied. The roll-back operation is a reverse operation of application operation. e.g. for user document with transaction id in pendingTransaction, pull transaction id from pendingTransaction and decrease the money amount.