'spring data mongo db multithreading issue

I have a spring boot rest application using spring data mongo (v3.0.0-release ) and I am experiencing some issues while performing load testing.

The flow from my method is as follows:

  1. get Payment from mongoDb - retrieve savedPayment object
  2. pass savedPayment to another method as parameter and check above payment db status is not accepted, if already accepted throw custom runtime exception and stop flow
  3. if not accepted set payment status to accepted and update the records proceed with flow.

On load testing same payment is being sent multiple times and it is being processed even when the second one should fail since the first one is already set as accepted.

I thought this can be a multithreading issue:

  1. I have tried to use @transaction however since it is a standalone mongo it cannot be used.
  2. I also tried to use @version however on update I cannot pass the @Version value therefore it will fail.

Any other idea how I can solve the above issue please? Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source