Skip to content

Commit 1d64ca0

Browse files
committed
Updated connection handling
1 parent 9720006 commit 1d64ca0

File tree

9 files changed

+54
-73
lines changed

9 files changed

+54
-73
lines changed

driver/src/main/java/org/neo4j/driver/internal/FailableCursor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
public interface FailableCursor
2424
{
2525
/**
26-
* Discarding all unconsumed records and returning failure if there is any to run and/or pulls.
26+
* Discarding all unconsumed records and returning failure if there is any pull errors.
2727
*/
2828
CompletionStage<Throwable> discardAllFailureAsync();
2929

3030
/**
31-
* Pulling all unconsumed records into memory and returning failure if there is any to run and/or pulls.
31+
* Pulling all unconsumed records into memory and returning failure if there is any pull errors.
3232
*/
3333
CompletionStage<Throwable> pullAllFailureAsync();
3434
}

driver/src/main/java/org/neo4j/driver/internal/cursor/AsyncResultCursorImpl.java

+4-27
Original file line numberDiff line numberDiff line change
@@ -116,38 +116,15 @@ public <T> CompletionStage<List<T>> listAsync( Function<Record,T> mapFunction )
116116
@Override
117117
public CompletionStage<Throwable> discardAllFailureAsync()
118118
{
119-
return consumeAsync().handle(
120-
( summary, consumeError ) ->
121-
{
122-
if ( runError != null )
123-
{
124-
if ( consumeError != null && runError != consumeError )
125-
{
126-
runError.addSuppressed( consumeError );
127-
}
128-
return runError;
129-
}
130-
return consumeError;
131-
} );
119+
// let handler process response and ignore error when runError exists as it would already be reported to user
120+
return consumeAsync().handle( ( summary, error ) -> runError != null ? null : error );
132121
}
133122

134123
@Override
135124
public CompletionStage<Throwable> pullAllFailureAsync()
136125
{
137-
return pullAllHandler.pullAllFailureAsync().handle(
138-
( ignored, pullAllError ) ->
139-
{
140-
if ( runError != null )
141-
{
142-
if ( pullAllError != null && runError != pullAllError )
143-
{
144-
runError.addSuppressed( pullAllError );
145-
}
146-
return runError;
147-
}
148-
return pullAllError;
149-
}
150-
);
126+
// let handler process response and ignore error when runError exists as it would already be reported to user
127+
return pullAllHandler.pullAllFailureAsync().thenApply( error -> runError != null ? null : error );
151128
}
152129

153130
private void internalForEachAsync( Consumer<Record> action, CompletableFuture<Void> resultFuture )

driver/src/main/java/org/neo4j/driver/internal/handlers/RunResponseHandler.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,9 @@ public void onFailure( Throwable error )
6666
{
6767
tx.markTerminated( error );
6868
}
69-
else
69+
else if ( error instanceof AuthorizationExpiredException )
7070
{
71-
if ( error instanceof AuthorizationExpiredException )
72-
{
73-
connection.terminateAndRelease( AuthorizationExpiredException.DESCRIPTION );
74-
}
75-
else
76-
{
77-
connection.release();
78-
}
71+
connection.terminateAndRelease( AuthorizationExpiredException.DESCRIPTION );
7972
}
8073
runFuture.completeExceptionally( error );
8174
}

