Skip to content

Commit 0bade55

Browse files
committed
Simplified connection closing so it's concise for the tutorial.
1 parent a436c2c commit 0bade55

1 file changed

Lines changed: 3 additions & 30 deletions

File tree

src/main/java/Main.java

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ private void showHome(HttpServletRequest req, HttpServletResponse resp)
2727
private void showDatabase(HttpServletRequest req, HttpServletResponse resp)
2828
throws ServletException, IOException {
2929
Connection connection = null;
30-
Statement stmt = null;
3130
try {
3231
connection = getConnection();
3332

34-
stmt = connection.createStatement();
33+
Statement stmt = connection.createStatement();
3534
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)");
3635
stmt.executeUpdate("INSERT INTO ticks VALUES (now())");
3736
ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks");
@@ -45,36 +44,10 @@ private void showDatabase(HttpServletRequest req, HttpServletResponse resp)
4544
} catch (Exception e) {
4645
resp.getWriter().print("There was an error: " + e.getMessage());
4746
} finally {
48-
ensureConnectionClosed(connection, stmt, null);
47+
if (connection != null) try{connection.close();} catch(SQLException e){}
4948
}
5049
}
5150

52-
public void ensureConnectionClosed(Connection conn, Statement ps, ResultSet rs) {
53-
if (rs != null) {
54-
try {
55-
rs.close();
56-
}
57-
catch (SQLException e) {
58-
}
59-
}
60-
61-
if (ps != null) {
62-
try {
63-
ps.close();
64-
}
65-
catch (SQLException e) {
66-
}
67-
}
68-
69-
if (conn != null) {
70-
try {
71-
conn.close();
72-
}
73-
catch (SQLException e) {
74-
}
75-
}
76-
}
77-
7851
private Connection getConnection() throws URISyntaxException, SQLException {
7952
URI dbUri = new URI(System.getenv("DATABASE_URL"));
8053

@@ -87,7 +60,7 @@ private Connection getConnection() throws URISyntaxException, SQLException {
8760
return DriverManager.getConnection(dbUrl, username, password);
8861
}
8962

90-
public static void main(String[] args) throws Exception{
63+
public static void main(String[] args) throws Exception {
9164
Server server = new Server(Integer.valueOf(System.getenv("PORT")));
9265
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
9366
context.setContextPath("/");

0 commit comments

Comments
 (0)