1

次のコードを見てください。1. stardog への接続プールを作成して
います。 2. プールから接続を取得しています。3. 使用後に接続をプールに戻す。

aConn.close()私の質問は、プールに戻る代わりにそうするとどうなるかということです.

 ConnectionConfiguration aConnConfig = ConnectionConfiguration
.to("testConnectionPool")
.credentials("admin", "admin");

ConnectionPoolConfig aConfig = ConnectionPoolConfig
   .using(aConnConfig)
   .minPool(10)
   .maxPool(1000)
   .expiration(1, TimeUnit.HOURS)   
   .blockAtCapacity(1, TimeUnit.MINUTES);

// now i can create my actual connection pool
ConnectionPool aPool = aConfig.create();

// if I want a connection object...
Connection aConn = aPool.obtain();

// now I can feel free to use the connection object as usual...

// and when I'm done with it, instead of closing the connection, 
//I want to return it to the pool instead.
aPool.release(aConn);

// and when I'm done with the pool, shut it down!
aPool.shutdown();

接続を閉じるとどうなりますかaConn.close();

プールオブジェクトを持っていないクラスで接続を使用するたびに私が尋ねる主な理由aPool.release(aConn);

することをお勧めします。プーリングの使用を台無しにしますか。

4

1 に答える 1

2

接続を直接閉じると、接続が解放されていないため、プールはまだ接続への参照を保持します。そのため、接続がそのリソースを閉じている間、プールは参照を保持し、時間の経過とともにメモリ リークが発生する可能性があります。

これに対処するための推奨される方法は、プールから接続を取得し、DelegatingConnection を使用してラップすることです。

public final class PooledConnection extends DelegatingConnection {
    private final ConnectionPool mPool;
    public PooledConnection(final Connection theConnection, final ConnectionPool thePool) {
        super(theConnection);
        mPool = thePool;
    }

    @Override
    public void close() {
        super.close();
        mPool.release(getConnection());
    }
}

このようにして、それを使用するコードで Connection を閉じるだけで、プールに正しく解放され、プールへの参照の受け渡しについて心配する必要がなくなります。

于 2013-04-10T13:34:58.170 に答える