我对Java编程很陌生,对使用资源共享代码有疑问。
String statement= "<statement>";
DataSource ds = createDSConnection();
if (ds == null) return;
try (PreparedStatement prepare = ds.getConnection().prepareStatement(statement)) {
// statement values
prepare.execute();
} catch (Exception e) {
}这将同时关闭PrepareStatement和db.connection(),还是只关闭PreparedStatement?
发布于 2020-10-01 08:51:31
如图所示,您的代码将只关闭准备好的语句,而泄漏连接。如果要同时关闭这两种方法,则需要在资源块中每个资源使用一条语句:
try (Connection connection = ds.getConnection();
PreparedStatement prepare = connection.prepareStatement(statement)) {
// your code here
}https://stackoverflow.com/questions/64144096
复制相似问题