I am working on a website which displays two h:dataTables. One lists all reserved items and the other one the ones which aren't reserved. The user should be able to click on itmes in the non reserve list in order to reserve them. So far, anything is working fine. The problem starts as soon as two users (with different browsers/sessions) want to reserve the same item in quasi parallel.
This is the timing when the problem occurs:
User A: load page
User B: load page
User A: reserve item 1
User B: reserve item 2 (<- that's were the problem arises)
I have a check in order to see whether the item already has been reserved or not in the meantime. This check is based on the h:column item's properties which are submitted with the reservation request. My problem is that for User B the returned properties are the ones of item 2 instead of item 1.
Here are my code snippers:
### 1 ### At this point User B already gets the 'itemName' from item 2 instead of item 1.
### 2 ### Reread database. In that way the page is refreshed (reserved item goes into second list)
Is there any mistake in my code/logic? Can anyone suggest a way to handle this probably (i.e., User B sees the error message: item already reserved instead of unknowingly reserving item 2)?
Ah, yes, good old ACID. Not much different in JSF than anywhere else, actually.
Doing a query isn't really optimal here, because you have to be able to lock the query while you are doing the update.
What I recommend is that you use the SELECT to get the initial table display, then on the AJAX listener that attempts to make the reservation, do a brute-force update attempt sourrounded by a try/catch so that if another user had already made the reservation, your second attempt will bounce, be intercepted, and you can report the bad news. If the reservation hasn't been made (exception thrown), commit the reservation attempt.
ORM systems are especially useful for stuff like this, and even more so when paired with a good support framework such as Spring.
"privilege" comes from the Latin words for "private" and "law" (legal) and dates to feudal times. To "claim privilege" meant that you were above the laws that applied to the common people.
New rule: no elephants at the chess tournament. Tiny ads are still okay.