Search in sources :

Example 6 with TransactionImpl

use of org.neo4j.kernel.impl.coreapi.TransactionImpl in project neo4j by neo4j.

the class DeleteNodeWithRelationshipsIT method shouldDeleteDenseNodeIfContainingEmptyGroupsFromPreviousContendedRelationshipDeletions.

@Test
void shouldDeleteDenseNodeIfContainingEmptyGroupsFromPreviousContendedRelationshipDeletions() throws ExecutionException, InterruptedException {
    // given
    long nodeId;
    RelationshipType typeA = RelationshipType.withName("A");
    RelationshipType typeB = RelationshipType.withName("B");
    try (Transaction tx = db.beginTx()) {
        Node node = tx.createNode();
        nodeId = node.getId();
        for (int i = 0; i < 200; i++) {
            node.createRelationshipTo(tx.createNode(), i % 2 == 0 ? typeA : typeB);
        }
        tx.commit();
    }
    // when starting a transaction that creates a relationship of type B and halting it before apply
    Barrier.Control barrier;
    try (OtherThreadExecutor t2 = new OtherThreadExecutor("T2")) {
        barrier = new Barrier.Control();
        Future<Object> t2Future = t2.executeDontWait(() -> {
            try (TransactionImpl tx = (TransactionImpl) db.beginTx()) {
                tx.getNodeById(nodeId).createRelationshipTo(tx.createNode(), typeB);
                tx.commit(barrier::reached);
            }
            return null;
        });
        barrier.awaitUninterruptibly();
        // and another transaction which deletes all relationships of type A, and let it commit
        try (Transaction tx = db.beginTx()) {
            tx.getNodeById(nodeId).getRelationships(typeA).forEach(Relationship::delete);
            tx.commit();
        }
        // and letting the first transaction complete
        barrier.release();
        t2Future.get();
    }
    // then deleting the node should remove the empty group A, even if it's only deleting relationships of type B
    try (Transaction tx = db.beginTx()) {
        Node node = tx.getNodeById(nodeId);
        node.getRelationships().forEach(relationship -> {
            assertThat(relationship.isType(typeB)).isTrue();
            relationship.delete();
        });
        node.delete();
        tx.commit();
    }
}
Also used : Barrier(org.neo4j.test.Barrier) TransactionImpl(org.neo4j.kernel.impl.coreapi.TransactionImpl) OtherThreadExecutor(org.neo4j.test.OtherThreadExecutor) Test(org.junit.jupiter.api.Test)

Example 7 with TransactionImpl

use of org.neo4j.kernel.impl.coreapi.TransactionImpl in project neo4j by neo4j.

the class DeleteNodeWithRelationshipsIT method shouldDeleteDenseNodeIfContainingEmptyGroups.

@Test
void shouldDeleteDenseNodeIfContainingEmptyGroups() throws Exception {
    // given
    long nodeId;
    RelationshipType typeA = RelationshipType.withName("A");
    RelationshipType typeB = RelationshipType.withName("B");
    try (Transaction tx = db.beginTx()) {
        Node node = tx.createNode();
        nodeId = node.getId();
        node.createRelationshipTo(tx.createNode(), typeA);
        for (int i = 0; i < 200; i++) {
            // Type A is created first and will get a lower type ID
            node.createRelationshipTo(tx.createNode(), typeA);
        }
        tx.getNodeById(nodeId).createRelationshipTo(tx.createNode(), typeB);
        tx.commit();
    }
    // when starting a transaction that creates a relationship of type A and halting it before apply
    Barrier.Control barrier;
    try (OtherThreadExecutor t2 = new OtherThreadExecutor("T2")) {
        barrier = new Barrier.Control();
        Future<Object> t2Future = t2.executeDontWait(() -> {
            try (TransactionImpl tx = (TransactionImpl) db.beginTx()) {
                tx.getNodeById(nodeId).createRelationshipTo(tx.createNode(), typeA);
                tx.commit(barrier::reached);
            }
            return null;
        });
        barrier.awaitUninterruptibly();
        // and another transaction which deletes all relationships of type B, and let it commit
        try (Transaction tx = db.beginTx()) {
            tx.getNodeById(nodeId).getRelationships(typeB).forEach(Relationship::delete);
            // This will fail to delete the group B since it can not get exlusive locks
            tx.commit();
        }
        // and letting the first transaction complete
        barrier.release();
        t2Future.get();
        // then removing the remaining relationships of A
        try (Transaction tx = db.beginTx()) {
            tx.getNodeById(nodeId).getRelationships(typeA).forEach(Relationship::delete);
            // this should only delete group A since B has a higher typeId than A (later in group-chain)
            tx.commit();
        }
    }
    // then deleting the node should also remove the empty group B
    try (Transaction tx = db.beginTx()) {
        tx.getNodeById(nodeId).delete();
        assertThatCode(tx::commit).doesNotThrowAnyException();
    }
}
Also used : Barrier(org.neo4j.test.Barrier) TransactionImpl(org.neo4j.kernel.impl.coreapi.TransactionImpl) OtherThreadExecutor(org.neo4j.test.OtherThreadExecutor) Test(org.junit.jupiter.api.Test)

