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));
}
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
}
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));
}
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);
}
}
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);
}
}
Aggregations