/** Stop managing a location. Use with great care (testing only). */ public static synchronized void expel(Location location, boolean force) { StoreConnection sConn = cache.get(location) ; if (sConn == null) return ; if (!force && sConn.transactionManager.activeTransactions()) throw new TDBTransactionException("Can't expel: Active transactions for location: " + location) ; // No transactions at this point (or we don't care and are clearing up forcefully.) sConn.transactionManager.closedown() ; sConn.baseDSG.close() ; sConn.isValid = false ; cache.remove(location) ; ChannelManager.release(sConn.transactionManager.getJournal().getFilename()) ; // Release the lock if (SystemTDB.DiskLocationMultiJvmUsagePrevention) { if (location.getLock().isOwned()) { location.getLock().release(); } else if (location.getLock().canLock()) { SystemTDB.errlog.warn("Location " + location.getDirectoryPath() + " was not locked, if another JVM accessed this location simultaneously data corruption may have occurred"); } } }
@Test(expected = TDBException.class) public void location_lock_dir_error_02() throws IOException { Assume.assumeTrue(negativePidsTreatedAsAlive); Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Write a fake PID to the lock file try (BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) { // Fake PID that would never be valid writer.write(Integer.toString(-1234)); } Assert.assertTrue(lock.isLocked()); Assert.assertFalse(lock.isOwned()); // Attempting to release a lock we don't own should error Assert.assertFalse(lock.canObtain()); lock.release(); }
@Test(expected = TDBException.class) public void location_lock_dir_error_02() throws IOException { Assume.assumeTrue(negativePidsTreatedAsAlive); Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Write a fake PID to the lock file try(BufferedWriter writer = new BufferedWriter(new FileWriter(dir.getPath("tdb.lock")))) { // Fake PID that would never be valid writer.write(Integer.toString(-1234)); } Assert.assertTrue(lock.isLocked()); Assert.assertFalse(lock.isOwned()); // Attempting to release a lock we don't own should error Assert.assertFalse(lock.canObtain()); lock.release(); } }
@Test public void location_lock_dir_01() { Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Try to obtain the lock lock.obtain(); Assert.assertTrue(lock.isLocked()); Assert.assertTrue(lock.isOwned()); // Release the lock lock.release(); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); }
@Test public void location_lock_dir_01() { Location dir = Location.create(tempDir.getRoot().getAbsolutePath()); LocationLock lock = dir.getLock(); Assert.assertTrue(lock.canLock()); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); Assert.assertTrue(lock.canObtain()); // Try to obtain the lock lock.obtain(); Assert.assertTrue(lock.isLocked()); Assert.assertTrue(lock.isOwned()); // Release the lock lock.release(); Assert.assertFalse(lock.isLocked()); Assert.assertFalse(lock.isOwned()); }