Search in sources :

Example 1 with MultipleAttemptsExecutor

use of uk.ac.ebi.utils.runcontrol.MultipleAttemptsExecutor in project rdf2neo by Rothamsted.

the class Neo4jDataManager method processCypherMatches.

public void processCypherMatches(Consumer<Record> action, String cypher, Object... keyVals) {
    if (log.isTraceEnabled())
        log.trace("Cypher: {} params: {}", cypher, ArrayUtils.toString(keyVals));
    // Re-attempt a couple of times, in case of exceptions due to deadlocks over locking nodes.
    MultipleAttemptsExecutor attempter = new MultipleAttemptsExecutor(TransientException.class, DatabaseException.class, ServiceUnavailableException.class);
    attempter.setMaxAttempts(10);
    attempter.setMinPauseTime(30 * 1000);
    attempter.setMaxPauseTime(3 * 60 * 1000);
    attempter.execute(() -> {
        try (Session session = this.neo4jDriver.session()) {
            StatementResult cursor = session.run(cypher, parameters(keyVals));
            cursor.forEachRemaining(action);
        }
    });
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) MultipleAttemptsExecutor(uk.ac.ebi.utils.runcontrol.MultipleAttemptsExecutor) Session(org.neo4j.driver.v1.Session)

Example 2 with MultipleAttemptsExecutor

use of uk.ac.ebi.utils.runcontrol.MultipleAttemptsExecutor in project rdf2neo by Rothamsted.

the class Neo4jDataManager method runCypher.

/**
 * <p>Runs a Cypher gommands against the current {@link #getNeo4jDriver()}.</p>
 *
 * <p>The keyVals parameter is passed to {@link Values#parameters(Object...)}.</p>
 *
 * <p>The command is wrapped into a single transaction, which is committed within the method.</p>
 *
 * <p>Because parallelism sometimes raises exceptions about race conditions, we use {@link MultipleAttemptsExecutor}
 * to re-attempt the command execution a couple of times, after such exceptions.</p>
 */
public void runCypher(String cypher, Object... keyVals) {
    if (log.isTraceEnabled())
        log.trace("Cypher: {} params: {}", cypher, ArrayUtils.toString(keyVals));
    // Re-attempt a couple of times, in case of exceptions due to deadlocks over locking nodes.
    MultipleAttemptsExecutor attempter = new MultipleAttemptsExecutor(TransientException.class, DatabaseException.class, ServiceUnavailableException.class);
    attempter.setMaxAttempts(10);
    attempter.setMinPauseTime(30 * 1000);
    attempter.setMaxPauseTime(3 * 60 * 1000);
    attempter.execute(() -> {
        try (Session session = this.neo4jDriver.session()) {
            session.run(cypher, parameters(keyVals));
        }
    });
}
Also used : MultipleAttemptsExecutor(uk.ac.ebi.utils.runcontrol.MultipleAttemptsExecutor) Session(org.neo4j.driver.v1.Session)

Aggregations

Session (org.neo4j.driver.v1.Session)2 MultipleAttemptsExecutor (uk.ac.ebi.utils.runcontrol.MultipleAttemptsExecutor)2 StatementResult (org.neo4j.driver.v1.StatementResult)1