如果我在一个程序中同时用Connection,PrepareStatement,ResultSet,应该如何关闭这些资源 ?我只关闭Connection而不关闭其他两个会怎样?
我只关闭Connection,PrepareStatement而不关闭ResultSet会怎样?
我只关闭Connection,PrepareStatement而不关闭ResultSet会怎样?
调试欢乐多
rs.close();
ps.close();
con.close();
/**
* Releases this <code>Connection</code> object's database and JDBC resources
* immediately instead of waiting for them to be automatically released.
* <P>
* Calling the method <code>close</code> on a <code>Connection</code>
* object that is already closed is a no-op.
* <P>
* It is <b>strongly recommended</b> that an application explicitly
* commits or rolls back an active transaction prior to calling the
* <code>close</code> method. If the <code>close</code> method is called
* and there is an active transaction, the results are implementation-defined.
* <P>
*
* @exception SQLException SQLException if a database access error occurs
*/
Statement的colse方法
/**
* Releases this <code>Statement</code> object's database
* and JDBC resources immediately instead of waiting for
* this to happen when it is automatically closed.
* It is generally good practice to release resources as soon as
* you are finished with them to avoid tying up database
* resources.
* <P>
* Calling the method <code>close</code> on a <code>Statement</code>
* object that is already closed has no effect.
* <P>
* <B>Note:</B>When a <code>Statement</code> object is
* closed, its current <code>ResultSet</code> object, if one exists, is
* also closed.
*
* @exception SQLException if a database access error occurs
*/
void close() throws SQLException;
这个应该可以得出关Statement就不用关ResultSet了,也就是说我只关Connection和Statement就可以了 ?
关键是“会怎样”,我知道依次关闭这回事好吧,高手指点
如果你一个connection 开了 N个rs或ps而里面又不是用一次rsps关一次rsps的话,最后系统会报 “打开的游标超过最大数量”的错误。不信可以
Connection con = ....
Statement st = ....
ResultSet rs = ...
for(int i=0;i<10000;i++){
rs=st.executeUpdate(sql);
}...试试看。oracle默认300。i跑到301,咯屁。
我只是在前几天写代码的时候突然想到为什么要在 finally里依次关闭rs,stmt,conn呢,直接关掉conn不就得了?
从api上看,如果关闭stmt那么rs也就关闭了(<B>Note: </B>When a <code>Statement </code> object is
* closed, its current <code>ResultSet </code> object, if one exists, is
* also closed. ),但是好像没有地方说关闭conn的话,stmt也会被关闭,所以上来问下,day day up .
还有16楼说的
“如果只关闭con,不关闭rs这样并不会有问题。因为con一关,由于rs、ps都要由con来的,所以con一关,rs、ps也会退出占有的资源” 这个话是否有依据 ?
“如果只关闭con,不关闭rs这样并不会有问题。因为con一关,由于rs、ps都要由con来的,所以con一关,rs、ps也会退出占有的资源” 这个话是否有依据 ?
----------------------------------------------------
只是关了conn拿不到rs和ps了 但不见得会回收已创建的rs和ps对象吧
}finally {
try {
rs.close() ;
ps.close() ;
conn.close() ;
}catch(Exception e) {
}}每打开一个连接就像打开一闪门一样。
而conn 是最外的一闪门。以次往里。 --ps --rs
而当你出来的时候一定要有礼貌的一闪一闪的把门关掉。虽然把最外的门一关就什么都没有了。
可是程序看起来就一点也不健壮。有好的习惯吧。
建议LZ都关闭 并按顺序依次关闭:
rs.close();
ps.close();
con.close();
JDBC最好就是它的效率了,如果你有对象不光的话会影响效率的
在说关一下举手之劳而已
try{
if(rs != null)
rs.close();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(prs != null)
prs.close();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(con != null)
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
......
finally {
try {
rs.close();
} catch (SQLException e) {
//dosth
}
try {
pstmt.close();
} catch (SQLException e) {
//dosth
}
try {
con.close();
} catch (SQLException e) {
//dosth
}
try {
rs.close();
} catch (SQLException e) {
//dosth
}
这个就不用要了,还有34说的是没有必要的,如果是空那就更不用关了,即使出异常也不会影响下面的关闭。还有那个说完美的那个,你的代码写的真是“完美”.
ps.close();
con.close();
如果对软件架构不熟悉的话,最好资源使用完,就关掉,垃圾回收机制是要在特定环境因素下才会主动去做回收动作的。
PrepareStatement 会进行预编译SQL语句 优化性能