Search in sources :

Example 1 with LocationLock

use of org.apache.jena.tdb.base.file.LocationLock in project jena by apache.

the class TestLocationLock method location_lock_dir_02.

@Test
public void location_lock_dir_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());
    Assert.assertFalse(lock.canObtain());
}
Also used : FileWriter(java.io.FileWriter) LocationLock(org.apache.jena.tdb.base.file.LocationLock) Location(org.apache.jena.tdb.base.file.Location) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 2 with LocationLock

use of org.apache.jena.tdb.base.file.LocationLock in project jena by apache.

the class TestLocationLockStoreConnection method location_lock_store_connection_02.

@Test(expected = TDBException.class)
public void location_lock_store_connection_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 create a connection on this location should error
    StoreConnection.make(dir);
}
Also used : FileWriter(java.io.FileWriter) LocationLock(org.apache.jena.tdb.base.file.LocationLock) Location(org.apache.jena.tdb.base.file.Location) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 3 with LocationLock

use of org.apache.jena.tdb.base.file.LocationLock in project jena by apache.

the class TestLocationLockStoreConnection method location_lock_store_connection_01.

@Test
public void location_lock_store_connection_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());
    // Creating a StoreConnection on the location will obtain the lock
    StoreConnection.make(dir);
    Assert.assertTrue(lock.isLocked());
    Assert.assertTrue(lock.isOwned());
    Assert.assertTrue(lock.canObtain());
    // Releasing the connection releases the lock
    StoreConnection.release(dir);
    Assert.assertFalse(lock.isLocked());
    Assert.assertFalse(lock.isOwned());
    Assert.assertTrue(lock.canObtain());
}
Also used : LocationLock(org.apache.jena.tdb.base.file.LocationLock) Location(org.apache.jena.tdb.base.file.Location) Test(org.junit.Test)

Example 4 with LocationLock

use of org.apache.jena.tdb.base.file.LocationLock in project jena by apache.

the class StoreConnection method _makeAndCache.

private static StoreConnection _makeAndCache(DatasetGraphTDB dsg) {
    Location location = dsg.getLocation();
    StoreConnection sConn = cache.get(location);
    if (sConn == null) {
        sConn = new StoreConnection(dsg);
        if (SystemTDB.DiskLocationMultiJvmUsagePrevention) {
            // Obtain the lock ASAP
            LocationLock lock = location.getLock();
            if (lock.canLock()) {
                if (!lock.canObtain())
                    throw new TDBException("Can't open database at location " + location.getDirectoryPath() + " as 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");
                lock.obtain();
                // and if not error
                if (!lock.isOwned()) {
                    throw new TDBException("Can't open database at location " + location.getDirectoryPath() + " as it is alread 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");
                }
            }
        }
        sConn.forceRecoverFromJournal();
        if (!location.isMemUnique())
            // Don't cache use-once in-memory datasets.
            cache.put(location, sConn);
        String NS = TDB.PATH;
        TransactionInfo txInfo = new TransactionInfo(sConn.transactionManager);
        ARQMgt.register(NS + ".system:type=Transactions", txInfo);
    }
    return sConn;
}
Also used : LocationLock(org.apache.jena.tdb.base.file.LocationLock) Location(org.apache.jena.tdb.base.file.Location)

Example 5 with LocationLock

use of org.apache.jena.tdb.base.file.LocationLock in project jena by apache.

the class TestLocationLock method location_lock_mem.

@Test
public void location_lock_mem() {
    Location mem = Location.mem();
    LocationLock lock = mem.getLock();
    Assert.assertFalse(lock.canLock());
    Assert.assertFalse(lock.isLocked());
    Assert.assertFalse(lock.isOwned());
    Assert.assertFalse(lock.canObtain());
}
Also used : LocationLock(org.apache.jena.tdb.base.file.LocationLock) Location(org.apache.jena.tdb.base.file.Location) Test(org.junit.Test)

Aggregations

Location (org.apache.jena.tdb.base.file.Location)8 LocationLock (org.apache.jena.tdb.base.file.LocationLock)8 Test (org.junit.Test)7 BufferedWriter (java.io.BufferedWriter)4 FileWriter (java.io.FileWriter)4