技术问题,求解答!
从性能及资源消耗方面考虑, 评论下面这段代码哪里可以做出改善?
public class Job { private DataSource dataSource; public void work() { Connection conn1 = null; Connection conn2 = null; try { conn1 = dataSource.getConnection(); conn2 = dataSource.getConnection(); // do something } catch (SQLException e) { e.printStackTrace(); } finally { if (conn1 != null) { try { conn1.close(); } catch (SQLException e) {} } if (conn2 != null) { try { conn2.close(); } catch (SQLException e) {} } } } // getter/setter ... }
public class Job {
private DataSource dataSource;
public void work() {
Connection conn1 = null;
Connection conn2 = null;
try {
conn1 = dataSource.getConnection();
conn2 = dataSource.getConnection();
// do something
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn1 != null) {
try {
conn1.close();
} catch (SQLException e) {}
}
if (conn2 != null) {
try {
conn2.close();
} catch (SQLException e) {}
}
}
}
// getter/setter ...
}