mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Matthew Swift
15.11.2012 4f425a29d8c7115f8014f7548db560400c850e41
Fix OPENDJ-590: ConnectionPool may return already closed/disconnected connections

Fix bug and enable unit test.


2 files modified
12 ■■■■ changed files
opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/FixedConnectionPool.java 10 ●●●●● patch | view | raw | blame | history
opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/ConnectionPoolTestCase.java 2 ●●● patch | view | raw | blame | history
opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/FixedConnectionPool.java
@@ -575,7 +575,9 @@
    @Override
    public FutureResult<Connection> getConnectionAsync(
            final ResultHandler<? super Connection> handler) {
        QueueElement holder;
        // Loop while iterating through stale connections (see OPENDJ-590).
        for (;;) {
            final QueueElement holder;
        synchronized (queue) {
            if (isClosed) {
                throw new IllegalStateException("FixedConnectionPool is already closed");
@@ -592,12 +594,17 @@
        if (!holder.isWaitingFuture()) {
            // There was a completed connection attempt.
            final Connection connection = holder.getWaitingConnection();
                if (connection.isValid()) {
            final PooledConnection pooledConnection = new PooledConnection(connection);
            if (handler != null) {
                handler.handleResult(pooledConnection);
            }
            return new CompletedFutureResult<Connection>(pooledConnection);
        } else {
                    // Close the stale connection and try again.
                    connection.close();
                }
            } else {
            // Grow the pool if needed.
            final FutureResult<Connection> future = holder.getWaitingFuture();
            if (!future.isDone() && currentPoolSize.tryAcquire()) {
@@ -606,6 +613,7 @@
            return future;
        }
    }
    }
    /**
     * {@inheritDoc}
opendj3/opendj-ldap-sdk/src/test/java/org/forgerock/opendj/ldap/ConnectionPoolTestCase.java
@@ -273,7 +273,7 @@
     * @throws Exception
     *             If an unexpected error occurred.
     */
    @Test(enabled = false)
    @Test
    public void testSkipStaleConnectionsOnGet() throws Exception {
        // Setup.
        final Connection connection1 = mock(Connection.class);