aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Locking and remoteException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Locking and remoteException" Watch "Locking and remoteException" New topic
Author

Locking and remoteException

Linda Andersson
Greenhorn

Joined: Dec 18, 2003
Posts: 21
Hi!
Do anyone have any suggestions to have to handle the following problem.

1. Client A locks record 3.
2. A network problem occurs when Client A calls the server to update the record and a RemoteException is thrown. Which means that record 3 will not be unlocked.
3. Client B tries to lock record 3. Since record 3 is locked the client goes into waiting.

The problem above is that record 3 never will be unlocked by client A which means that Client B never will leave waiting.

Does anyone have a solution to this problem?

//Linda
Linda Andersson
Greenhorn

Joined: Dec 18, 2003
Posts: 21
I just want to add that I tried solving the problem through adding a timestamp to every cookie.
Then I had a thread that checked if the timestamp was more than a certain number of seconds old. If it was I removed the cookie.

Sun did not like this at all so they gave me very low points on the Locking part.

//Linda
Mike Vess
Ranch Hand

Joined: Aug 25, 2004
Posts: 41
Do you really think you have to solve this problem? There is nothing about this in the requirements.

Sun did not like this at all so they gave me very low points on the Locking part.


Did you fail the assignment or what?


What you thought was right today may need a refactoring tomorrow...
Linda Andersson
Greenhorn

Joined: Dec 18, 2003
Posts: 21
Yes I did fail and I only got 7 out of 80 points in locking. In the rest of the asignment I lost very few points.

The thing is that this problem causes a deadlock if it occures and it does not feel good to ignore that kind of a problem.

//Linda
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Linda Andersson:
Yes I did fail and I only got 7 out of 80 points in locking. In the rest of the asignment I lost very few points.

The thing is that this problem causes a deadlock if it occures and it does not feel good to ignore that kind of a problem.

//Linda


If you want to solve the problem of lost connections you should do this in the network code.

Keep a reference to the Data object in the lock map and unlock all records owned by that data object if the network disconnects its client. If you are using RMI you can do this using the Unreferenced interface.

For more details do a search on Unreferenced.
Linda Andersson
Greenhorn

Joined: Dec 18, 2003
Posts: 21
I have now been reading about Unreferenced. To me it seems like the server reacts when it has lost contact with all clients. My problem is if one client looses contact with the server before it has unlocked a specific record.

//Linda
Mogens Nidding
Ranch Hand

Joined: Mar 08, 2004
Posts: 77
I think I read about people in this forum getting full locking scores without using Unreferenced or WeakHashMap or the like. I regard dead client detection as an "extra" feature. Could a 7/80 indicate that there is an error in something fundamental? Did you try out George's "super software exerciser"? (try searching for it in the forum). These are just ideas.
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Linda Andersson:
I have now been reading about Unreferenced. To me it seems like the server reacts when it has lost contact with all clients. My problem is if one client looses contact with the server before it has unlocked a specific record.

//Linda


Unreferenced.unreferenced() is called when a remote object on the server has no remaining references from clients. In my code the server publishes a DataAdapterFactory in the registry. This factory creates a DataAdapter for any client that needs one. If the client goes away its DataAdapterImpl instance on the server will become unreferenced when the lease runs out. Since the DataAdapterFactory is registered with the registry, it never gets unreferenced.

It is true that the problem of lost connections is outside the required scope of this project. I suspect the low locking score is caused by a fundemental failure of the locking code. Did you test it with multiple threads?

Here's a simple multithreaded lock tester for the URLyBird project:

Mike Vess
Ranch Hand

Joined: Aug 25, 2004
Posts: 41
Yes I did fail and I only got 7 out of 80 points in locking. In the rest of the asignment I lost very few points.


Did sun tell you what they thought was wrong with your locking solution or is the message only that you failed?
Linda Andersson
Greenhorn

Joined: Dec 18, 2003
Posts: 21
No, sun did not tell me what I had done wrong. But since I only got 7 out of 80 I guess that I had done the locking part really wrong.

//Linda
John Winstanley
Greenhorn

Joined: Apr 08, 2003
Posts: 14
Linda I don't see why your approach was considered so bad perhaps there was a functional problem.

Do you think this a valid lock method?


Where some SomeLockError is an unchecked exception.
[ January 21, 2005: Message edited by: John Winstanley ]

[ January 21, 2005: Message edited by: John Winstanley ]

[Andrew: Broke up really long line of code.]
[ January 23, 2005: Message edited by: Andrew Monkhouse ]

John
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11483
    
  94

Hi Linda & John,

Linda - Sorry to hear you failed.

I know many people have passed (with good scores) without any orphaned lock processing. As per Peter's suggestion, you might want to reconsider your locking strategy to ensure it does meet the requirements (and does not change any requirements). Some things to think about: Do you handle the case where one thread deletes a record while another thread is waiting for the lock? Do you handle the case where two threads are trying to create a record simultaneously?

As Peter mentions, the unreferenced() method is called when there are no references to a particular remote object. If you use a factory to create one remote object per connected client then unreferenced() will be called when that particular client disconnects. You will still need some way of tracking which locks are still owned by that particular client though.

Another alternative is to use a WeakHashMap to store the locks / client references (again, this requires a unique remote object per connected client). Any orphaned locks will automatically be removed from the WeakHashMap sometime after the client disconnects (exact timing dependant on lease value). You would also need a separate thread monitoring the expected locks versus the actual number of locks, and call notifyAll() if the two numbers differ.

John - I suspect that your solution will not meet the requirements. Check the wording of the API documentation you were provided - does it imply that you can have a timeout at all? If you can have a timeout, do you think having the timeout on the thread attempting to aquire the lock is correct?

Regarding the exception thrown - it lists two possible reasons, only one of which is valid. You should reduce your message to show the correct cause.

You also try to aquire the lock before checking whether the record is valid or not. You might want to consider having a separate (private) method to check validity of the record, in which case your code could be reduced to:

The recordIsValid() method can throw the RecordNotFoundException directly.

Also - you really should not have such a long line anywhere in your code. The Sun Coding Conventions suggest you should Avoid lines longer than 80 characters, since they're not handled well by many terminals and tools.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Locking and remoteException