fixed leaky lock (answers todo about verifying that channel.close() should be called...
authorjprocter <jprocter@compbio.dundee.ac.uk>
Fri, 24 Mar 2006 19:13:22 +0000 (19:13 +0000)
committerjprocter <jprocter@compbio.dundee.ac.uk>
Fri, 24 Mar 2006 19:13:22 +0000 (19:13 +0000)
git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@211 be28352e-c001-0410-b1a7-c7978e42abec

src/org/vamsas/client/simpleclient/Lock.java

index 5c052f6..903214d 100644 (file)
@@ -64,21 +64,24 @@ public class Lock {
     return false;
   }
   public void release() {
+    release(true);
+  }
+  public void release(boolean closeChannel) {
     try {
-      // TODO: verify that channel.close should be called after release() for rigourous locking.
+      // channel.close should be called before release() for rigourous locking.
       if (rafile!=null && rafile.getChannel()!=null) {
         if (rafile.getChannel().isOpen()) {
+          if (closeChannel && rafile.getChannel().isOpen())
+            rafile.getChannel().close();
           if (lock!=null && lock.isValid())
             lock.release();
-          if (rafile.getChannel().isOpen())
-            rafile.getChannel().close();
         }
       }
     } catch (IOException e) {
       log.warn("Whilst releasing lock",e);
     }
     lock=null;
-    rafile=null;
+    rafile=null; 
   }
   
   /**
@@ -136,7 +139,8 @@ public class Lock {
    * @see java.lang.Object#finalize()
    */
   protected void finalize() throws Throwable {
-    release();
+    release(true); // we explicitly lose the lock here.
+    // log.debug("lock closing through garbage collection ?");
     super.finalize();
   }