Skip to content

Commit 0cb7a9e

Browse files
author
Zhen Li
authored
Merge pull request #244 from pontusmelke/1.1-fixup
Fixes to driver
2 parents 38aaa1a + 3004db0 commit 0cb7a9e

File tree

9 files changed

+52
-21
lines changed

9 files changed

+52
-21
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.neo4j.driver.v1.Value;
3535
import org.neo4j.driver.v1.Values;
3636
import org.neo4j.driver.v1.exceptions.ClientException;
37+
import org.neo4j.driver.v1.exceptions.ConnectionFailureException;
3738
import org.neo4j.driver.v1.types.TypeSystem;
3839

3940
import static org.neo4j.driver.v1.Values.value;
@@ -296,9 +297,9 @@ private void ensureConnectionIsOpen()
296297
{
297298
if ( !connection.isOpen() )
298299
{
299-
throw new ClientException( "The current session cannot be reused as the underlying connection with the " +
300-
"server has been closed due to unrecoverable errors. " +
301-
"Please close this session and retry your statement in another new session." );
300+
throw new ConnectionFailureException( "The current session cannot be reused as the underlying connection with the " +
301+
"server has been closed due to unrecoverable errors. " +
302+
"Please close this session and retry your statement in another new session." );
302303
}
303304
}
304305

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public BoltServerAddress apply( Value value )
241241
//must be called from a synchronized method
242242
private boolean call( BoltServerAddress address, String procedureName, Consumer<Record> recorder )
243243
{
244-
Connection acquire = null;
244+
Connection acquire;
245245
Session session = null;
246246
try
247247
{
@@ -271,11 +271,6 @@ private boolean call( BoltServerAddress address, String procedureName, Consumer<
271271
{
272272
session.close();
273273
}
274-
if ( acquire != null )
275-
{
276-
acquire.close();
277-
}
278-
279274
}
280275
return true;
281276
}

driver/src/main/java/org/neo4j/driver/internal/net/pooling/PooledConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public PooledConnection( Connection delegate, Consumer<PooledConnection> release
6767
this.lastUsed = clock.millis();
6868
}
6969

70-
public void updateUsageTimestamp()
70+
public void updateTimestamp()
7171
{
7272
lastUsed = clock.millis();
7373
}

driver/src/main/java/org/neo4j/driver/internal/net/pooling/SocketConnectionPool.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.neo4j.driver.internal.net.pooling;
2020

21+
import java.util.ArrayList;
22+
import java.util.List;
2123
import java.util.Map;
2224
import java.util.concurrent.BlockingQueue;
2325
import java.util.concurrent.ConcurrentHashMap;
@@ -39,6 +41,8 @@
3941
import org.neo4j.driver.v1.Value;
4042
import org.neo4j.driver.v1.exceptions.ClientException;
4143

44+
import static java.util.Collections.emptyList;
45+
4246
/**
4347
* The pool is designed to buffer certain amount of free sessions into session pool. When closing a session, we first
4448
* try to return the session into the session pool, however if we failed to return it back, either because the pool
@@ -115,7 +119,7 @@ public Connection acquire( BoltServerAddress address )
115119
conn = new PooledConnection( connect( address ), new
116120
PooledConnectionReleaseConsumer( connections, stopped, new PooledConnectionValidator( this, poolSettings ) ), clock );
117121
}
118-
conn.updateUsageTimestamp();
122+
conn.updateTimestamp();
119123
return conn;
120124
}
121125

@@ -184,4 +188,19 @@ public void close()
184188
pools.clear();
185189
}
186190

191+
//for testing
192+
public List<PooledConnection> connectionsForAddress(BoltServerAddress address)
193+
{
194+
LinkedBlockingQueue<PooledConnection> pooledConnections =
195+
(LinkedBlockingQueue<PooledConnection>) pools.get( address );
196+
if (pooledConnections == null)
197+
{
198+
return emptyList();
199+
}
200+
else
201+
{
202+
return new ArrayList<>( pooledConnections );
203+
}
204+
}
205+
187206
}

driver/src/main/java/org/neo4j/driver/internal/spi/Collector.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ public void doneFailure( Neo4jException error )
4141
throw new ClientException(
4242
"Invalid server response message `FAILURE` received for client message `ACK_FAILURE`.", error );
4343
}
44-
45-
@Override
46-
public void doneIgnored()
47-
{
48-
throw new ClientException(
49-
"Invalid server response message `IGNORED` received for client message `ACK_FAILURE`." );
50-
}
5144
};
5245

5346
class InitCollector extends NoOperationCollector

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.neo4j.driver.v1.Logger;
2828
import org.neo4j.driver.v1.Transaction;
2929
import org.neo4j.driver.v1.exceptions.ClientException;
30+
import org.neo4j.driver.v1.exceptions.ConnectionFailureException;
3031

3132
import static junit.framework.Assert.fail;
3233
import static junit.framework.TestCase.assertNotNull;
@@ -121,7 +122,7 @@ public void shouldNotAllowMoreStatementsInSessionWhileConnectionClosed() throws
121122
when( mock.isOpen() ).thenReturn( false );
122123

123124
// Expect
124-
exception.expect( ClientException.class );
125+
exception.expect( ConnectionFailureException.class );
125126

126127
// When
127128
sess.run( "whatever" );
@@ -134,7 +135,7 @@ public void shouldNotAllowMoreTransactionsInSessionWhileConnectionClosed() throw
134135
when( mock.isOpen() ).thenReturn( false );
135136

136137
// Expect
137-
exception.expect( ClientException.class );
138+
exception.expect( ConnectionFailureException.class );
138139

139140
// When
140141
sess.beginTransaction();

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
import org.neo4j.driver.internal.logging.ConsoleLogging;
3737
import org.neo4j.driver.internal.net.BoltServerAddress;
38+
import org.neo4j.driver.internal.net.pooling.PooledConnection;
39+
import org.neo4j.driver.internal.net.pooling.SocketConnectionPool;
3840
import org.neo4j.driver.internal.spi.Connection;
3941
import org.neo4j.driver.v1.AccessMode;
4042
import org.neo4j.driver.v1.Config;
@@ -86,6 +88,26 @@ public void shouldDiscoverServers() throws IOException, InterruptedException, St
8688
assertThat( server.exitStatus(), equalTo( 0 ) );
8789
}
8890

91+
@Test
92+
public void shouldOnlyPutConnectionInPoolOnce() throws IOException, InterruptedException, StubServer.ForceKilled
93+
{
94+
// Given
95+
StubServer server = StubServer.start( "discover_servers.script", 9001 );
96+
URI uri = URI.create( "bolt+routing://127.0.0.1:9001" );
97+
98+
// When
99+
try ( RoutingDriver driver = (RoutingDriver) GraphDatabase.driver( uri, config ) )
100+
{
101+
// Then
102+
SocketConnectionPool pool = (SocketConnectionPool) driver.connectionPool();
103+
List<PooledConnection> pooledConnections = pool.connectionsForAddress( address( 9001 ) );
104+
assertThat(pooledConnections, hasSize( 1 ));
105+
}
106+
107+
// Finally
108+
assertThat( server.exitStatus(), equalTo( 0 ) );
109+
}
110+
89111
@Test
90112
public void shouldDiscoverNewServers() throws IOException, InterruptedException, StubServer.ForceKilled
91113
{

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import org.neo4j.driver.v1.exceptions.NoSuchRecordException;
4444
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
4545
import org.neo4j.driver.v1.summary.ResultSummary;
46-
import org.neo4j.driver.v1.util.BiFunction;
4746
import org.neo4j.driver.v1.util.Function;
4847

4948
import static java.util.Arrays.asList;

driver/src/test/java/org/neo4j/driver/internal/net/pooling/PooledConnectionTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public void dispose()
120120
assertThat( flags[0], equalTo( false ) );
121121
}
122122

123+
123124
@Test
124125
public void shouldDisposeConnectionIfValidConnectionAndIdlePoolIsFull() throws Throwable
125126
{

0 commit comments

Comments
 (0)