File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Locking issues 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 issues" Watch "Locking issues" New topic
Author

Locking issues

Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11525
    
100

Hi Surya,

You can use the java.rmi.server.Unreferenced interface to get notification of when an RMI connection is lost. Similar to Peter's manual sending of keep alive messages, RMI itself sends keep alive messages and will call the unreferenced() method when it notices that a client is no longer responding to these keep alive messages.

Alternatively you can use WeakReferences (especially the WeakHashMap) to store your locks. When the distributed garbage collector is notified by RMI that a client is no longer connected, there will no longer be a reference on the lock, so it will be automagically cleared. Phil talks about the ramifications of this design in this thread.

Both these solutions assume that you have a unique connection per client, which implies some sort of connection factory on the server.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Surya Kumar
Ranch Hand

Joined: Aug 21, 2004
Posts: 52
Andrew:
In my case i am using one data object where all the clients connects to. The design is such that all the clients connect to one single data access object. So unReference may not be feasible!!


Regards<br />Prem<br />SCJP, SCJD, SCWCD, SCBCD, SCEA, SCJDWS
Surya Kumar
Ranch Hand

Joined: Aug 21, 2004
Posts: 52
Any way how much these are going to affect my scoring, if i did'nt implemnent the orphan clean up! Just wondering!
Surya Kumar
Ranch Hand

Joined: Aug 21, 2004
Posts: 52
Is it necessary that we have to always clean up? I have a locking solution that work perfectly. I am using a FIFO queue of recordnumbers, that got notified in a sequence. This works fine. But if a client that's on the queuegoes down for whatever reasons other clients waiting on this record numbers are un rerachabl. Does anybody has any idea of how to do a proper thread cleaning? Or is it necessary at all? I am doing B&S. Thanxes in advance.
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Surya Kumar:
Is it necessary that we have to always clean up? I have a locking solution that work perfectly. I am using a FIFO queue of recordnumbers, that got notified in a sequence. This works fine. But if a client that's on the queuegoes down for whatever reasons other clients waiting on this record numbers are un rerachabl. Does anybody has any idea of how to do a proper thread cleaning? Or is it necessary at all? I am doing B&S. Thanxes in advance.


You need to associate a Data instance or some kind of Client instance with each locked record. If that Client or Data instance times out, gets unreferenced, gets closed or garbage collected you need to search through the lock map and unlock any orphaned locks.

The unlocking should be driven by your network code, since its the only place you can tell that a client has gone away. Both sockets and RMI provide ways to determine this, but they are not especially reliable since TCP won't inform a server if a client simply disappears. I've taken the approach of using an application level keep alive message and server timer to deal with this. It's described in my responses to this topic.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11525
    
100

Hi Surya,

As long as you document that you have not done any clean up of orphaned locks (and why), then you should not loose any marks.

Regards, Andrew
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Locking issues