Win a copy of Svelte and Sapper in Action this week in the JavaScript 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Updating h:dataTable + multiple users Problem

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)?
Saloon Keeper
Posts: 22498
Android Eclipse IDE Tomcat Server Redhat Java 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.
New rule: no elephants at the chess tournament. Tiny ads are still okay.
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic