Managing resources is important. Any mishandling (not releasing) of the resources—database connections and file descriptors left opened, for example—can exhaust the system's capability to operate. That's why, in JDK 7, the try-with-resources statement was introduced. We have used it in the examples of Chapter 6, Database Programming:
try (Connection conn = getDbConnection();
Statement st = createStatement(conn)) {
st.execute(sql);
} catch (Exception ex) {
ex.printStackTrace();
}
As a reminder, here is the getDbConnection() method:
Connection getDbConnection() {
PGPoolingDataSource source = new PGPoolingDataSource();
source.setServerName("localhost");
source.setDatabaseName("cookbook");
try {
return source.getConnection();
} catch(Exception ex) {
ex.printStackTrace();
return...