Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Do I need to synchronize the book_ticket method to avoid a race condition in a theater webapp?

 
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there's a ticket booking app, since every user comes with a new thread, there are chances multiple users do click on the same "seat" at the same time and proceed to book. There are chances the final payment submission also may happen concurrently. How to avoid race condition in this case?

Thanks in advance.
 
Arun Singh Raaj
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When only one user out of all concurrent users does book a particular seat and the booking happens after the payment is made. Then what happens to the payment of remaining users who were discarded to book because one has already occupied the seat. How does the payment get reverted?
Thanks.
 
Saloon Keeper
Posts: 5696
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Web sites I'm familiar with solve this by reserving the seats for a user for some time (15 minutes or so). So the same seats aren't sold twice, and the problem is avoided.
 
Saloon Keeper
Posts: 10396
221
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if two users try to reserve a seat at the same time, it can be solved by performing the reservation (making a change in the database) in a database transaction. If the transaction fails because a seat was already reserved, the user can be notified of this.
 
Arun Singh Raaj
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:And if two users try to reserve a seat at the same time, it can be solved by performing the reservation (making a change in the database) in a database transaction. If the transaction fails because a seat was already reserved, the user can be notified of this.


Hi, what makes the transaction fail in this case? try to overwrite the PK in db? (But the PK will be different for every transaction when PK is auto-generated.)
How does Hibernate make sure the specific seat already exist in db? (If you say you run a query that checks if the specific seat# with the specific date already exist then still concurrent access of multiple threads may pass this condition.)
Thanks.
 
Stephan van Hulst
Saloon Keeper
Posts: 10396
221
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already know the IDs of the seats. The seats aren't created when the user reserves them, they're already there. The user picks a seat (or is assigned one) and when a reservation is made, you could add a reservation entry that refers to a seat. You can add constraints to the database so that no two reservations for the same viewing may refer to the same seat. The simplest way to do this is by giving the reservation a compound unique key that consists of the viewing ID and the seat ID. When two reservations are made concurrently for the same seat, one of the two updates will cause an error, and make the entire transaction fail. You don't need a separate query to first check if the seat was already reserved.

Even if you wanted to perform multiple queries in a single transaction, and two transactions occur concurrently, you can control to what extent the transactions can interfere with each other by setting the transaction isolation level.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!