Writeconflict_Transactions_Management

The Transaction_Sample project contains the following processes:
  • ConcurrentTransaction1
  • ConcurrentTransaction2

Note: The process ConcurrentTransaction2 is a copy of the process ConcurrentTransaction1.

These processes when run concurrently demonstrate how to retry a transaction in case of a writeconflict exception. UpdateDocument, QueryDocument and RemoveDocument activities are placed inside transaction group surrounded by RepeatonError group. RepeatonError group repeats for the number of retrials configured only in the case of a writeconflict exception. The RepeatonError group terminates for any other exceptions. WriteConflict exception occurs when both the processes run at the same time. If there is a writeconflict exception in a transaction, the transaction waits for two seconds before retrial for the concurrent transaction to release the write lock. The transaction keeps retrying until the retrial count configured value reaches or no writeconflict exception is encountered.

Note: All the MongoDB Write operations internally acquire a write lock. When a transaction writes on some document and if any concurrent transaction is trying to write on the same document then a writeconflict exception occurs in the second transaction. In this case, retry the second transaction after the time entered for Retry Interval. Meanwhile, if the first transaction that has acquired document lock is completed on next retry the second transaction is successful else you can perform a limited number of retries entered in Number of Tries for the second transaction to succeed. This mitigates the WriteConflict exception transactions failures.
The following table describes the activities in this process.
Activity Description
CleanDocuments Removes all the documents in the specified collection.
InsertDocument Inserts a document in the specified collection that is used by activities inside local transaction group. Specified collection is created if it does not exist. This activity is used because transactions work only on existing collections.
Note: You might encounter a duplicate id exception in one of the processes. This exception can be ignored as it does not affect transaction and the transaction can be retried when WriteConflict exception is encountered.
UpdateDocument This is the first activity inside a transaction. Transaction starts with a session that is used for subsequent activities inside group. Updates the inserted document with a field in the specified collection using the session.
QueryDocument Queries the inserted document in the specified collection using the session.
RemoveDocument Removes the document inserted in the specified collection using the session.
ErrorCondition Error transition is created from transaction group. If the exception is writeconflict redirecting to WriteConflictSleep activity and for other types of exceptions or when the retrial count exceeds for writeconflict exceptions terminating RepeatonError using condition expressions.
WriteConflictSleep Makes the transaction to wait for the specified time interval.