/** * Gets whether the location is currently locked * * @return boolean */ public boolean isLocked() { // Memory locations are never considered locked if (location.isMem()) return false; return getOwner() != NO_OWNER; }
/** * Gets whether the lock is owned by the current process * * @return True if the location is locked and owned by the process, false * otherwise */ public boolean isOwned() { // Memory locations are never considered locked if (location.isMem()) return false; int owner = getOwner(); if (owner == NO_OWNER) return false; return owner == ProcessUtils.getPid(NO_OWNER); }
/** * Gets whether the current JVM can obtain the lock * * @return True if the lock can be obtained (or is already held), false * otherwise */ public boolean canObtain() { // Memory locations cannot be locked if (location.isMem()) return false; int owner = this.getOwner(); int pid = ProcessUtils.getPid(NO_OWNER); if (owner == NO_OWNER) { // Can obtain provided we have a valid PID if (pid != NO_OWNER) return true; } else if (owner == pid) { // Already held return true; } // Owned by another process, only obtainable if other process is dead if (!ProcessUtils.isAlive(owner)) return true; // Otherwise not obtainable return false; }
/** * Releases the lock so that other JVMs can use the location */ public void release() { // Memory locations cannot be locked so nothing to do if (location.isMem()) return; int owner = this.getOwner(); // Nobody owned the lock so nothing to do if (owner == NO_OWNER) return; // Some other process owns the lock so we can't release it if (owner != ProcessUtils.getPid(NO_OWNER)) throw new TDBException("Cannot release the lock on location " + location.getDirectoryPath() + " since this process does not own the lock"); File lockFile = getLockFile(); // No lock file exists so nothing to do if (!lockFile.exists()) return; // Try and delete the lock file thereby releasing the lock if (!lockFile.delete()) throw new TDBException("Failed to release the lock on location " + location.getDirectoryPath() + ", it may be necessary to manually remove the lock file"); }
if (!lock.canObtain()) { int here = ProcessUtils.getPid(0); throw new TDBException("Process ID "+here+" can't open database at location " + location.getDirectoryPath() + " because it is already locked by the process with PID " + lock.getOwner() + ". "+ "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("Process ID "+here+" failed to open database at location " + location.getDirectoryPath() + " because it is already locked by the process with PID " + lock.getOwner() + ". "+ "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");
int owner = this.getOwner(); int pid = ProcessUtils.getPid(NO_OWNER); if (owner == NO_OWNER) {