Sometimes you may want to terminate (abort) a long running transaction from another thread.
Tip
|
The source code used in this example is found here: TerminateTransactions.java |
To begin with, we start the database server:
component=neo4j-examples source=org/neo4j/examples/TerminateTransactions.java classifier=sources tag=startDb
Now we start creating an infinite binary tree of nodes in the database, as an example of a long running transaction.
component=neo4j-examples source=org/neo4j/examples/TerminateTransactions.java classifier=sources tag=mkTree
After waiting for some time, we decide to terminate the transaction. This is done from a separate thread.
component=neo4j-examples source=org/neo4j/examples/TerminateTransactions.java classifier=sources tag=terminateTx
Running this will execute the long running transaction for about one second and prints the maximum depth of the tree that was created before the transaction was terminated. No changes are actually made to the data — because the transaction has been terminated, the end result is as if no operations were performed.
Example output
Finally, let’s shut down the database again.
component=neo4j-examples source=org/neo4j/examples/TerminateTransactions.java classifier=sources tag=shutdownDb