java.sql.statement_java.sql.Connection.close()对java.sql.Statement对象等的影响

Does closing a java.sql.Connection

also close all the statements,

prepared statements, etc. obtained

from that connection? Or is there

going to be memory leak if I close the

connection but leave the statements,

etc. unclosed?

你不应该依赖它.

规范如下:

An application calls the method

Statement.close to indicate that it

has finished processing a statement.

All Statement objects will be closed

when the connection that created them

is closed. However, it is good coding

practice for applications to close

statements as soon as they have

finished processing them. This allows

any external resources that the

statement is using to be released

immediately.

最佳做法是在finally块中关闭所有ResultSet,Statements和Connections,每个ResultSet都包含在它们自己的try / catch中,与获取的顺序相反.

写一个这样的类:

public class DatabaseUtils

{

public static void close(Statement s)

{

try

{

if (s != null)

{

s.close();

}

}

catch (SQLException e)

{

// log or report in someway

e.printStackTrace();

}

}

// similar for ResultSet and Connection

}

像这样打电话:

Statement s;

try

{

// JDBC stuff here

}

finally

{

DatabaseUtils.close(s);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值