Skip to content

Commit e523912

Browse files
committed
Merge pull request #167 from jakewins/1.0-pool-close
Resolve issue where pooled connections could leak if exception occurred
2 parents 95706eb + 05e3f59 commit e523912

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

driver/src/main/java/org/neo4j/driver/internal/pool/PooledConnection.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,15 @@ public void close()
156156
{
157157
reset( StreamCollector.NO_OP );
158158
sync();
159-
release.accept( this );
160159
}
161160
catch (Exception ex)
162161
{
163162
dispose();
164163
}
164+
finally
165+
{
166+
release.accept( this );
167+
}
165168
}
166169

167170
@Override
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright (c) 2002-2016 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.internal.pool;
20+
21+
import org.junit.Test;
22+
23+
import java.util.LinkedList;
24+
25+
import org.neo4j.driver.internal.spi.Connection;
26+
import org.neo4j.driver.internal.spi.StreamCollector;
27+
import org.neo4j.driver.internal.util.Consumer;
28+
import org.neo4j.driver.v1.exceptions.DatabaseException;
29+
30+
import static org.hamcrest.CoreMatchers.equalTo;
31+
import static org.hamcrest.CoreMatchers.hasItem;
32+
import static org.hamcrest.MatcherAssert.assertThat;
33+
import static org.mockito.Matchers.any;
34+
import static org.mockito.Mockito.doThrow;
35+
import static org.mockito.Mockito.mock;
36+
37+
public class PooledConnectionTest
38+
{
39+
@Test
40+
public void shouldReturnToPoolIfExceptionDuringReset() throws Throwable
41+
{
42+
// Given
43+
final LinkedList<PooledConnection> returnedToPool = new LinkedList<>();
44+
Connection conn = mock( Connection.class );
45+
46+
doThrow( new DatabaseException( "asd", "asd" ) ).when(conn).reset( any( StreamCollector.class) );
47+
48+
PooledConnection pooledConnection = new PooledConnection( conn, new Consumer<PooledConnection>()
49+
{
50+
@Override
51+
public void accept( PooledConnection pooledConnection )
52+
{
53+
returnedToPool.add( pooledConnection );
54+
}
55+
} );
56+
57+
// When
58+
pooledConnection.close();
59+
60+
// Then
61+
assertThat( returnedToPool, hasItem(pooledConnection) );
62+
assertThat( returnedToPool.size(), equalTo( 1 ));
63+
}
64+
}

0 commit comments

Comments
 (0)