• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: Lock Cookie

 
rubens gama
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must implement the following method:
public long lock(int recNo) throws RecordNotFoundException;

how can I implement a synchronized method to generate the lock cookie?
a serial number initialized with X and incremented for each method invocation?
Thanks in Advance.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rubens,
Welcome to JavaRanch.
how can I implement a synchronized method to generate the lock cookie?
a serial number initialized with X and incremented for each method invocation?

That should work without problems.
It is a little bit insecure (if I lock a record and get cookie number '5', then logically I might know that cookie number '4' has been issued - if I was a malicious coder I could then try and unlock records that weren't issued to me). Those people who were concerned about this have generally used random numbers as the cookies (see the Random class). However you may decide that security is not a requirement for this assignment, in which case the incrementing number should be fine.
Regardless of which way you do it, you should keep track of which cookies are currently issued for each record.
Regards, Andrew
 
Ben Zung
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regardless of which way you do it, you should keep track of which cookies are currently issued for each record.

Hi, Andrew:
I did search on "lock" and have read many of the talks about it. And this is why I am replying to this topic which dated a while back.
Why do I have to keep track of the cookie, could you please explain in more detail? Thanks.
And to be honest, the more I read about the lock/unlock topics, the more I get confused. My implementation of lock is like the following, please let me know if I missed anything:
public long lock(int recn) thorws ...{
if not a valid record throw exception
synchronize lock manager
while recn is locked already, wait
return lock manager.lock record recn
}
unlock
public void unlock(int recN, long cookie) throws ...{
if (not a valid record) throws exception
synchronize lock manager
if lock manager.get lock cookie for recN
throw security exception
lock manager.remove lock on recN
notify whoever is waiting
}
And in the remote interface implementaion, I have the following when updateing a record:
...
long mycookie = data.lock(...)
data.update(...) //or data.create() or data.delete()
data.unlock(..., mycookie)
I don't see a need for keep track of cookie.
I may just have covered the simplest case, please advise. Thanks.
bing
 
Ben Zung
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in my last post:
if lock manager.get lock cookie for recN

I meant if lock manager.get lock cookie for recN is not equal to the cookie passed in
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bing

I meant if lock manager.get lock cookie for recN is not equal to the cookie passed in[/QB]

OK - now I am confused. How are you checking that the lock cookie for recN is equal to the lock cookie passed in if you are not keeping track (storing) the lock cookie somewhere?
Your lock / unlock methods look pretty good to me.
Regards, Andrew
 
Ben Zung
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, Andre, I just realized what you meant by keep track of the cookie. Indeed I have them kept tracked in a hashmap in the lockmanager. I thought you were talking about exposing the cookie to the client, the client would keep track of them, which I personally don't think it is necessary. Client should know nothing about the locking schema except gets warnings if one of the record is not available at the time if opt to.
It is my bad. Sorry.
Your lock / unlock methods look pretty good to me

Thanks for the comment. I am confident with it now.


Bing
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic