Skip to content

Commit

Permalink
Handle nulls returned from async tx functions
Browse files Browse the repository at this point in the history
Previously code failed with NPE when provided async transaction
function returned null.

This commit fixes the problem by wrapping null in a completed future.
  • Loading branch information
lutovich committed Dec 15, 2017
1 parent f2f55c2 commit 992ef0e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,10 @@ private <T> CompletionStage<T> safeExecuteWork( ExplicitTransaction tx, Transact
// sync failure will result in an exception being thrown
try
{
return work.execute( tx );
CompletionStage<T> result = work.execute( tx );

// protect from given transaction function returning null
return result == null ? completedFuture( null ) : result;
}
catch ( Throwable workError )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,16 @@ public void shouldAllowAccessingRecordsAfterSessionClosed()
}
}

@Test
public void shouldAllowReturningNullFromAsyncTransactionFunction()
{
CompletionStage<Object> readResult = session.readTransactionAsync( tx -> null );
assertNull( await( readResult ) );

CompletionStage<Object> writeResult = session.writeTransactionAsync( tx -> null );
assertNull( await( writeResult ) );
}

private Future<List<CompletionStage<Record>>> runNestedQueries( StatementResultCursor inputCursor )
{
CompletableFuture<List<CompletionStage<Record>>> resultFuture = new CompletableFuture<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,16 @@ public void shouldAllowLongRunningQueryWithConnectTimeout() throws Exception
}
}

@Test
public void shouldAllowReturningNullFromTransactionFunction()
{
try ( Session session = neo4j.driver().session() )
{
assertNull( session.readTransaction( tx -> null ) );
assertNull( session.writeTransaction( tx -> null ) );
}
}

private void assumeServerIs31OrLater()
{
ServerVersion serverVersion = ServerVersion.version( neo4j.driver() );
Expand Down

0 comments on commit 992ef0e

Please sign in to comment.