Java 如果我关闭stardog连接池中的连接会发生什么
看看下面的代码。 1.我正在创建与stardog的连接池Java 如果我关闭stardog连接池中的连接会发生什么,java,connection-pooling,rdf,pool,stardog,Java,Connection Pooling,Rdf,Pool,Stardog,看看下面的代码。 1.我正在创建与stardog的连接池 2.正在从池中获取连接。 3.使用后将连接返回到池 我的问题是如果我执行acon.close()而不是返回池,会发生什么 ConnectionConfiguration aConnConfig = ConnectionConfiguration .to("testConnectionPool") .credentials("admin", "admin"); ConnectionPoolConfig aConfig = Connect
2.正在从池中获取连接。 3.使用后将连接返回到池 我的问题是如果我执行
acon.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();
如果我通过acon.close()关闭连接会发生什么代码>
当我在任何类中使用连接时,我询问的主要原因是我没有池对象来执行aPool.release(acon)代码>
建议您这样做。
它会破坏池的使用。如果您直接关闭连接,池仍将有一个对连接的引用,因为它尚未被释放,因此当连接将关闭其资源时,池将保留该引用,并且随着时间的推移,您可能会泄漏内存
处理此问题的建议方法是,当您从池中获取连接时,使用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());
}
}
这样,您只需在使用它的代码中关闭连接,它就会正确地释放回池中,您不必担心传递对池的引用。池保持关闭的连接会产生任何其他问题吗?例如,如果另一个类请求一个连接,而池提供了一个关闭的连接,它将以一个关闭的连接异常结束。通常,请为这些创建单独的问题。但是不,这不应该发生,因为池不会发出它认为已签出的连接。为什么需要“super.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();