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