Skip to content

Commit 177a4ff

Browse files
committed
Session#reset() releases connection last
Previously it tried to first release connection and then mark existing transaction as terminated. This could've been problematic because it allowed transaction to be used on the client after RESET, which clears connection state on the server. This commit makes `Session#reset()` first mark existing transaction as terminated and only then release the connection. Releasing connection implies RESET.
1 parent aac5738 commit 177a4ff

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,13 @@ public void reset()
254254

255255
private CompletionStage<Void> resetAsync()
256256
{
257-
return releaseConnectionNow().thenCompose( ignore -> existingTransactionOrNull() )
258-
.thenAccept( tx ->
259-
{
260-
if ( tx != null )
261-
{
262-
tx.markTerminated();
263-
}
264-
} );
257+
return existingTransactionOrNull().thenAccept( tx ->
258+
{
259+
if ( tx != null )
260+
{
261+
tx.markTerminated();
262+
}
263+
} ).thenCompose( ignore -> releaseConnectionNow() );
265264
}
266265

267266
@Override

driver/src/test/java/org/neo4j/driver/internal/NetworkSessionTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,25 @@ public void shouldBeginTxAfterRunFailureToAcquireConnection()
663663
verifyBeginTx( connection, times( 1 ) );
664664
}
665665

666+
@Test
667+
public void shouldMarkTransactionAsTerminatedAndThenReleaseConnectionOnReset()
668+
{
669+
NetworkSession session = newSession( connectionProvider, READ );
670+
Transaction tx = session.beginTransaction();
671+
672+
assertTrue( tx.isOpen() );
673+
when( connection.releaseNow() ).then( invocation ->
674+
{
675+
// verify that tx is not open when connection is released
676+
assertFalse( tx.isOpen() );
677+
return completedFuture( null );
678+
} );
679+
680+
session.reset();
681+
682+
verify( connection ).releaseNow();
683+
}
684+
666685
private void testConnectionAcquisition( AccessMode sessionMode, AccessMode transactionMode )
667686
{
668687
NetworkSession session = newSession( connectionProvider, sessionMode );

0 commit comments

Comments
 (0)