Skip to content

Commit 2912037

Browse files
authored
Backport UnmanagedTransaction stage and completion improvements (#1069)
* Make UnmanagedTransaction return ongoing tx completion stage (#1057) This update ensures that `UnmanagedTransaction` returns existing on-going tx completion stage when a similar request is made. For instance, if it was requested to be rolled back and then requested to be closed, both invocations should get the same on-going stage. In addition, it should not accept conflicting actions, like committing and rolling back at the same time. In addition, it makes sure that cancellation on reactive transaction function results in rollback. * Call close with the appropriate flag to commit or rollback on UnmanagedTransaction where possible to avoid double state acquisition (#1065) * Call close with the appropriate flag to commit or rollback on UnmanagedTransaction where possible to avoid double state acquisition Calling `close` instead of separate `isOpen` and `commitAsync` requires less lock acquisitions and is safer. * Update tests
1 parent 86220ae commit 2912037

File tree

8 files changed

+411
-229
lines changed

8 files changed

+411
-229
lines changed

driver/src/main/java/org/neo4j/driver/internal/async/InternalAsyncSession.java

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private <T> void executeWork(CompletableFuture<T> resultFuture, UnmanagedTransac
146146
Throwable error = Futures.completionExceptionCause( completionError );
147147
if ( error != null )
148148
{
149-
rollbackTxAfterFailedTransactionWork( tx, resultFuture, error );
149+
closeTxAfterFailedTransactionWork( tx, resultFuture, error );
150150
}
151151
else
152152
{
@@ -174,43 +174,33 @@ private <T> CompletionStage<T> safeExecuteWork(UnmanagedTransaction tx, AsyncTra
174174
}
175175
}
176176

177-
private <T> void rollbackTxAfterFailedTransactionWork(UnmanagedTransaction tx, CompletableFuture<T> resultFuture, Throwable error )
177+
private <T> void closeTxAfterFailedTransactionWork( UnmanagedTransaction tx, CompletableFuture<T> resultFuture, Throwable error )
178178
{
179-
if ( tx.isOpen() )
180-
{
181-
tx.rollbackAsync().whenComplete( ( ignore, rollbackError ) -> {
182-
if ( rollbackError != null )
179+
tx.closeAsync().whenComplete(
180+
( ignored, rollbackError ) ->
183181
{
184-
error.addSuppressed( rollbackError );
185-
}
186-
resultFuture.completeExceptionally( error );
187-
} );
188-
}
189-
else
190-
{
191-
resultFuture.completeExceptionally( error );
192-
}
182+
if ( rollbackError != null )
183+
{
184+
error.addSuppressed( rollbackError );
185+
}
186+
resultFuture.completeExceptionally( error );
187+
} );
193188
}
194189

195190
private <T> void closeTxAfterSucceededTransactionWork(UnmanagedTransaction tx, CompletableFuture<T> resultFuture, T result )
196191
{
197-
if ( tx.isOpen() )
198-
{
199-
tx.commitAsync().whenComplete( ( ignore, completionError ) -> {
200-
Throwable commitError = Futures.completionExceptionCause( completionError );
201-
if ( commitError != null )
192+
tx.closeAsync( true ).whenComplete(
193+
( ignored, completionError ) ->
202194
{
203-
resultFuture.completeExceptionally( commitError );
204-
}
205-
else
206-
{
207-
resultFuture.complete( result );
208-
}
209-
} );
210-
}
211-
else
212-
{
213-
resultFuture.complete( result );
214-
}
195+
Throwable commitError = Futures.completionExceptionCause( completionError );
196+
if ( commitError != null )
197+
{
198+
resultFuture.completeExceptionally( commitError );
199+
}
200+
else
201+
{
202+
resultFuture.complete( result );
203+
}
204+
} );
215205
}
216206
}

0 commit comments

Comments
 (0)