我正在寻找一种从HikariCP数据库查询返回ResultSet(或CachedResultSet,因为连接关闭)的通用方法
我环顾四周,一个建议的选项是将FutureTask与Callback一起使用,这是我尝试的方法:
public static CachedRowSet query(String query) {
FutureTask<CachedRowSet> future =
new FutureTask(new Callable() {
public CachedRowSet call() {
try {
Connection connection = hikari.getConnection();
ResultSet set = connection.prepareStatement(query).executeQuery();
CachedRowSet crs = new CachedRowSetImpl();
crs.populate(set);
set.close();
connection.close();
return crs;
} catch (SQLException e) {
e.printStackTrace();;
return null;
}
}
});
try {
CachedRowSet crs = future.get();
return crs;
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
return null;
}
}
它只是挂了。整个方法还没有完成,我想知道自己做错了什么? (因为我刚才刚刚转到数据库,所以定期查询数据库是没有问题的)
或者,您能提出一个更好的选择并引导我前进吗?