Search in sources :

Example 31 with TransactionFailureException

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

the class DeadlockDocTest method transactionWithRetry.

private Object transactionWithRetry(GraphDatabaseService databaseService) {
    // tag::retry[]
    Throwable txEx = null;
    int RETRIES = 5;
    int BACKOFF = 3000;
    for (int i = 0; i < RETRIES; i++) {
        try (Transaction tx = databaseService.beginTx()) {
            Object result = doStuff(tx);
            tx.commit();
            return result;
        } catch (Throwable ex) {
            txEx = ex;
            // Add whatever exceptions to retry on here
            if (!(ex instanceof DeadlockDetectedException)) {
                break;
            }
        }
        // Wait so that we don't immediately get into the same deadlock
        if (i < RETRIES - 1) {
            try {
                Thread.sleep(BACKOFF);
            } catch (InterruptedException e) {
                throw new TransactionFailureException("Interrupted", e);
            }
        }
    }
    if (txEx instanceof TransactionFailureException) {
        throw ((TransactionFailureException) txEx);
    } else if (txEx instanceof Error) {
        throw ((Error) txEx);
    } else {
        throw ((RuntimeException) txEx);
    }
// end::retry[]
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException)

Example 32 with TransactionFailureException

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

the class PropertyConstraintsStressIT method performInserts.

/**
     * Inserts a bunch of new nodes with the label and property key currently set in the fields in this class, where
     * running this method twice will insert nodes with duplicate property values, assuming property key or label has
     * not changed.
     */
private WorkerCommand<Object, Integer> performInserts(final HighlyAvailableGraphDatabase slave, final boolean constraintCompliant) {
    return new WorkerCommand<Object, Integer>() {

        @Override
        public Integer doWork(Object state) throws Exception {
            int i = 0;
            try {
                for (; i < 100; i++) {
                    try (Transaction tx = slave.beginTx()) {
                        constraintOps.createEntity(slave, labelOrRelType, property, "value" + i, constraintCompliant);
                        tx.success();
                    }
                }
            } catch (TransactionFailureException | TransientTransactionFailureException e) {
            // Swallowed on purpose, we except it to fail sometimes due to either
            //  - constraint violation on master
            //  - concurrent schema operation on master
            } catch (ConstraintViolationException e) {
            // Constraint violation detected on slave while building transaction
            } catch (ComException e) {
            // Happens sometimes, cause:
            // - The lock session requested to start is already in use.
            //   Please retry your request in a few seconds.
            }
            return i;
        }
    };
}
Also used : ComException(org.neo4j.com.ComException) WorkerCommand(org.neo4j.test.OtherThreadExecutor.WorkerCommand) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) TransientTransactionFailureException(org.neo4j.graphdb.TransientTransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) TransientTransactionFailureException(org.neo4j.graphdb.TransientTransactionFailureException)

Example 33 with TransactionFailureException

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

the class WorkLoad method doWork.

@Override
protected void doWork() {
    GraphDatabaseService db = dbRef.get();
    try (Transaction tx = db.beginTx()) {
        Node node = db.createNode(label);
        for (int i = 1; i <= 8; i++) {
            node.setProperty(prop(i), "let's add some data here so the transaction logs rotate more often...");
        }
        tx.success();
    } catch (DatabaseShutdownException | TransactionFailureException e) {
    // whatever let's go on with the workload
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) DatabaseShutdownException(org.neo4j.graphdb.DatabaseShutdownException)

Example 34 with TransactionFailureException

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

the class DeferringLocksIT method removeNodeChangeNodeProperty.

@Test(timeout = TEST_TIMEOUT)
public void removeNodeChangeNodeProperty() throws Exception {
    // GIVEN
    final Barrier.Control barrier = new Barrier.Control();
    final long nodeId;
    try (Transaction tx = db.beginTx()) {
        Node node = db.createNode();
        nodeId = node.getId();
        node.setProperty(PROPERTY_KEY, VALUE_1);
        tx.success();
    }
    // WHEN
    Future<Void> future = t2.execute(new WorkerCommand<Void, Void>() {

        @Override
        public Void doWork(Void state) throws Exception {
            try (Transaction tx = db.beginTx()) {
                db.getNodeById(nodeId).delete();
                tx.success();
                barrier.reached();
            }
            return null;
        }
    });
    try {
        try (Transaction tx = db.beginTx()) {
            barrier.await();
            db.getNodeById(nodeId).setProperty(PROPERTY_KEY, VALUE_2);
            tx.success();
            barrier.release();
        }
    } catch (TransactionFailureException e) {
        // Node was already deleted, fine.
        assertThat(e.getCause(), instanceOf(InvalidRecordException.class));
    }
    future.get();
    try (Transaction tx = db.beginTx()) {
        try {
            db.getNodeById(nodeId);
            assertEquals(VALUE_2, db.getNodeById(nodeId).getProperty(PROPERTY_KEY, VALUE_2));
        } catch (NotFoundException e) {
        // Fine, its gone
        }
        tx.success();
    }
}
Also used : Node(org.neo4j.graphdb.Node) NotFoundException(org.neo4j.graphdb.NotFoundException) Barrier(org.neo4j.test.Barrier) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) NotFoundException(org.neo4j.graphdb.NotFoundException) InvalidRecordException(org.neo4j.kernel.impl.store.InvalidRecordException) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.Test)

Example 35 with TransactionFailureException

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

the class DbRepresentation method of.

public static DbRepresentation of(GraphDatabaseService db, boolean includeIndexes) {
    int retryCount = 5;
    while (true) {
        try (Transaction ignore = db.beginTx()) {
            DbRepresentation result = new DbRepresentation();
            for (Node node : db.getAllNodes()) {
                NodeRep nodeRep = new NodeRep(db, node, includeIndexes);
                result.nodes.put(node.getId(), nodeRep);
                result.highestNodeId = Math.max(node.getId(), result.highestNodeId);
                result.highestRelationshipId = Math.max(nodeRep.highestRelationshipId, result.highestRelationshipId);
            }
            return result;
        } catch (TransactionFailureException e) {
            if (retryCount-- < 0) {
                throw e;
            }
        }
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node)

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