Example 8 with TransactionImpl

use of org.neo4j.kernel.impl.coreapi.TransactionImpl in project neo4j by neo4j.

the class SystemGraphComponent method executeWithFullAccess.

static void executeWithFullAccess(GraphDatabaseService system, ThrowingConsumer<Transaction, Exception> consumer) throws Exception {
    try (TransactionImpl tx = (TransactionImpl) system.beginTx();
        KernelTransaction.Revertable ignore = tx.kernelTransaction().overrideWith(SecurityContext.AUTH_DISABLED)) {
        consumer.accept(tx);
        tx.commit();
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransactionImpl(org.neo4j.kernel.impl.coreapi.TransactionImpl)

Example 9 with TransactionImpl

use of org.neo4j.kernel.impl.coreapi.TransactionImpl in project neo4j by neo4j.

the class TransactionImplTest method shouldShowTransactionTerminatedExceptionAsTransient.

@Test
void shouldShowTransactionTerminatedExceptionAsTransient() throws Exception {
    KernelTransaction kernelTransaction = mock(KernelTransaction.class);
    doReturn(true).when(kernelTransaction).isOpen();
    RuntimeException error = new TransactionTerminatedException(Status.Transaction.Terminated);
    doThrow(error).when(kernelTransaction).close();
    TransactionImpl transaction = new TransactionImpl(tokenHolders, contextFactory, availabilityGuard, engine, kernelTransaction, null, null);
    transaction.commit();
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransactionTerminatedException(org.neo4j.graphdb.TransactionTerminatedException) TransactionImpl(org.neo4j.kernel.impl.coreapi.TransactionImpl) Test(org.junit.jupiter.api.Test)

Example 10 with TransactionImpl

use of org.neo4j.kernel.impl.coreapi.TransactionImpl in project neo4j by neo4j.

the class TransactionImplTest method shouldLetThroughTransientFailureException.

@Test
void shouldLetThroughTransientFailureException() throws Exception {
    // GIVEN
    KernelTransaction kernelTransaction = mock(KernelTransaction.class);
    when(kernelTransaction.isOpen()).thenReturn(true);
    doThrow(new TransientFailureException("Just a random failure") {

        // @Override
        public Status status() {
            return null;
        }
    }).when(kernelTransaction).close();
    TransactionImpl transaction = new TransactionImpl(tokenHolders, contextFactory, availabilityGuard, engine, kernelTransaction, null, null);
    // WHEN
    transaction.commit();
}
Also used : Status(org.neo4j.kernel.api.exceptions.Status) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransientFailureException(org.neo4j.graphdb.TransientFailureException) TransactionImpl(org.neo4j.kernel.impl.coreapi.TransactionImpl) Test(org.junit.jupiter.api.Test)

Aggregations

TransactionImpl (org.neo4j.kernel.impl.coreapi.TransactionImpl)12 Test (org.junit.jupiter.api.Test)8 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)8 Barrier (org.neo4j.test.Barrier)4 OtherThreadExecutor (org.neo4j.test.OtherThreadExecutor)4 Status (org.neo4j.kernel.api.exceptions.Status)2 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)2 MutableLong (org.apache.commons.lang3.mutable.MutableLong)1 TransactionTerminatedException (org.neo4j.graphdb.TransactionTerminatedException)1 TransientFailureException (org.neo4j.graphdb.TransientFailureException)1 TokenRead (org.neo4j.internal.kernel.api.TokenRead)1 TransactionFailureException (org.neo4j.internal.kernel.api.exceptions.TransactionFailureException)1