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