This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generate cookie for locking

 
Terence Lai
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My certification assignment asks me to return a cookie (long data type) upon a specified record is locked. I am trying to come up a solution of generating the cookie. Is timestamp a good choice for the cookie value(i.e. (new java.util.Date()).getTime())?

Alternately, I can use a sequence number as the cookie. By doing so, I have to mantain a counter and increment the value by 1 whenever a cookie value is returned.

I personally prefer to use the timestamp since it does not cause any maintenance effort. It is also relatively harder to guess the cookie value comparing the use of the sequence number. Does anyone has a better suggestion? Do you see any problem of using the timestamp?

Thanks.
[ December 16, 2007: Message edited by: Terence Lai ]
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I don't have cookie in my assignment, so the following is pure opinion..

I don't consider timestamp to be "unique" enough. In my mind, 2 clients could get the same "cookie-timestamp" and then accessed each other's record.

If you search on this forum, you'll find A LOT of information on the generation of that cookie.

javarach-009347
javaranch-010079
javaranch-008322

Personally, I think I would synchronized 1 line of code that creates a cookie incremented by 1. Each client would get a "cookie + 1" value.. But I haven't done a lot of thinking on that, just a quick opinion..

Regards,
Alex
 
Herman Schelti
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Terence,

I used System.currentTimeMillis() and got 80/80 on locking.

Herman
 
Terence Lai
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex/Herman,

Thanks for you feedback and inputs. They are very helpful.

Terence
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic