Search in sources :

Example 6 with TransactionFailureException

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

the class BoltStateMachineTest method testRollbackError.

@Test
public void testRollbackError() throws Throwable {
    // Given
    BoltStateMachine machine = newMachine(READY);
    // Given there is a running transaction
    machine.run("BEGIN", EMPTY_PARAMS, nullResponseHandler());
    machine.discardAll(nullResponseHandler());
    // And given that transaction will fail to roll back
    TransactionStateMachine txMachine = (TransactionStateMachine) machine.ctx.statementProcessor;
    when(txMachine.ctx.currentTransaction.isOpen()).thenReturn(true);
    doThrow(new TransactionFailureException("No Mr. Bond, I expect you to die.")).when(txMachine.ctx.currentTransaction).close();
    // When
    machine.run("ROLLBACK", EMPTY_PARAMS, nullResponseHandler());
    machine.discardAll(nullResponseHandler());
    // Then
    assertThat(machine, inState(FAILED));
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Test(org.junit.Test)

Example 7 with TransactionFailureException

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

the class DeadlockDocTest method transactionWithRetry.

private Object transactionWithRetry() {
    GraphDatabaseService graphDatabaseService = rule.getGraphDatabaseAPI();
    // START SNIPPET: retry
    Throwable txEx = null;
    int RETRIES = 5;
    int BACKOFF = 3000;
    for (int i = 0; i < RETRIES; i++) {
        try (Transaction tx = graphDatabaseService.beginTx()) {
            Object result = doStuff(tx);
            tx.success();
            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 if (txEx instanceof RuntimeException) {
        throw ((RuntimeException) txEx);
    } else {
        throw new TransactionFailureException("Failed", txEx);
    }
// END SNIPPET: retry
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException)

Example 8 with TransactionFailureException

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

the class BoltStateMachineV4Test method testRollbackError.

@Test
void testRollbackError() throws Throwable {
    // Given
    BoltStateMachine machine = init(newMachine());
    // Given there is a running transaction
    machine.process(BoltV4Messages.begin(), nullResponseHandler());
    // And given that transaction will fail to roll back
    TransactionStateMachine txMachine = txStateMachine(machine);
    doThrow(new TransactionFailureException("No Mr. Bond, I expect you to die.")).when(txMachine.ctx.currentTransaction).rollback();
    // When
    machine.process(BoltV4Messages.rollback(), nullResponseHandler());
    // Then
    assertThat(machine).satisfies(inState(FailedState.class));
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) FailedState(org.neo4j.bolt.v4.runtime.FailedState) Test(org.junit.jupiter.api.Test)

Example 9 with TransactionFailureException

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

the class KernelTransactions method newInstance.

public KernelTransaction newInstance(KernelTransaction.Type type, LoginContext loginContext, ClientConnectionInfo clientInfo, long timeout) {
    assertCurrentThreadIsNotBlockingNewTransactions();
    SecurityContext securityContext = loginContext.authorize(tokenHoldersIdLookup, namedDatabaseId.name(), securityLog);
    try {
        while (!newTransactionsLock.readLock().tryLock(1, TimeUnit.SECONDS)) {
            assertRunning();
        }
        try {
            assertRunning();
            TransactionId lastCommittedTransaction = transactionIdStore.getLastCommittedTransaction();
            KernelTransactionImplementation tx = localTxPool.acquire();
            Locks.Client lockClient = locks.newClient();
            tx.initialize(lastCommittedTransaction.transactionId(), lastCommittedTransaction.commitTimestamp(), lockClient, type, securityContext, timeout, userTransactionIdCounter.incrementAndGet(), clientInfo);
            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) SecurityContext(org.neo4j.internal.kernel.api.security.SecurityContext) Locks(org.neo4j.kernel.impl.locking.Locks) TransactionId(org.neo4j.storageengine.api.TransactionId)

Example 10 with TransactionFailureException

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

the class NodeEntity method removeProperty.

@Override
public Object removeProperty(String key) throws NotFoundException {
    KernelTransaction transaction = internalTransaction.kernelTransaction();
    int propertyKeyId;
    try {
        propertyKeyId = transaction.tokenWrite().propertyKeyGetOrCreateForName(key);
    } catch (IllegalTokenNameException e) {
        throw new IllegalArgumentException(format("Invalid property key '%s'.", key), e);
    } catch (KernelException e) {
        throw new TransactionFailureException("Unknown error trying to get property key token", e);
    }
    try {
        return transaction.dataWrite().nodeRemoveProperty(nodeId, propertyKeyId).asObjectCopy();
    } catch (EntityNotFoundException e) {
        throw new NotFoundException(e);
    } catch (InvalidTransactionTypeKernelException e) {
        throw new ConstraintViolationException(e.getMessage(), e);
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) InvalidTransactionTypeKernelException(org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException) NotFoundException(org.neo4j.graphdb.NotFoundException) EntityNotFoundException(org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) EntityNotFoundException(org.neo4j.internal.kernel.api.exceptions.EntityNotFoundException) IllegalTokenNameException(org.neo4j.internal.kernel.api.exceptions.schema.IllegalTokenNameException) PropertyKeyIdNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException) InvalidTransactionTypeKernelException(org.neo4j.internal.kernel.api.exceptions.InvalidTransactionTypeKernelException) TokenCapacityExceededKernelException(org.neo4j.internal.kernel.api.exceptions.schema.TokenCapacityExceededKernelException) LabelNotFoundKernelException(org.neo4j.internal.kernel.api.exceptions.LabelNotFoundKernelException) KernelException(org.neo4j.exceptions.KernelException)

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