2013-04-10 8 views
1

다음 코드를 살펴보십시오. 1. stardog에 연결 풀을 생성 중입니다.
2. 풀에서 연결 얻기. 3. 사용 후 풀에 연결을 되 돌리십시오.stardog의 연결 풀에서 연결을 닫으면 어떻게됩니까

풀로 돌아가는 대신 aConn.close()을하면 내 질문은 어떻게됩니까? 내가 aConn.close();

가 추천 주요 내가 어떤 클래스에서 연결을 사용할 때마다 내가 해달라고 이유는 내가 aPool.release(aConn);

을 할 수있는 풀 객체를 해달라고인가하여 연결을 닫으면 happends 것을

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(); 

그래. 풀링 사용을 망칠 수 있습니까?

답변

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()); 
    } 
} 

이 방법을 올바르게 풀에 다시 릴리스하면 풀에 대한 참조를 전달하는 것에 대해 걱정할 필요가 없습니다.

+0

감사합니다. 이것은 명확한 아이디어를 제공합니다. –

+0

다른 사람들이 이것이 올바른 정보라는 것을 알고 있도록 답변을 수락하십시오. – Michael

+0

닫힌 연결을 유지하는 풀은 다른 문제를 생성합니까? 다른 클래스가 연결을 요청하면 풀은 닫힌 연결을 제공하고 닫힌 연결 예외로 끝납니다. –

관련 문제