• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BETA: lockCookie = 0

 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One very last obscure thing:
The assignment states:
To attempt to access a record without a lock, the lock cookie should be set to 0.
I don't know if I understand this correctly. If some client does not have a lock on a record but still wants to modify it, it just sets the cookie to 0. On the DB-side, if the record is already locked, the client will have to wait, but if the record is not locked, the client will be allowed to proceed without locking the record (What happens if another client tries to lock the same record while the latter is being modified by the first client?). Is my understanding correct or am I missing something very obvious?
Thanks
 
Tybon Wu
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

On the DB-side, if the record is already locked, the client will have to wait

I throw a RecordLockedException in that situation.

What happens if another client tries to lock the same record while the latter is being modified by the first client?

My guess is that lock and update are synchronized to the same object monitor.
Another question is what should happen if a record is not locked, and a client who wants to modify the record sets the lock cookie to something other than 0?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tybon,
Another question is what should happen if a record is not locked, and a client who wants to modify the record sets the lock cookie to something other than 0?
That was going to be my next question, too
 
AJ2
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lockCookie = 0 applies to local mode. In the remote mode, the caller will call lock first to obtain a lock cookie, do the required database operation (update, delete etc) and then call unlock on the cookie we got back from the initial lock call. I used a random number generator as the cookie value.
Use random() in Math class for this.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AJ2,
lockCookie = 0 applies to local mode.
That would make sense if your DB knew that it is operating in local mode. The DB implementation is exactly the same for local and network mode, only the DB server that is accessing the DB changes.
I used a random number generator as the cookie value. Use random() in Math class for this.
Not a really good idea in my opinion since you have no guarantee that the same random number won't be generated twice in a row. I think you do know that the default random number generator of Java is based on the System.currentTimeMillis() method and that's why you used it has a cookie counter, but it would be a bad idea to use this mechanism in a real-world application where you are using a random number generator for which you do not know how it is implemented.
Personally, I have a counter starting at 0 and incrementing each time a record is locked. That way I'm sure that for a pretty long time no two cookies will have the same value.
Finally,
we'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
PS: Quote from the naming policy:

For your publicly displayed name, use a first name, a space, and a last name. Obviously fictitious names or improperly formatted names may be locked out.

[ October 06, 2002: Message edited by: Valentin Crettaz ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic