Skip to content

Commit c3ecb25

Browse files
committed
Fix JDBC resource leaks
1 parent e28da29 commit c3ecb25

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

src/main/java/com/wildcodeschool/wildandwizard/repository/WizardRepository.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
package com.wildcodeschool.wildandwizard.repository;
22

3+
import java.sql.Connection;
4+
import java.sql.Date;
5+
import java.sql.DriverManager;
6+
import java.sql.PreparedStatement;
7+
import java.sql.ResultSet;
8+
import java.sql.SQLException;
9+
import java.sql.Statement;
10+
311
import com.wildcodeschool.wildandwizard.entity.Wizard;
412

5-
import java.sql.*;
13+
import util.JdbcUtils;
614

715
public class WizardRepository {
816

@@ -13,11 +21,14 @@ public class WizardRepository {
1321
public Wizard save(String firstName, String lastName, Date birthday,
1422
String birthPlace, String biography, boolean muggle) {
1523

24+
Connection connection = null;
25+
PreparedStatement statement = null;
26+
ResultSet generatedKeys = null;
1627
try {
17-
Connection connection = DriverManager.getConnection(
28+
connection = DriverManager.getConnection(
1829
DB_URL, DB_USER, DB_PASSWORD
1930
);
20-
PreparedStatement statement = connection.prepareStatement(
31+
statement = connection.prepareStatement(
2132
"INSERT INTO wizard (first_name, last_name, birthday, birth_place, biography, is_muggle) VALUES (?, ?, ?, ?, ?, ?)",
2233
Statement.RETURN_GENERATED_KEYS
2334
);
@@ -32,7 +43,7 @@ public Wizard save(String firstName, String lastName, Date birthday,
3243
throw new SQLException("failed to insert data");
3344
}
3445

35-
ResultSet generatedKeys = statement.getGeneratedKeys();
46+
generatedKeys = statement.getGeneratedKeys();
3647

3748
if (generatedKeys.next()) {
3849
Long id = generatedKeys.getLong(1);
@@ -43,6 +54,10 @@ public Wizard save(String firstName, String lastName, Date birthday,
4354
}
4455
} catch (SQLException e) {
4556
e.printStackTrace();
57+
} finally {
58+
JdbcUtils.closeResultSet(generatedKeys);
59+
JdbcUtils.closeStatement(statement);
60+
JdbcUtils.closeConnection(connection);
4661
}
4762
return null;
4863
}

src/main/java/util/JdbcUtils.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package util;
2+
3+
import java.sql.Connection;
4+
import java.sql.ResultSet;
5+
import java.sql.SQLException;
6+
import java.sql.Statement;
7+
8+
/**
9+
* Utility methods for closing JDBC resources.
10+
*
11+
* https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jdbc/support/JdbcUtils.html
12+
* https://www.selikoff.net/2008/07/30/finally-closing-jdbc-resources/
13+
*/
14+
public class JdbcUtils {
15+
16+
public static void closeConnection(Connection con) {
17+
if (con != null) {
18+
try {
19+
con.close();
20+
} catch (SQLException e) {
21+
e.printStackTrace();
22+
}
23+
}
24+
}
25+
26+
public static void closeResultSet(ResultSet rs) {
27+
if (rs != null) {
28+
try {
29+
rs.close();
30+
} catch (SQLException e) {
31+
e.printStackTrace();
32+
}
33+
}
34+
}
35+
36+
public static void closeStatement(Statement stmt) {
37+
if (stmt != null) {
38+
try {
39+
stmt.close();
40+
} catch (SQLException e) {
41+
e.printStackTrace();
42+
}
43+
}
44+
}
45+
46+
}

0 commit comments

Comments
 (0)