private void takeLock(int pid) { File lockFile = getLockFile(); checkLockFileForWrite(lockFile); // Write our PID to the lock file try (BufferedWriter writer = new BufferedWriter(new FileWriter(lockFile))) { writer.write(Integer.toString(pid)); writer.close(); } 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(); }