I am developing a hotel booking site. It is a J2EE 1.4 web application and using JSP and JDBC.
I have one method which is responsible to book the hotel rooms.
booking()
And from this method I am calling other four methods
bookRooms()
makePayment()
confirmUserByMail()
confirmUserBySMS()
I am aware that two users can try to book the same room at the same time and in my current system it is possible that two users might end up with the same room.
How should I handle the transaction to avoid this concurrency issue?
It might be very common scenario but I never handled this kind of scenario before, so please guide me.
I'm sure there are high level ways to do this but I would just use a simple boolean isFull and have it become true when a booking is made.
Then you can still use your current code.
To ensure that a room is only booked once one can use the synchronized keyword to ensure that the method can be used once( not the best since two different rooms can't be booked at once)
Other option is to use volatile keyword so if a room object is being accessed other methods can see that.
Source: http://www.vogella.com/tutorials/JavaConcurrency/article.html#concurrency_overview