我正在为 redission 客户端编写一个适配器以在我们的应用程序中使用,我不确定在 finalize 块中关闭客户端是否是一个好的设计。下面是代码。请告诉我
private static final RedissonClient client;
static {
File configFile = Paths.get(Constants.ConfigDir, "cache-
config.yml").toFile();
try {
client = Redisson.create(Config.fromYAML(configFile));
} catch (IOException e) {
throw new UnableToCreateCacheClientException(e.getMessage() + e.getStackTrace(), e.getCause());
}
}
@Override
protected void finalize() throws Throwable {
super.finalize();
client.shutdown();
}
public static RedissonClient getClient() {
return client;
}
编辑:我有兴趣了解在 Web 应用程序中关闭静态最终连接对象的正确设计。我无法在方法的 finally 块中关闭它,因为客户端将被多个类中的多个方法使用