Search in sources :

Example 1 with TransactionFailureException

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

the class RecoveryIT method recoveryShouldFixPartiallyAppliedSchemaIndexUpdates.

@Test(timeout = 60_000)
public void recoveryShouldFixPartiallyAppliedSchemaIndexUpdates() {
    Label label = Label.label("Foo");
    String property = "Bar";
    // cause failure during 'relationship.delete()' command application
    ClassGuardedAdversary adversary = new ClassGuardedAdversary(new CountingAdversary(1, true), Command.RelationshipCommand.class);
    adversary.disable();
    File storeDir = directory.graphDbDir();
    GraphDatabaseService db = AdversarialPageCacheGraphDatabaseFactory.create(fileSystemRule.get(), adversary).newEmbeddedDatabaseBuilder(storeDir).newGraphDatabase();
    try {
        try (Transaction tx = db.beginTx()) {
            db.schema().constraintFor(label).assertPropertyIsUnique(property).create();
            tx.success();
        }
        long relationshipId = createRelationship(db);
        TransactionFailureException txFailure = null;
        try (Transaction tx = db.beginTx()) {
            Node node = db.createNode(label);
            node.setProperty(property, "B");
            // this should fail because of the adversary
            db.getRelationshipById(relationshipId).delete();
            tx.success();
            adversary.enable();
        } catch (TransactionFailureException e) {
            txFailure = e;
        }
        assertNotNull(txFailure);
        adversary.disable();
        // heal the db so it is possible to inspect the data
        healthOf(db).healed();
        // now we can observe partially committed state: node is in the index and relationship still present
        try (Transaction tx = db.beginTx()) {
            assertNotNull(findNode(db, label, property, "B"));
            assertNotNull(db.getRelationshipById(relationshipId));
            tx.success();
        }
        // panic the db again to force recovery on the next startup
        healthOf(db).panic(txFailure.getCause());
        // restart the database, now with regular page cache
        db.shutdown();
        db = startDatabase(storeDir);
        // now we observe correct state: node is in the index and relationship is removed
        try (Transaction tx = db.beginTx()) {
            assertNotNull(findNode(db, label, property, "B"));
            assertRelationshipNotExist(db, relationshipId);
            tx.success();
        }
    } finally {
        db.shutdown();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) CountingAdversary(org.neo4j.adversaries.CountingAdversary) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) ClassGuardedAdversary(org.neo4j.adversaries.ClassGuardedAdversary) Transaction(org.neo4j.graphdb.Transaction) Command(org.neo4j.kernel.impl.transaction.command.Command) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) File(java.io.File) Test(org.junit.Test)

Example 2 with TransactionFailureException

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

the class ClusterPartitionIT method ensureInstanceIsReadOnlyInPendingState.

/*
     * This method must be called on an instance that has had addSomeData() called on it.
     */
