我正在使用HikariDataSource管理到我的Postgres DB的连接池。
我正在尝试使用一种资源来从HikariDataSource获取连接,我想了解以下内容:
-
每次连接是否真的关闭?
-
如果可以,以这种方式处理准备好的语句没有优点吗?
-
在连接池中使用预准备语句的最佳实践是什么?
这是我的连接代码:
<OnSwipe
app:dragDirection="dragUp"
app:onTouchUp="autoCompleteToEnd"
app:touchAnchorId="@+id/card"
app:touchAnchorSide="top"
app:touchRegionId="@+id/card" />
以下是使用预准备语句执行查询的示例:
public <T> CompletableFuture<T> withConnection(FunctionThatThrowsChecedException<Connection,T> action) {
return CompletableFuture.supplyAsync(() -> {
try (Connection connection = ds.getconnection()) {
return action.apply(connection);
} catch (SQLException | IOException e) {
throw new RuntimeException("error while getting collection",e);
}
},workerThreads);
}