我很困惑:
用法:try(Pipeline pipeline = jedisPool.getResource().pipelined())
它会关闭管道和jedis实例吗?或者只是关闭管道?
我在多线程中使用它,我得到的错误如下。我很困惑,错误是由JedisPool中的实例耗尽(由于资源的尝试使用)或因为死锁(因为JedisPool在多线程中不能很好)?
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
org.apache.commons.pool2.impl.LinkedBlockingDeque.takeFirst(LinkedBlockingDeque.java:583)
org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:442)
org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)
redis.clients.util.Pool.getResource(Pool.java:49)
redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
io.codis.jodis.RoundRobinJedisPool.getResource(RoundRobinJedisPool.java:214)
答案 0 :(得分:1)
在您的案例Pipeline pipeline
中,尝试使用资源仅关闭变量。您可以在文档https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
您可以使用示例进行检查:
MyResource
上课:
class MyResource implements AutoCloseable {
public SubResource getSubResource() {
return new SubResource();
}
@Override
public void close() throws Exception {
System.out.println("Resource closed");
}
}
SubResource
上课:
class SubResource implements AutoCloseable{
@Override
public void close() throws Exception {
System.out.println("SubResource closed");
}
}
Main
上课:
class Main {
public static void main(String[] args) {
try (SubResource s = new MyResource().getSubResource()) {
System.out.println("Before closing");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("After closing");
}
}
执行结果:
Before closing
SubResource closed
After closing