private void ensureInstanceIsReadOnlyInPendingState(HighlyAvailableGraphDatabase instance) {
    assertEquals(PENDING, instance.getInstanceState());
    tx(instance, retryACoupleOfTimesOn(TRANSIENT_ERRORS), db -> assertEquals(testPropValue, instance.getNodeById(testNodeId).getProperty(testPropKey)));
    try (Transaction ignored = instance.beginTx()) {
        instance.getNodeById(testNodeId).delete();
        fail("Should not be able to do write transactions when detached");
    } catch (TransientDatabaseFailureException | TransactionFailureException expected) {
    // expected
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) TransientDatabaseFailureException(org.neo4j.graphdb.TransientDatabaseFailureException)

Example 3 with TransactionFailureException

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

the class KernelIT method shouldNotBeAbleToCommitIfFailedTransactionContext.

@Test
public void shouldNotBeAbleToCommitIfFailedTransactionContext() throws Exception {
    // WHEN
    Node node = null;
    int labelId = -1;
    TransactionFailureException expectedException = null;
    try (Transaction transaction = db.beginTx()) {
        Statement statement = statementContextSupplier.get();
        node = db.createNode();
        labelId = statement.tokenWriteOperations().labelGetOrCreateForName("labello");
        statement.dataWriteOperations().nodeAddLabel(node.getId(), labelId);
        statement.close();
        transaction.failure();
        transaction.success();
    } catch (TransactionFailureException e) {
        expectedException = e;
    } finally {
        Assert.assertNotNull("Should have failed", expectedException);
    }
    // THEN
    try (Transaction tx = db.beginTx()) {
        Statement statement = statementContextSupplier.get();
        try {
            statement.readOperations().nodeHasLabel(node.getId(), labelId);
            fail("should have thrown exception");
        } catch (EntityNotFoundException e) {
        // Yay!
        }
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Statement(org.neo4j.kernel.api.Statement) Node(org.neo4j.graphdb.Node) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException) Test(org.junit.Test)

Example 4 with TransactionFailureException

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

the class TransactionTemplate method execute.

public <T> T execute(Function<Transaction, T> txFunction) throws TransactionFailureException {
    Throwable txEx = null;
    for (int i = 0; i < retries; i++) {
        try (Transaction tx = gds.beginTx()) {
            T result = txFunction.apply(tx);
            tx.success();
            return result;
        } catch (Throwable ex) {
            monitor.failure(ex);
            txEx = ex;
            if (!retryPredicate.test(ex)) {
                break;
            }
        }
        if (i < retries - 1) {
            try {
                Thread.sleep(backoff);
            } catch (InterruptedException e) {
                throw new TransactionFailureException("Interrupted", e);
            }
            monitor.retrying();
        }
    }
    if (txEx instanceof TransactionFailureException) {
        throw (TransactionFailureException) txEx;
    } else if (txEx instanceof Error) {
        throw (Error) txEx;
    } else if (txEx instanceof RuntimeException) {
        throw (RuntimeException) txEx;
    } else {
        throw new TransactionFailureException("Failed", txEx);
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction)

Example 5 with TransactionFailureException

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

the class KernelTransactions method newInstance.

public KernelTransaction newInstance(KernelTransaction.Type type, SecurityContext securityContext, long timeout) {
    assertCurrentThreadIsNotBlockingNewTransactions();
    SecurityContext frozenSecurityContext = securityContext.freeze();
    try {
        while (!newTransactionsLock.readLock().tryLock(1, TimeUnit.SECONDS)) {
            assertRunning();
        }
        try {
            assertRunning();
            TransactionId lastCommittedTransaction = transactionIdStore.getLastCommittedTransaction();
            KernelTransactionImplementation tx = localTxPool.acquire();
            StatementLocks statementLocks = statementLocksFactory.newInstance();
            tx.initialize(lastCommittedTransaction.transactionId(), lastCommittedTransaction.commitTimestamp(), statementLocks, type, frozenSecurityContext, timeout);
            return tx;
        } finally {
            newTransactionsLock.readLock().unlock();
        }
    } catch (InterruptedException ie) {
        Thread.interrupted();
        throw new TransactionFailureException("Fail to start new transaction.", ie);
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) StatementLocks(org.neo4j.kernel.impl.locking.StatementLocks) SecurityContext(org.neo4j.kernel.api.security.SecurityContext) TransactionId(org.neo4j.kernel.impl.store.TransactionId)

Aggregations

TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)56 Transaction (org.neo4j.graphdb.Transaction)16 NotFoundException (org.neo4j.graphdb.NotFoundException)9 Test (org.junit.Test)7 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)7 Node (org.neo4j.graphdb.Node)7 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)7 LinkedList (java.util.LinkedList)6 SystemException (javax.transaction.SystemException)6 XAException (javax.transaction.xa.XAException)6 XAResource (javax.transaction.xa.XAResource)6 KernelException (org.neo4j.exceptions.KernelException)6 EntityNotFoundException (org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException)6 InvalidTransactionTypeKernelException (org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException)6 PropertyKeyIdNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)6 IllegalTokenNameException (org.neo4j.internal.kernel.api.exceptions.schema.IllegalTokenNameException)6 TokenCapacityExceededKernelException (org.neo4j.internal.kernel.api.exceptions.schema.TokenCapacityExceededKernelException)6 XaDataSource (org.neo4j.kernel.impl.transaction.xaframework.XaDataSource)6 XaResource (org.neo4j.kernel.impl.transaction.xaframework.XaResource)6 IOException (java.io.IOException)5