• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
pkinuk Buler
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • 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?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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

 
Naveen Narayanan
Ranch Hand
Posts: 114
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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 Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its okay Roel the fast-man
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic