I suspect that you are not exhausting the Iterator
from the ExecutionResult
returned by ExecutionEngine#execute()
. If you do so, the test will finish quickly without needing to wrap execute()
in a transaction.
Here's what I think is going on, although I haven't reproduced this in a debugger.
ExecutionEngine#execute()
creates its own transaction, so it shouldn't be necessary to create your own. However all transactions must be closed. In this case the transaction state is handled by the ExecutionResult
its methods as documented here. If you partially consume the results Iterator
then the transaction is left in an open state.
When you call GraphDatabaseService#shutdown()
, it tries to accommodate open transactions by waiting for them to complete before forcing them closed. The stall you see in your test is probably that timeout happening. Of course in this case it is not possible for the transaction to close during the wait because it is held by the same thread that is shutting the database down.
Because transactions are tied to threads it would be possible, in principle, for the GraphDatabaseService
to detect this case and close the transaction without waiting. However it's probably rare in production code that the same thread is executing transactions and controlling the database, so the extra complexity would not be justified.
The simplest way to handle this is to always ensure that you exhaust the results iterators that are returned by the ExecutionEngine
.