在Connection上调用close方法会关闭Statement和ResultSet吗?
级联的关闭这听起来好像很有道理,而且在很多地方这样做也是正确的,通常这样写
Connection con = getConnection();//getConnection is your method
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
……
///rs.close();
///ps.close();
con.close(); // NO!
这
样做的问题在于Connection是个接口,它的close实现可能是多种多样的。在普通情况下,你用
DriverManager.getConnection()得到一个Connection实例,调用它的close方法会关闭Statement和
ResultSet。但是在很多时候,你需要使用数据库连接池,在连接池中的得到的Connection上调用close方法的时候,Connection可能并没有被释放,而是回到了连接池中。它以后可能被其它代码取出来用。如果没有释放Statement和ResultSet,那么在Connection上没有关闭的Statement和ResultSet可能会越来越多,那么……
相反,我看到过这样的说法,有人把Connection关闭了,却继续使用ResultSet,认为这样是可以的,引发了激烈的讨论,到底是怎么回事就不用我多说了吧。
所以我们必须很小心的释放数据库资源,下面的代码片断展示了这个过程
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = getConnection();//getConnection is your method
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
///...........
}
catch (SQLException ex) {
///错误处理
}
finally{
try {
if(ps!=null)
ps.close();
}
catch (SQLException ex) {
///错误处理
}
try{
if(con!=null)
con.close();
}
catch (SQLException ex) {
///错误处理
}
}
很麻烦是不是?但为了写出健壮的程序,这些处理是必须的。
上面这东西讲得真是精辟。
分享到:
相关推荐
NULL 博文链接:https://rickgong.iteye.com/blog/2354177
HikariCP-java7-2.4.10.jar 光连接池 传说光一样的速度.
7.关闭连接 `ResultSet 、Statement 、Connection` 3.SQL注入问题与解决(Statement 、PreparedStatement) 3.1.模拟SQL注入 3.2.PreparedStatement解决 3.3.Statement 与 PreparedStatement 4.完整源码
* Close a jdbc resource, such as ResultSet, Statement, Connection.... All * these objects must have a method signature is void close(). * * @param resource - * jdbc resouce to close */ public ...
目前比较广泛使用的分页方式是将查询结果缓存在HttpSession或有状态bean中,翻页的时候从缓存中取出一页...因为ResultSet在Statement或Connection关闭时也会被关闭,如果要使ResultSet有效势必长时间占用数据库连接。
DbUtils 关闭链接等操作 QueryRunner 进行查询的操作 org apache commons dbutils handlers ArrayHandler :将ResultSet中第一行的数据转化成对象数组 ArrayListHandler将ResultSet中所有的数据转化成List List中...
目前比较广泛使用的分页方式是将查询结果缓存在HttpSession或有状态bean中,翻页的时候从缓存中取出一页...因为ResultSet在Statement或Connection关闭时也会被关闭,如果要使ResultSet有效势必长时间占用数据库连接。
释放相关资源 booleanisClosed()throwsSQLException 查询此ResultSet对象是否已经被关闭 StatementgctStatcmcnt()throwsSQLException 获取生成此ResultSet对象的Statement对象 (4)数据库的查询和更新操作 Java数据库...
因为ResultSet在Statement或Connection关闭时也会被关闭,如果要使ResultSet有效势必长时间占用数据库连接。 因此比较好的分页做法应该是每次翻页的时候只从数据库里检索页面大小的块区的数据。这样虽然每次翻页...
JDBC连接所有数据库步骤 1 将数据库的JDBC驱动加载到classpath中,在基于JAVAEE的WEB应用实际开发过程中,通常要把目标...7 依次将ResultSet、Statement、PreparedStatement、Connection对象关闭,释放所占用的资源。
该类整理了连接数据库的常用方法,包括打开连接,关闭连接,关闭Statement,关闭resultset以及数据库配置文件
一:工作量比较大,需要连接,然后处理jdbc底层事务,处理数据类型,还需要操作Connection,Statement对象和ResultSet对象去拿数据并关闭他们。 二:我们对jdbc编程可能产生的异常进行捕捉处理并正确关闭资源 ...
public static void close(ResultSet resultSet, Statement statement, Connection connection) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace();...
一.jdbc访问数据库步骤 1.载入驱动 将jar包加入工程,Class.forname(""); 2.建立连接 使用connection对象的getConnection方法 url(jdbc:oracle:thin:@...6.关闭连接 按照 ResultSet ,Statement,Connection顺序来关闭
public static void closeConn(ResultSet rs, Connection conn, PreparedStatement pstm) { try { if (rs != null) { rs.close(); } } catch (SQLException e) { System.out.println("rs关闭异常!"); }...
其中,与数据库的连接声明成了成员变量,建立连接后可执行各种操作,每个类中的statement、resultset等操作在方法内均关闭,但数据库连接没有关闭,需要在操作完成后调用其中的 closeConnection方法关闭;...
//关闭数据库连接 public void closeConnection() { try { if (rs!=null) rs.close(); } catch(Exception e) { e.printStackTrace(); } try { if (stmt!=null) stmt.close(); ...
//关闭数据库连接 public void closeConnection() { try { if (rs!=null) rs.close(); } catch(Exception e) { e.printStackTrace(); } try { if (stmt!=null) stmt.close(); ...
pstm=con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); for(int i=0;i;i++){ pstm.setObject(i+1,params[i]); } pstm.execute(); }catch...
o(5)操作ResultSet结果集 o(6)关闭连接,释放资源 2.下面进行代码演示 1.注册数据库驱动程序的语法格式如下: DriverManager.registerDriver(Driver driver) 或者 Class.forName("DriverName");