• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX:what is lockCookie

 
Nick Lee
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my assignment(Hotel booking system),it asks me to return a "long lockCookie" in my lockRecord(long recordNo) method,and want this cookie to be used only be this client,can anyone tell me how to produce this cookie and what is its format?
Thanks
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nick Lee:
In my assignment(Hotel booking system),it asks me to return a "long lockCookie" in my lockRecord(long recordNo) method,and want this cookie to be used only be this client,can anyone tell me how to produce this cookie and what is its format?
Thanks

I dont have that setup, but it looks like there is no format except that its a long, which you can generate in any number of ways, System.currentTimeMillis() is the simplest
 
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 Ta Ri Ki Sun,
you can generate in any number of ways, System.currentTimeMillis() is the simplest

Nice suggestion - this could also have value in setting timeouts (out of scope, I know ), however it may not generate a unique number. From the Java API documenation for System.currentTimeMillis():
Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

So if you were operating on one of those operating systems that returns a number in tens of milliseconds, and it takes less than 10 milleseconds for your locking routine to work, then you could get a duplicate number.
Alternatively you could have synchronized methods getting an auto incrementing number.
Regards, Andrew
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andrew Monkhouse:
Hi Ta Ri Ki Sun,

So if you were operating on one of those operating systems that returns a number in tens of milliseconds, and it takes less than 10 milleseconds for your locking routine to work, then you could get a duplicate number.
Alternatively you could have synchronized methods getting an auto incrementing number.
Regards, Andrew

yep, well spotted, and he could also have the current time in millis with a Math.random combo and check if he's already assigned a matching long when he's done, but lets leave some implementation details and maybe even some pitfalls to him
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with TQ, part of the fun is coming up with one's own ideas (it's also one of the purposes of the SCJD). So Nick, the cookie is just some unique identifier you have to cook up and serve to clients so that they can be uniquely identified. This should raise some more very interesting questions regarding what to do if clients go away never to return, or the server crashes, or...
 
S. Ganapathy
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the user wish to update/delete the record, then locking that record has meaning. But to read the record, even if the record is locked, user can read that record. SecurityException is thrown on updateRecord(recordNumber), and deleteRecord(recordNumber). So what I am thinking is lock cookie is the same as record number(lockCookie = recordNumber). This lock cookie is stored in the data structure object of locked records. So who ever the user, update/delete the particular record number, they find in the locked records data structure, and wii get thrown security exception. If I am wrong, please correct me.
GVRao
[ April 22, 2003: Message edited by: Venkateswara Rao Gunturu ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic