Daniel
Daniel

Reputation: 2662

Prevent simultaneous DB Transactions on play2.1 application

I came across a serious issue with our payment workflow today.
Once a payment is completed, two things happen:

When the user gets redirected to the success-page we check whether a payment with the payment id exists. If this is not the case we create a pending payment that we can show to him.

When the payment provider sends a payment notification we check whether there exists a pending payment with the payment id and set it to active, if no payment exists we just create an active one.

Here's the problem: Both events can (and did!) happen simultaniously such that both routines did not find an existing payment and so two payments got created. One pending and one active.

Is there a good way to circumvent this behaviour by possibly using locks or something? I'm using the default Ebean ORM on a java based play 2.1.0 with a MySQL db.

Upvotes: 1

Views: 123

Answers (2)

Christopher Hunt
Christopher Hunt

Reputation: 2081

This isn't a Play thing at all. My advice is to not use locks. If you do then you will limit the scalability of your application.

You need to cope with this race condition. Race conditions will always occur and there's nothing you can do to prevent them. So, embrace them.

In the instance you describe its sounds like the pending state should be overridden by the active state if both exist in the database.

Upvotes: 1

djangofan
djangofan

Reputation: 29669

I don't know if this answers your question or not, but here is a link to the Ebean transactions page.

Upvotes: 1

Related Questions