public class TransactionTemplate extends Object
First instantiate and configure the template using the fluent API methods, and then invoke execute which will begin/commit transactions in a loop for the specified number of times.
By default all exceptions (except Errors and TransactionTerminatedException) cause a retry,
and the monitor does nothing, but these can be overridden with custom behavior.
A bit more narrow and typical exception to retry on is TransientFailureException
,
which aims to represent exceptions that are most likely to succeed after a retry.
Modifier and Type | Class and Description |
---|---|
static interface |
TransactionTemplate.Monitor |
Constructor and Description |
---|
TransactionTemplate()
Creates a template for performing transactions with retry logic.
|
TransactionTemplate(org.neo4j.graphdb.GraphDatabaseService gds,
TransactionTemplate.Monitor monitor,
int retries,
long backoff,
Predicate<Throwable> retryPredicate)
Create a template for performing transaction with retry logic.
|
Modifier and Type | Method and Description |
---|---|
TransactionTemplate |
backoff(long backoff,
TimeUnit unit) |
void |
execute(Consumer<org.neo4j.graphdb.Transaction> txConsumer)
Executes a transaction with retry logic.
|
<T> T |
execute(Function<org.neo4j.graphdb.Transaction,T> txFunction)
Executes a transaction with retry logic returning a result.
|
TransactionTemplate |
monitor(TransactionTemplate.Monitor monitor) |
TransactionTemplate |
retries(int retries) |
TransactionTemplate |
retryOn(Predicate<Throwable> retryPredicate) |
TransactionTemplate |
with(org.neo4j.graphdb.GraphDatabaseService gds) |
public TransactionTemplate()
Default exceptions to retry on is everything except Error
and TransactionTerminatedException
.
public TransactionTemplate(org.neo4j.graphdb.GraphDatabaseService gds, TransactionTemplate.Monitor monitor, int retries, long backoff, Predicate<Throwable> retryPredicate)
gds
- graph database to execute on.monitor
- a monitor that can react to events.retries
- number of retries to try before failing.backoff
- milliseconds to wait between each retry.retryPredicate
- what Throwable
's to retry on.public TransactionTemplate with(org.neo4j.graphdb.GraphDatabaseService gds)
public TransactionTemplate retries(int retries)
public TransactionTemplate backoff(long backoff, TimeUnit unit)
public TransactionTemplate monitor(TransactionTemplate.Monitor monitor)
public TransactionTemplate retryOn(Predicate<Throwable> retryPredicate)
public void execute(Consumer<org.neo4j.graphdb.Transaction> txConsumer)
txConsumer
- a consumer that takes transactions.org.neo4j.graphdb.TransactionFailureException
- if an error occurs other than those specified in retryOn(Predicate)
or if the retry count was exceeded.public <T> T execute(Function<org.neo4j.graphdb.Transaction,T> txFunction) throws org.neo4j.graphdb.TransactionFailureException
T
- type of the result.txFunction
- function taking a transaction and producing a result.T
.org.neo4j.graphdb.TransactionFailureException
- if an error occurs other than those specified in retryOn(Predicate)
or if the retry count was exceeded.Copyright © 2002–2018 The Neo4j Graph Database Project. All rights reserved.