Search in sources :

Example 1 with Lock

use of org.neo4j.graphdb.Lock in project neo4j by neo4j.

the class NestedTransactionLocksIT method nestedTransactionCanAcquireLocksFromTransactionObject.

@Test
public void nestedTransactionCanAcquireLocksFromTransactionObject() throws Exception {
    // given
    Node resource = createNode();
    try (Transaction outerTx = db.beginTx();
        Transaction nestedTx = db.beginTx()) {
        assertNotSame(outerTx, nestedTx);
        try (OtherThreadExecutor<Void> otherThread = new OtherThreadExecutor<>("other thread", null)) {
            // when
            Lock lock = nestedTx.acquireWriteLock(resource);
            Future<Lock> future = tryToAcquireSameLockOnAnotherThread(resource, otherThread);
            // then
            acquireOnOtherThreadTimesOut(future);
            // and when
            lock.release();
            //then
            assertNotNull(future.get());
        }
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) OtherThreadExecutor(org.neo4j.test.OtherThreadExecutor) Node(org.neo4j.graphdb.Node) Lock(org.neo4j.graphdb.Lock) Test(org.junit.Test)

Example 2 with Lock

use of org.neo4j.graphdb.Lock in project neo4j by neo4j.

the class ManualAcquireLockTest method releaseReleaseManually.

@Test
public void releaseReleaseManually() throws Exception {
    String key = "name";
    Node node = getGraphDb().createNode();
    tx.success();
    Transaction current = tx.begin();
    Lock nodeLock = current.acquireWriteLock(node);
    worker.beginTx();
    try {
        worker.setProperty(node, key, "ksjd");
        fail("Shouldn't be able to grab it");
    } catch (Exception ignored) {
    }
    nodeLock.release();
    worker.setProperty(node, key, "yo");
    try {
        worker.finishTx();
    } catch (ExecutionException e) {
    // Ok, interrupting the thread while it's waiting for a lock will lead to tx failure.
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Lock(org.neo4j.graphdb.Lock) Test(org.junit.Test)

Example 3 with Lock

use of org.neo4j.graphdb.Lock in project neo4j by neo4j.

the class ManualAcquireLockTest method canOnlyReleaseOnce.

@Test
public void canOnlyReleaseOnce() throws Exception {
    Node node = getGraphDb().createNode();
    tx.success();
    Transaction current = tx.begin();
    Lock nodeLock = current.acquireWriteLock(node);
    nodeLock.release();
    try {
        nodeLock.release();
        fail("Shouldn't be able to release more than once");
    } catch (IllegalStateException e) {
    // Good
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Lock(org.neo4j.graphdb.Lock) Test(org.junit.Test)

Example 4 with Lock

use of org.neo4j.graphdb.Lock in project neo4j by neo4j.

the class TransactionConstraintsIT method deadlockDetectionBetween.

private void deadlockDetectionBetween(HighlyAvailableGraphDatabase slave1, final HighlyAvailableGraphDatabase slave2) throws Exception {
    // GIVEN
    // -- two members acquiring a read lock on the same entity
    final Node commonNode;
    try (Transaction tx = slave1.beginTx()) {
        commonNode = slave1.createNode();
        tx.success();
    }
    OtherThreadExecutor<HighlyAvailableGraphDatabase> thread2 = new OtherThreadExecutor<>("T2", slave2);
    Transaction tx1 = slave1.beginTx();
    Transaction tx2 = thread2.execute(new BeginTx());
    tx1.acquireReadLock(commonNode);
    thread2.execute(state -> tx2.acquireReadLock(commonNode));
    // -- and one of them wanting (and awaiting) to upgrade its read lock to a write lock
    Future<Lock> writeLockFuture = thread2.executeDontWait(state -> {
        try (Transaction ignored = tx2) {
            return tx2.acquireWriteLock(commonNode);
        }
    });
    for (int i = 0; i < 10; i++) {
        thread2.waitUntilThreadState(Thread.State.TIMED_WAITING, Thread.State.WAITING);
        Thread.sleep(2);
    }
    try (// Close transaction no matter what happens
    Transaction ignored = tx1) {
        // WHEN
        tx1.acquireWriteLock(commonNode);
        // -- Deadlock detection is non-deterministic, so either the slave or the master will detect it
        writeLockFuture.get();
        fail("Deadlock exception should have been thrown");
    } catch (DeadlockDetectedException e) {
    // THEN -- deadlock should be avoided with this exception
    } catch (ExecutionException e) {
        // OR -- the tx2 thread fails with executionexception, caused by deadlock on its end
        assertThat(e.getCause(), instanceOf(DeadlockDetectedException.class));
    }
    thread2.close();
}
Also used : Transaction(org.neo4j.graphdb.Transaction) OtherThreadExecutor(org.neo4j.test.OtherThreadExecutor) HighlyAvailableGraphDatabase(org.neo4j.kernel.ha.HighlyAvailableGraphDatabase) Node(org.neo4j.graphdb.Node) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) ExecutionException(java.util.concurrent.ExecutionException) Lock(org.neo4j.graphdb.Lock)

Example 5 with Lock

use of org.neo4j.graphdb.Lock in project neo4j by neo4j.

the class ManualAcquireLockTest method makeSureNodeStaysLockedEvenAfterManualRelease.

@Test
public void makeSureNodeStaysLockedEvenAfterManualRelease() throws Exception {
    String key = "name";
    Node node = getGraphDb().createNode();
    tx.success();
    Transaction current = tx.begin();
    Lock nodeLock = current.acquireWriteLock(node);
    node.setProperty(key, "value");
    nodeLock.release();
    worker.beginTx();
    try {
        worker.setProperty(node, key, "ksjd");
        fail("Shouldn't be able to grab it");
    } catch (Exception e) {
    // e.printStackTrace();
    }
    tx.success();
    try {
        worker.finishTx();
    } catch (ExecutionException e) {
    // Ok, interrupting the thread while it's waiting for a lock will lead to tx failure.
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Lock(org.neo4j.graphdb.Lock) Test(org.junit.Test)

Aggregations

Lock (org.neo4j.graphdb.Lock)5 Node (org.neo4j.graphdb.Node)5 Transaction (org.neo4j.graphdb.Transaction)5 Test (org.junit.Test)4 ExecutionException (java.util.concurrent.ExecutionException)3 OtherThreadExecutor (org.neo4j.test.OtherThreadExecutor)2 DeadlockDetectedException (org.neo4j.kernel.DeadlockDetectedException)1 HighlyAvailableGraphDatabase (org.neo4j.kernel.ha.HighlyAvailableGraphDatabase)1