这不一定会产生经典的“永久”意义上的泄漏。垃圾收集器最终将到达准备好的语句的第一个实例,并调用其终结器。
但是,这不是让垃圾收集器处理释放潜在的关键资源(例如DB句柄)的好习惯:您应该
close在重用已准备好的语句变量之前调用该方法,或者根本不要重用该变量。
try { Connection = connectionFactory.getConnection(); ps = statement.prepareStamement(someQuery); // execute and read and stuff // now you want to use the ps again, since you don't want ps1, ps2, ps3, etc. // v v v v v v v v v v v SomeUtilClass.close(ps); // ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ps = statement.prepareStatement(someOtherQuery); // DOES THIS FORM A POTENTIAL LEAK?} catch (a lot of exceptions) { // process exceptions} finally { // close the resources (using util class with null-checks and everything) SomeUtilClass.close(ps); SomeUtilClass.close(connection);}


