Skip to content

Commit d860477

Browse files
committed
Sync only open connections
Added a check to sync only open connections before closing them in the session.
1 parent 0ddac3d commit d860477

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public StatementResult run( Statement statement )
9191
ensureSessionIsOpen();
9292
ensureNoOpenTransactionBeforeRunningSession();
9393

94-
closeCurrentConnection( true );
94+
syncAndCloseCurrentConnection();
9595
currentConnection = acquireConnection();
9696

9797
return run( currentConnection, statement, this );
@@ -140,13 +140,6 @@ public void close()
140140
throw new ClientException( "This session has already been closed." );
141141
}
142142

143-
if ( currentConnection != null && !currentConnection.isOpen() )
144-
{
145-
// the socket connection is already closed due to some error, cannot send more data
146-
closeCurrentConnection( false );
147-
return;
148-
}
149-
150143
synchronized ( this )
151144
{
152145
if ( currentTransaction != null )
@@ -162,7 +155,8 @@ public void close()
162155
}
163156
}
164157
}
165-
closeCurrentConnection( true );
158+
159+
syncAndCloseCurrentConnection();
166160
}
167161

168162
@Override
@@ -177,7 +171,7 @@ public synchronized Transaction beginTransaction( String bookmark )
177171
ensureSessionIsOpen();
178172
ensureNoOpenTransactionBeforeOpeningTransaction();
179173

180-
closeCurrentConnection( true );
174+
syncAndCloseCurrentConnection();
181175
currentConnection = acquireConnection();
182176

183177
currentTransaction = new ExplicitTransaction( currentConnection, this, bookmark );
@@ -200,15 +194,15 @@ public TypeSystem typeSystem()
200194
@Override
201195
public synchronized void onResultConsumed()
202196
{
203-
closeCurrentConnection( false );
197+
closeCurrentConnection();
204198
}
205199

206200
@Override
207201
public synchronized void onTransactionClosed( ExplicitTransaction tx )
208202
{
209203
if ( currentTransaction != null && currentTransaction == tx )
210204
{
211-
closeCurrentConnection( false );
205+
closeCurrentConnection();
212206
lastBookmark = currentTransaction.bookmark();
213207
currentTransaction = null;
214208
}
@@ -286,6 +280,16 @@ boolean currentConnectionIsOpen()
286280
return currentConnection != null && currentConnection.isOpen();
287281
}
288282

283+
private void syncAndCloseCurrentConnection()
284+
{
285+
closeCurrentConnection( true );
286+
}
287+
288+
private void closeCurrentConnection()
289+
{
290+
closeCurrentConnection( false );
291+
}
292+
289293
private void closeCurrentConnection( boolean sync )
290294
{
291295
if ( currentConnection == null )
@@ -297,7 +301,7 @@ private void closeCurrentConnection( boolean sync )
297301
currentConnection = null;
298302
try
299303
{
300-
if ( sync )
304+
if ( sync && connection.isOpen() )
301305
{
302306
connection.sync();
303307
}

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.Rule;
2323
import org.junit.Test;
2424
import org.junit.rules.ExpectedException;
25+
import org.mockito.InOrder;
2526

2627
import java.util.Map;
2728

@@ -47,6 +48,7 @@
4748
import static org.mockito.Matchers.eq;
4849
import static org.mockito.Mockito.RETURNS_MOCKS;
4950
import static org.mockito.Mockito.anyMapOf;
51+
import static org.mockito.Mockito.inOrder;
5052
import static org.mockito.Mockito.mock;
5153
import static org.mockito.Mockito.never;
5254
import static org.mockito.Mockito.times;
@@ -204,11 +206,13 @@ public void acquiresNewConnectionForRun()
204206
}
205207

206208
@Test
207-
public void closesPreviousConnectionForRun()
209+
public void syncsAndClosesPreviousConnectionForRun()
208210
{
209211
ConnectionProvider connectionProvider = mock( ConnectionProvider.class );
210212
PooledConnection connection1 = mock( PooledConnection.class );
213+
when( connection1.isOpen() ).thenReturn( true );
211214
PooledConnection connection2 = mock( PooledConnection.class );
215+
when( connection2.isOpen() ).thenReturn( true );
212216
when( connectionProvider.acquireConnection( READ ) ).thenReturn( connection1 ).thenReturn( connection2 );
213217
NetworkSession session = newSession( connectionProvider, READ );
214218

@@ -220,7 +224,29 @@ public void closesPreviousConnectionForRun()
220224
verify( connectionProvider, times( 2 ) ).acquireConnection( READ );
221225
verify( connection2 ).run( eq( "RETURN 2" ), anyParams(), any( Collector.class ) );
222226

223-
verify( connection1 ).sync();
227+
InOrder inOrder = inOrder( connection1 );
228+
inOrder.verify( connection1 ).sync();
229+
inOrder.verify( connection1 ).close();
230+
}
231+
232+
@Test
233+
public void closesPreviousBrokenConnectionForRun()
234+
{
235+
ConnectionProvider connectionProvider = mock( ConnectionProvider.class );
236+
PooledConnection connection1 = mock( PooledConnection.class );
237+
PooledConnection connection2 = mock( PooledConnection.class );
238+
when( connectionProvider.acquireConnection( READ ) ).thenReturn( connection1 ).thenReturn( connection2 );
239+
NetworkSession session = newSession( connectionProvider, READ );
240+
241+
session.run( "RETURN 1" );
242+
verify( connectionProvider ).acquireConnection( READ );
243+
verify( connection1 ).run( eq( "RETURN 1" ), anyParams(), any( Collector.class ) );
244+
245+
session.run( "RETURN 2" );
246+
verify( connectionProvider, times( 2 ) ).acquireConnection( READ );
247+
verify( connection2 ).run( eq( "RETURN 2" ), anyParams(), any( Collector.class ) );
248+
249+
verify( connection1, never() ).sync();
224250
verify( connection1 ).close();
225251
}
226252

0 commit comments

Comments
 (0)