• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Business Layer concurrency

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just had a quick question. Are you guys handling Business service concurrency as well?

So what if Thread 1 comes in, get a lock on rec #2, then Thread 1 completes and books the room. Then thread 2 comes in at the same time and is waiting for the lock. Do you have some type of kick-out on the update method to say that if the Room is not available, then throw an exception?

This is how I implemented it.. I was wondering what you guys did.

Because is doing this enough??

if(room.isAvailable()) {
database.update(recNo);
} else throw RecordNotFoundException.

Or would you have to do what I did, and if two records get a lock, the second one that tries to update is kicked out.

This is the call from the business service.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My book room method (in the business service) contains all necessary business logic. And after a record is locked successfully I read the record again to verify if the room is still available and was not already booked by another client (thread). If it is, a specific business exception is thrown (and the record is unlocked). Otherwise record is updated and unlocked.

Hope it helps!
 
S. Thakker
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its a synchronized block correct? I am wondering if this is a must requirement... I am guessing it would be
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it's not a synchronized block. That's not needed at all. It even prevents your application from being a multi-threaded application (and thus could result in failure).
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S. Thakker wrote:Are you guys handling Business service concurrency as well?



Hum... no. Only in the Data class (which is just what is required).


So what if Thread 1 comes in, get a lock on rec #2, then Thread 1 completes and books the room. Then thread 2 comes in at the same time and is waiting for the lock. Do you have some type of kick-out on the update method to say that if the Room is not available, then throw an exception?

This is how I implemented it.. I was wondering what you guys did.

Because is doing this enough??

if(room.isAvailable()) {
database.update(recNo);
} else throw RecordNotFoundException.

Or would you have to do what I did, and if two records get a lock, the second one that tries to update is kicked out.

This is the call from the business service.



Hum... I don't think that's gonna work. Because if both Threads come at the same time, both of them will get true for the room.isAvailable() method. Before updating the record, you should call the lock() method, verify if the client id of the record is still empty; if so, then you go ahead and update the record and unlock the record, otherwise, throw a business exception and unlock the record. Something like this:

 
S. Thakker
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that code is executed after the lock method... For roel, I was talking about synchronizing the update with the check, however I now realize the otehr threads are waiting on the lock, so theres no threat
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

S. Thakker wrote:however I now realize the otehr threads are waiting on the lock, so theres no threat


 
reply
    Bookmark Topic Watch Topic
  • New Topic