Try harder to clean TDB locks on normal JVM termination (JENA-1136)

For normal JVM termination make more effort to try and clean up the lock
file when the JVM exits.  Note that this will only help when the JVM
terminates normally.


Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/0fa0e515
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/0fa0e515
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/0fa0e515

Branch: refs/heads/master
Commit: 0fa0e515f2c6d3c7ec79ac7afeed4ec56d246525
Parents: 5a46b50
Author: Rob Vesse <[email protected]>
Authored: Wed Feb 10 09:58:55 2016 +0000
Committer: Rob Vesse <[email protected]>
Committed: Wed Feb 10 09:58:55 2016 +0000

----------------------------------------------------------------------
 .../apache/jena/tdb/base/file/LocationLock.java | 25 +++++++++++---------
 1 file changed, 14 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/0fa0e515/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/LocationLock.java
----------------------------------------------------------------------
diff --git 
a/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/LocationLock.java 
b/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/LocationLock.java
index edfa7e7..3a28efb 100644
--- a/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/LocationLock.java
+++ b/jena-tdb/src/main/java/org/apache/jena/tdb/base/file/LocationLock.java
@@ -18,16 +18,15 @@
 
 package org.apache.jena.tdb.base.file;
 
-
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 
 import org.apache.jena.atlas.io.IO;
-import org.apache.jena.tdb.TDBException ;
-import org.apache.jena.tdb.sys.ProcessUtils ;
-import org.apache.jena.tdb.sys.SystemTDB ;
+import org.apache.jena.tdb.TDBException;
+import org.apache.jena.tdb.sys.ProcessUtils;
+import org.apache.jena.tdb.sys.SystemTDB;
 
 /**
  * Represents a lock on a TDB location
@@ -164,7 +163,8 @@ public class LocationLock {
             if (pid == NO_OWNER) {
                 // In the case where we cannot obtain our PID then we cannot
                 // obtain a lock
-                SystemTDB.errlog.warn("Location " + 
location.getDirectoryPath() + " cannot be locked as unable to obtain PID of 
current process, if another JVM accessed this location while this process is 
accessing it then data corruption may occur");
+                SystemTDB.errlog.warn("Location " + location.getDirectoryPath()
+                        + " cannot be locked as unable to obtain PID of 
current process, if another JVM accessed this location while this process is 
accessing it then data corruption may occur");
                 return;
             }
 
@@ -175,12 +175,9 @@ public class LocationLock {
             // Someone other process potentially owns the lock on this location
             // Check if the owner is alive
             if (ProcessUtils.isAlive(owner))
-                throw new TDBException(
-                        "Location "
-                                + location.getDirectoryPath()
-                                + " is currently locked by PID "
-                                + owner
-                                + ".  TDB databases do not permit concurrent 
usage across JVMs so in order to prevent possible data corruption you cannot 
open this location from the JVM that does not own the lock for the dataset");
+                throw new TDBException("Location " + 
location.getDirectoryPath() + " is currently locked by PID "
+                        + owner
+                        + ".  TDB databases do not permit concurrent usage 
across JVMs so in order to prevent possible data corruption you cannot open 
this location from the JVM that does not own the lock for the dataset");
 
             // Otherwise the previous owner is dead so we can take the lock
             takeLock(ProcessUtils.getPid(NO_OWNER));
@@ -197,6 +194,12 @@ public class LocationLock {
         } catch (IOException e) {
             throw new TDBException("Failed to obtain a lock on the location " 
+ location.getDirectoryPath(), e);
         }
+
+        // Mark lock for deletion on JVM exit
+        // This does not guarantee that the lock file gets cleaned up because
+        // such deletions only succeed for normal JVM termination but it should
+        // clean up the lock for normal JVM terminations
+        lockFile.deleteOnExit();
     }
 
     /**

Reply via email to