aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Clarification of the lock mechanism. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Clarification of the lock mechanism. " Watch "Clarification of the lock mechanism. " New topic
Author

Clarification of the lock mechanism.

Olena Golub
Ranch Hand

Joined: Jan 17, 2005
Posts: 113
Hello All,

My assignment contains lock() method with return value long. And it says:

Locks a record so that it can only be updated or deleted by this client.

As I understand I need to have two Arrays:
The first Array holds record number and lock-Cookies. And the second one holds the client id and the Records number to be sure that the current client has locked this record.

If one client tries to delete or update one record, I need to check if this record already locked. If the record is not locked:
1. The current client calls lock method.
2. Lock-method generates and returns to the client the lock�cookie.
3. The client saves the record number and the lock-cookie in one array.
4. The client saves the record number and the client-Id in another one.
5. �

And when unlock will be called:
1. Check the second array if the client has locked this record. I.e. the array contains the paar the record �Id and the client-Id.
2. If the array contains this client ID and the record number than deletes from the first and second arrays this record number.

I will make these two arrays as HashMap and in both cases the key will be the record number.

Could you make some comments about my locking mechanism? Can I implement this locking in this way?

Thanks a lot!
Olena.


SCJP 1.4<br />SCJD 1.4 (in progress)
Frans Janssen
Ranch Hand

Joined: Dec 29, 2004
Posts: 357
Originally posted by Olena Golub:
3. The client saves the record number and the lock-cookie in one array.
4. The client saves the record number and the client-Id in another one.


Olena,

I don't think that you need step 4. The lock cookie will be sufficient to identify a client as being the lock owner. If a client calls the update or delete method and supplies the correct cookie for the record, you can assume it is the client that locked the record. Therefore your code does not need to remember separately which client locked the record.

Frans.


SCJP 1.4, SCJD
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456

I don't think that you need step 4. The lock cookie will be sufficient to identify a client as being the lock owner. If a client calls the update or delete method and supplies the correct cookie for the record, you can assume it is the client that locked the record. Therefore your code does not need to remember separately which client locked the record.


Thinking (or coding ;-) a bit further, you'll probably stumble into the situation that an evil client (or a not-so-friendly test class) will request a lock on the same record twice. A straight forward implementation might run into the problem that client A (requesting the lock for record 1) waits forever, because only client A himself can release the lock...

From my point of view, the code should be able to handle this issue.

A simple approach would be to forbid a client to request the same lock twice. Another option might be a sort of watchdog mechanism, paired with a lock time-out rule...

Other ideas welcomed ;-)

many greets,
jan.
Frans Janssen
Ranch Hand

Joined: Dec 29, 2004
Posts: 357
Originally posted by Jan Groth:
Thinking (or coding ;-) a bit further, you'll probably stumble into the situation that an evil client (or a not-so-friendly test class) will request a lock on the same record twice. A straight forward implementation might run into the problem that client A (requesting the lock for record 1) waits forever, because only client A himself can release the lock...


Hmm, smart thinking, Jan!

In my assignment I did have the cookie-less interface, so I did not run into this issue.

This makes IMHO the cookie assignment a bit awkward, because if one must maintain a separate client-record map, one could use this map to verify that a client is a valid lock owner and then there would not be much added value in doing any double-checking on the cookie (except of course to fulfill the assgnment requirements).

Frans.
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
Frans, I've never seen it like this, you are perfectly right. Hmmm.

Well, there are more issues in the interface which can bring a coder into trouble. One of the most discussed in this forum is the fact that create() throws a DuplicateKeyException, while update() lives its life in peace and harmony.

The common consensus (a very positve one towards Sun) seems to be that they are trying to bring us into discussions. :-)

And, at the end of the day: real-life customers don't behave like academic well-defined interfaces, too. :-)

Jan.
Frans Janssen
Ranch Hand

Joined: Dec 29, 2004
Posts: 357
Originally posted by Jan Groth:
And, at the end of the day: real-life customers don't behave like academic well-defined interfaces, too. :-)


No, that's true. But the customers I have to deal with fortunately don't impose any inconsistent interfaces to me! :-)

Frans.
Olena Golub
Ranch Hand

Joined: Jan 17, 2005
Posts: 113
And I am staying with the same question: Do i need to track the client or not?
Is it enough to save only the lock-cookie and the record Id? Or do I need to save also the client Ids?

Thanks a lot for your help!
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
Originally posted by Olena Golub:

Is it enough to save only the lock-cookie and the record Id?


Yes
Olena Golub
Ranch Hand

Joined: Jan 17, 2005
Posts: 113
Do I understand it correctly?:
For assignment with lock-Method with return value we don't need to save the client id.
The cookie with the record number is enough.

And for the lock-Method without return value we should to save the client id.
Am I right?
Regards,
Olena
[ April 17, 2005: Message edited by: Olena Golub ]
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Originally posted by Olena Golub:
Do I understand it correctly?:
For assignment with lock-Method with return value we don't need to save the client id.
The cookie with the record number is enough.

And for the lock-Method without return value we should to save the client id.
Am I right?
Regards,
Olena

[ April 17, 2005: Message edited by: Olena Golub ]
Yes, you are correct. That is all you have to do for this assignment. There are better ways of doing it... but remember that for this assignment it is best to keep things simple, even if a more elegant, but more complex solution exists.


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
 
wood burning stoves
 
subject: Clarification of the lock mechanism.