This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Updating h:dataTable + multiple users Problem  RSS feed

Oliver Hanka
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:


item Class:

itemList Class:

### 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)?
Tim Holloway
Posts: 18709
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!