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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "locking, locking,...." Watch "locking, locking,...." New topic
Author

locking, locking,....

Muhammad Shafique
Ranch Hand

Joined: Sep 30, 2006
Posts: 57
Hi folks,
My specification:

Any attempt to lock a resource that is already locked should cause the current thread to give up the CPU, consuming no CPU cycles until the desired resource becomes available.


What if one record is locked forever (locking client is crashed or ...)?

In my application thread waits for some time, if it could not get the lock, server throws an exception with the message that record is locked by some other client.

Data is displayed on table. What if you want to updated a record which is already updated by some other user but those updates are not visible to you? Shall we overwrite?

Comments please....!

Regards,
Shafique
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11285
    
  59

Hi Shafique,

According to the specifications you have listed, you should not be throwing an exception after any length of time - the client just waits until the lock becomes available (no matter how long that is).

There are many ways of handling crashed clients. Some of the more popular include handling the exception thrown when a socket is closed in the case of a socket solution, using the Unreferenced class in the case of RMI, or using a WeakHashMap for either solution. There are other possibilities as well, many of which have been discussed in this forum - for example Phil Maquet described an alternate to the WeakHashMap where the value was the WeakReference instead of the Key.

Data is displayed on table. What if you want to updated a record which is already updated by some other user but those updates are not visible to you? Shall we overwrite?
But that invalidates the (limited) value in having the lock() and unlock() methods. If you use those two methods correctly, you can write a client that will never overwrite another client's modifications.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3206
    
    2
Originally posted by Muhammad Shafique:


Data is displayed on table. What if you want to updated a record which is already updated by some other user but those updates are not visible to you? Shall we overwrite?


In the absence of versionid, you can send all the values that the client currently has for a row and the new values in the book method. The book method can lock the row, check if the values in the db are same as what the client had, and if same, apply the updates and release the lock. If the values are not same, you can throw an exception or return a status that tells the client to refresh the view and try booking again.

HTH,
Paul.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Muhammad Shafique
Ranch Hand

Joined: Sep 30, 2006
Posts: 57
Thanks Andrew & Paul for your reply!

Andrew;
- I have removed timer from lock method and know it waits until notified by unlock.
- My specifications does not say any thing about crashed client and I have seen many people on this forum who didn't implement this and got high scores. I read interesting post by Philippe Maquet about WeakHashMap (September 11, 2003) but the big constraint for me is time.

Paul;
- I am doing this pre-update check for the selected row. If the row is updated and not visible, an exception RecordChangedException is thrown by the GUI controller. User has to book/release record again because I am updating view with server data not with the data entered by this user.

Many thanks,
Shafique
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: locking, locking,....
 
Similar Threads
Is my locking mechanism going to work?
Locking -> No wait/notify needed?
delete in B&S
B&S: Same Questions
Record locking on recNo