gpt4 book ai didi

java - 如果我关闭 stardog 连接池中的连接会发生什么

转载 作者:行者123 更新时间:2023-11-30 04:25:35 24 4
gpt4 key购买 nike

看一下下面的代码。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);

是否建议这样做。它会破坏池化的使用吗?

最佳答案

如果你直接关闭连接,池仍然会有对连接的引用,因为它还没有被释放,所以当连接将关闭其资源时,池将保留引用,你可能会泄漏内存时间。

处理这个问题的建议方法是当您从池中获取连接时,使用 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());
}
}

这样,您可以简单地在使用它的代码中关闭连接,它将正确地释放回池中,并且您不必担心传递对池的引用。

关于java - 如果我关闭 stardog 连接池中的连接会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15922807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com