driver/src/test/java/org/neo4j/driver/integration/ConnectionHandlingIT.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ void connectionUsedForTransactionReturnedToThePoolWhenTransactionFailsToCommitte
278278
void connectionUsedForSessionRunReturnedToThePoolWhenSessionClose()
279279
{
280280
Session session = driver.session();
281-
Result result = createNodes( 12, session );
281+
createNodes( 12, session );
282282

283283
Connection connection1 = connectionPool.lastAcquiredConnectionSpy;
284284
verify( connection1, never() ).release();

driver/src/test/java/org/neo4j/driver/internal/messaging/v3/BoltProtocolV3Test.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import static org.junit.jupiter.api.Assertions.assertFalse;
7878
import static org.junit.jupiter.api.Assertions.assertNotNull;
7979
import static org.junit.jupiter.api.Assertions.assertNull;
80+
import static org.junit.jupiter.api.Assertions.assertSame;
8081
import static org.junit.jupiter.api.Assertions.assertThrows;
8182
import static org.junit.jupiter.api.Assertions.assertTrue;
8283
import static org.mockito.ArgumentMatchers.any;
@@ -389,6 +390,7 @@ protected void testRunInUnmanagedTransactionAndWaitForRunResponse( boolean succe
389390

390391
ResponseHandler runResponseHandler = verifyRunInvoked( connection, false, InternalBookmark.empty(), TransactionConfig.empty(), mode ).runHandler;
391392
assertFalse( cursorFuture.isDone() );
393+
Throwable error = new RuntimeException();
392394

393395
if ( success )
394396
{
@@ -397,18 +399,18 @@ protected void testRunInUnmanagedTransactionAndWaitForRunResponse( boolean succe
397399
else
398400
{
399401
// When responded with a failure
400-
runResponseHandler.onFailure( new RuntimeException() );
402+
runResponseHandler.onFailure( error );
401403
}
402404

403405
// Then
404406
assertTrue( cursorFuture.isDone() );
405407
if ( success )
406408
{
407-
assertNotNull( cursorFuture.get() );
409+
assertFalse( cursorFuture.get().runError().isPresent() );
408410
}
409411
else
410412
{
411-
assertThrows( RuntimeException.class, () -> await( cursorFuture ) );
413+
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
412414
}
413415
}
414416

@@ -474,11 +476,12 @@ protected void testFailedRunInAutoCommitTxWithWaitingForResponse( Bookmark bookm
474476
assertFalse( cursorFuture.isDone() );
475477

476478
ResponseHandler runResponseHandler = verifyRunInvoked( connection, true, bookmark, config, mode ).runHandler;
477-
runResponseHandler.onFailure( new RuntimeException() );
479+
Throwable error = new RuntimeException();
480+
runResponseHandler.onFailure( error );
478481
assertEquals( bookmark, bookmarkHolder.getBookmark() );
479482

480-
assertTrue( cursorFuture.isCompletedExceptionally() );
481-
assertThrows( RuntimeException.class, () -> await( cursorFuture ) );
483+
assertTrue( cursorFuture.isDone() );
484+
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
482485
}
483486

484487
private static InternalAuthToken dummyAuthToken()

driver/src/test/java/org/neo4j/driver/internal/messaging/v4/BoltProtocolV4Test.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
import static org.junit.jupiter.api.Assertions.assertFalse;
7979
import static org.junit.jupiter.api.Assertions.assertNotNull;
8080
import static org.junit.jupiter.api.Assertions.assertNull;
81-
import static org.junit.jupiter.api.Assertions.assertThrows;
81+
import static org.junit.jupiter.api.Assertions.assertSame;
8282
import static org.junit.jupiter.api.Assertions.assertTrue;
8383
import static org.mockito.ArgumentMatchers.any;
8484
import static org.mockito.ArgumentMatchers.eq;
@@ -381,12 +381,13 @@ protected void testFailedRunInAutoCommitTxWithWaitingForResponse( Bookmark bookm
381381
assertFalse( cursorFuture.isDone() );
382382

383383
// When I response to Run message with a failure
384-
runHandler.onFailure( new RuntimeException() );
384+
Throwable error = new RuntimeException();
385+
runHandler.onFailure( error );
385386

386387
// Then
387388
assertEquals( bookmark, bookmarkHolder.getBookmark() );
388-
assertTrue( cursorFuture.isCompletedExceptionally() );
389-
assertThrows( RuntimeException.class, () -> await( cursorFuture ) );
389+
assertTrue( cursorFuture.isDone() );
390+
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
390391
}
391392

392393
protected void testSuccessfulRunInAutoCommitTxWithWaitingForResponse( Bookmark bookmark, TransactionConfig config, AccessMode mode ) throws Exception
@@ -424,6 +425,7 @@ protected void testRunInUnmanagedTransactionAndWaitForRunResponse( boolean succe
424425

425426
ResponseHandler runHandler = verifyTxRunInvoked( connection );
426427
assertFalse( cursorFuture.isDone() );
428+
Throwable error = new RuntimeException();
427429

428430
if ( success )
429431
{
@@ -432,18 +434,18 @@ protected void testRunInUnmanagedTransactionAndWaitForRunResponse( boolean succe
432434
else
433435
{
434436
// When responded with a failure
435-
runHandler.onFailure( new RuntimeException() );
437+
runHandler.onFailure( error );
436438
}
437439

438440
// Then
439441
assertTrue( cursorFuture.isDone() );
440442
if ( success )
441443
{
442-
assertNotNull( cursorFuture.get() );
444+
assertFalse( cursorFuture.get().runError().isPresent() );
443445
}
444446
else
445447
{
446-
assertThrows( RuntimeException.class, () -> await( cursorFuture ) );
448+
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
447449
}
448450
}
449451

driver/src/test/java/org/neo4j/driver/internal/messaging/v41/BoltProtocolV41Test.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
import static org.junit.jupiter.api.Assertions.assertFalse;
8080
import static org.junit.jupiter.api.Assertions.assertNotNull;
8181
import static org.junit.jupiter.api.Assertions.assertNull;
82-
import static org.junit.jupiter.api.Assertions.assertThrows;
82+
import static org.junit.jupiter.api.Assertions.assertSame;
8383
import static org.junit.jupiter.api.Assertions.assertTrue;
8484
import static org.mockito.ArgumentMatchers.any;
8585
import static org.mockito.ArgumentMatchers.eq;
@@ -376,12 +376,13 @@ private void testFailedRunInAutoCommitTxWithWaitingForResponse( Bookmark bookmar
376376
assertFalse( cursorFuture.isDone() );
377377

378378
// When I response to Run message with a failure
379-
runHandler.onFailure( new RuntimeException() );
379+
Throwable error = new RuntimeException();
380+
runHandler.onFailure( error );
380381

381382
// Then
382383
assertEquals( bookmark, bookmarkHolder.getBookmark() );
383-
assertTrue( cursorFuture.isCompletedExceptionally() );
384-
assertThrows( RuntimeException.class, () -> await( cursorFuture ) );
384+
assertTrue( cursorFuture.isDone() );
385+
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
385386
}
386387

387388
private void testSuccessfulRunInAutoCommitTxWithWaitingForResponse( Bookmark bookmark, TransactionConfig config, AccessMode mode ) throws Exception
@@ -419,6 +420,7 @@ private void testRunInUnmanagedTransactionAndWaitForRunResponse( boolean success
419420

420421
ResponseHandler runHandler = verifyTxRunInvoked( connection );
421422
assertFalse( cursorFuture.isDone() );
423+
Throwable error = new RuntimeException();
422424

423425
if ( success )
424426
{
@@ -427,18 +429,18 @@ private void testRunInUnmanagedTransactionAndWaitForRunResponse( boolean success
427429
else
428430
{
429431
// When responded with a failure
430-
runHandler.onFailure( new RuntimeException() );
432+
runHandler.onFailure( error );
431433
}
432434

433435
// Then
434436
assertTrue( cursorFuture.isDone() );
435437
if ( success )
436438
{
437-
assertNotNull( cursorFuture.get() );
439+
assertFalse( cursorFuture.get().runError().isPresent() );
438440
}
439441
else
440442
{
441-
assertThrows( RuntimeException.class, () -> await( cursorFuture ) );
443+
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
442444
}
443445
}
444446

driver/src/test/java/org/neo4j/driver/internal/messaging/v42/BoltProtocolV42Test.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
import static org.junit.jupiter.api.Assertions.assertFalse;
8080
import static org.junit.jupiter.api.Assertions.assertNotNull;
8181
import static org.junit.jupiter.api.Assertions.assertNull;
82-
import static org.junit.jupiter.api.Assertions.assertThrows;
82+
import static org.junit.jupiter.api.Assertions.assertSame;
8383
import static org.junit.jupiter.api.Assertions.assertTrue;
8484
import static org.mockito.ArgumentMatchers.any;
8585
import static org.mockito.ArgumentMatchers.eq;
@@ -376,12 +376,13 @@ private void testFailedRunInAutoCommitTxWithWaitingForResponse( Bookmark bookmar
376376
assertFalse( cursorFuture.isDone() );
377377

378378
// When I response to Run message with a failure
379-
runHandler.onFailure( new RuntimeException() );
379+
Throwable error = new RuntimeException();
380+
runHandler.onFailure( error );
380381

381382
// Then
382383
assertEquals( bookmark, bookmarkHolder.getBookmark() );
383-
assertTrue( cursorFuture.isCompletedExceptionally() );
384-
assertThrows( RuntimeException.class, () -> await( cursorFuture ) );
384+
assertTrue( cursorFuture.isDone() );
385+
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
385386
}
386387

387388
private void testSuccessfulRunInAutoCommitTxWithWaitingForResponse( Bookmark bookmark, TransactionConfig config, AccessMode mode ) throws Exception
@@ -419,6 +420,7 @@ private void testRunInUnmanagedTransactionAndWaitForRunResponse( boolean success
419420

420421
ResponseHandler runHandler = verifyTxRunInvoked( connection );
421422
assertFalse( cursorFuture.isDone() );
423+
Throwable error = new RuntimeException();
422424

423425
if ( success )
424426
{
@@ -427,18 +429,18 @@ private void testRunInUnmanagedTransactionAndWaitForRunResponse( boolean success
427429
else
428430
{
429431
// When responded with a failure
430-
runHandler.onFailure( new RuntimeException() );
432+
runHandler.onFailure( error );
431433
}
432434

433435
// Then
434436
assertTrue( cursorFuture.isDone() );
435437
if ( success )
436438
{
437-
assertNotNull( cursorFuture.get() );
439+
assertFalse( cursorFuture.get().runError().isPresent() );
438440
}
439441
else
440442
{
441-
assertThrows( RuntimeException.class, () -> await( cursorFuture ) );
443+
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
442444
}
443445
}
444446

driver/src/test/java/org/neo4j/driver/internal/messaging/v43/BoltProtocolV43Test.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
import static org.junit.jupiter.api.Assertions.assertFalse;
7979
import static org.junit.jupiter.api.Assertions.assertNotNull;
8080
import static org.junit.jupiter.api.Assertions.assertNull;
81-
import static org.junit.jupiter.api.Assertions.assertThrows;
81+
import static org.junit.jupiter.api.Assertions.assertSame;
8282
import static org.junit.jupiter.api.Assertions.assertTrue;
8383
import static org.mockito.ArgumentMatchers.any;
8484
import static org.mockito.ArgumentMatchers.eq;
@@ -375,12 +375,13 @@ private void testFailedRunInAutoCommitTxWithWaitingForResponse( Bookmark bookmar
375375
assertFalse( cursorFuture.isDone() );
376376

377377
// When I response to Run message with a failure
378-
runHandler.onFailure( new RuntimeException() );
378+
Throwable error = new RuntimeException();
379+
runHandler.onFailure( error );
379380

380381
// Then
381382
assertEquals( bookmark, bookmarkHolder.getBookmark() );
382-
assertTrue( cursorFuture.isCompletedExceptionally() );
383-
assertThrows( RuntimeException.class, () -> await( cursorFuture ) );
383+
assertTrue( cursorFuture.isDone() );
384+
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
384385
}
385386

386387
private void testSuccessfulRunInAutoCommitTxWithWaitingForResponse( Bookmark bookmark, TransactionConfig config, AccessMode mode ) throws Exception
@@ -418,6 +419,7 @@ private void testRunInUnmanagedTransactionAndWaitForRunResponse( boolean success
418419

419420
ResponseHandler runHandler = verifyTxRunInvoked( connection );
420421
assertFalse( cursorFuture.isDone() );
422+
Throwable error = new RuntimeException();
421423

422424
if ( success )
423425
{
@@ -426,18 +428,18 @@ private void testRunInUnmanagedTransactionAndWaitForRunResponse( boolean success
426428
else
427429
{
428430
// When responded with a failure
429-
runHandler.onFailure( new RuntimeException() );
431+
runHandler.onFailure( error );
430432
}
431433

432434
// Then
433435
assertTrue( cursorFuture.isDone() );
434436
if ( success )
435437
{
436-
assertNotNull( cursorFuture.get() );
438+
assertFalse( cursorFuture.get().runError().isPresent() );
437439
}
438440
else
439441
{
440-
assertThrows( RuntimeException.class, () -> await( cursorFuture ) );
442+
assertSame( error, cursorFuture.get().runError().orElseThrow( () -> new RuntimeException( "Unexpected" ) ) );
441443
}
442444
}
443445

0 commit comments

Comments
 (0)