File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Locking and remoteException

 
Linda Andersson
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Linda Andersson
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1033
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1033
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11833
181
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic