java - 查询 Oracle 的错误时,避免 Stream 的任何解决方法已经关闭?
问题描述
我正在使用 ojdbc8 版本 21.1.0.0 作为我的 JDBC。我遇到的问题是每次调用之前我的连接流都会关闭,close()
当我执行我的查询时,由于未知原因从 resultSet 请求了很长时间,并且我的 SQL 在 SQL 开发人员上运行良好。所以我用谷歌搜索并看到甲骨文报告了这个错误。
https://support.oracle.com/knowledge/Middleware/832903_1.html
似乎这是他们的 JDBC 的错误?有谁知道哪个版本的 ODBC8 没有这个 bug?或其他解决方法以避免此错误?
==== 更新 ====
@斯蒂芬C
好吧,如果你这么说。但我认为这无论如何都无济于事,因为代码本身太简单了,不会有这样的错误。
class Channel {
private static DataSource ds = null;
private static final String GET_ONE = "SELECT * FROM Channel WHERE Id=?";
public Channel() {
if(ds == null) {
try {
Context ctx = new InitialContext();
ds = (DataSource) ctx.lookup("jdbc/oracle");
} catch (NamingException e) {
e.printStackTrace();
}
}
}
public HashMap<String, Object> getOne(String id) throws SQLException{
HashMap<String, Object> result = null;
ResultSet rs = null;
try(Connection conn = ds.getConnection();
PreparedStatement pstmt = con.prepareStatement(GET_ONE);){
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if(rs.next()){
result = new HashMap<String, Object>();
result.put("Time_Limit", rs.getLong("Time_Limit"));// recommended by @tgdavies
result.put("Id", rs.getString("Id"));
result.put("Name", rs.getString("Name"));
}
} catch(SQLException e){
throw e;
} finally {
if(rs != null){
try{
rs.close();
} catch(SQLException e){}
}
}
return result;
}
}
解决方案
我想我知道问题是什么。
(警告:您没有包含堆栈跟踪,因此我无法确认这一点。此外,您发布的链接未打开,因此我们无法看到您正在谈论的错误报告的详细信息。)
try(Connection conn = ds.getConnection();
PreparedStatement pstmt = con.prepareStatement(GET_ONE);){
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if(rs.next()){
result = new HashMap<String, Object>();
result.put("Time_Limit", rs.getLong("Time_Limit"));// recommended by @tgdavies
result.put("Id", rs.getString("Id"));
result.put("Name", rs.getString("Name"));
}
} catch(SQLException e){
throw e;
} finally {
if(rs != null){
try{
rs.close();
} catch(SQLException e){}
}
}
问题是finally
在资源自动关闭之后执行。因此,您的代码将在关闭rs.close()
后调用。PreparedStatement
但关闭语句会关闭从该语句创建的任何结果集。
参考:
可能的解决方案:
- 也将其
ResultSet
视为资源;例如,使用资源的嵌套尝试。 - 摆脱
finally
...
推荐阅读
- java - 如何解决 org.postgresql.jdbc.PgConnection.createClob() 尚未实现
- docker - 是否可以在 Kubernetes 中启动自签名 Docker 注册表并让其他服务使用它作为注册表来获取其映像?
- python - 我的二十一点游戏代码有什么问题?
- java - 如何在不影响 Cucumber 和 Selenium 中的其他步骤的情况下 close() 和 quit() Selenium 驱动程序?
- python - Tensorflow 虽然 loop_vars 和 body 参数相同,但两个结构在 while_loop 中没有相同的嵌套结构
- angular - Angular:调用帖子时出现错误请求错误
- java - 使用导航组件来回导航时,ViewPager 的内容消失
- python - 运行 python 代码时,在 Qt 设计器中创建的 ui 看起来不一样
- python - 我的 Django 服务器没有任何响应
- google-sheets - 如何根据相邻单元格计算文本字符串?