• 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

Can I return the cookie number the same as recNo in lockRecord(long recNo);

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, all

I just searched the forum, I unstood the reason to use look cookie. I just want to know if I can return the same number as the recNo as the look cookie in the method:

.

I image the recNo is the record's location in the db file & it is also unique. I think that is enough to use recNo to distinguish from different clients. Am I right?
 
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
Hi pkinuk,

if you return the recNo and 2 clients (A and B) are trying to lock record 1, they both will get 1 as lockCookie (according to your logic) and so both clients can update record 1 (because they have the correct lockCookie value). A situation you are trying to prevent with the use of lockCookie.

[edit] as Naveen pointed out correctly: just one record at a time will own the lock (and the lockCookie), so the above scenario will never happen and recNo can be used as lockCookie

Kind regards,
Roel

 
Ranch Hand
Posts: 114
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,
I have a doubt, will this scenario work ...

Thread A & B races for lock on record 1.
Thread A locks record 1 gets cookie 1.
Thread B trys to lock record 1, sees its locked, waits.
Thread A performs update on record 1 and unlocks it.
Thread B gets notification, wakes up.
Thread B locks record 1 gets cookie 1.
Thread B updates record 1, and releases lock.

2 clients (A and B) are trying to lock record 1, they both will get 1 as lockCookie ...


this happens only if A&B can lock record 1 at same time, seems thats not possible with a lockRecord like that I used

Since recNo is unique, will that substitute a unique-cookie ...
 
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
Hi Naveen,

Your scenario will work (of course), because (as you pointed out) 1 record at a time can have the lock on a record (and the according lockCookie). I was a bit too fast But I still think it's not a good idea to use recNo as lockCookie (easy to hack of course). Using System.nanoTime() is as easy as using the recNo, but offers a lot more safety.

Kind regards,
Roel
 
Naveen Narayanan
Ranch Hand
Posts: 114
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its okay Roel the fast-man
reply
    Bookmark Topic Watch Topic
  • New Topic