Handling the concurrent request while persisting in oracle database?

后端 未结 4 997
孤独总比滥情好
孤独总比滥情好 2020-12-02 01:23

I have this scenario ,on a airline website (using Java) two separate customers send two requests at same time to book a same seat in same airline
from New York to Chica

4条回答
  •  孤街浪徒
    2020-12-02 01:33

    Aside from doing everything in a single UPDATE by carefully crafting WHERE clause, you can do this:

    Transaction 1:

    • SELECT ... FOR UPDATE exclusively locks the row for the duration of the transaction.
    • Check if the returned status of the row is "booked" and exit (or retry another row) if it is.
    • UPDATE the row and set its "status" to "booked" - it is guaranteed nobody else updated it in the meantime.
    • Commit. This removes the exclusive lock.

    Transaction 2:

    • SELECT ... FOR UPDATE blocks until Transaction 1 finishes, then exclusively locks the row.
    • The returned status of the row is "booked" (since Transaction 1 marked it that way), so exit (or possibly retry another row).

提交回复
热议问题