Skip to content

Commit adbc457

Browse files
Refactor resource handling with try-with-resources.
Encapsulated JDBC resource management in try-with-resources blocks to ensure proper closing of connections, statements, and result sets. This improves code safety and prevents potential resource leaks.
1 parent 485c31b commit adbc457

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/main/java/fr/abes/sudoc/component/BaseXmlFunctionsCaller.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,47 +32,47 @@ public BaseXmlFunctionsCaller(DataSource dataSource) {
3232
public List<String> issnToPpn(String issn) throws UncategorizedSQLException, SQLException {
3333
List<String> resultList = new ArrayList<>();
3434

35-
Connection connection = dataSource.getConnection();
36-
PreparedStatement ps = connection.prepareStatement(requestIssn);
35+
try (Connection connection = dataSource.getConnection();
36+
PreparedStatement ps = connection.prepareStatement(requestIssn)) {
3737

38-
ps.setString(1, issn);
38+
ps.setString(1, issn);
3939

40-
ResultSet rs = ps.executeQuery();
41-
while (rs.next()) {
42-
resultList.add(rs.getString("ppn"));
40+
ResultSet rs = ps.executeQuery();
41+
while (rs.next()) {
42+
resultList.add(rs.getString("ppn"));
43+
}
4344
}
44-
connection.close();
4545
return resultList;
4646
}
4747

4848

4949
public String isbnToPpn(String isbn) throws UncategorizedSQLException, SQLException {
5050
String result = null;
51-
Connection connection = dataSource.getConnection();
52-
PreparedStatement ps = connection.prepareStatement(requestIsbn);
51+
try (Connection connection = dataSource.getConnection();
52+
PreparedStatement ps = connection.prepareStatement(requestIsbn)) {
5353

54-
ps.setString(1, isbn);
54+
ps.setString(1, isbn);
5555

56-
ResultSet rs = ps.executeQuery();
57-
if (rs.next()) {
58-
result = rs.getString("ppn");
56+
ResultSet rs = ps.executeQuery();
57+
if (rs.next()) {
58+
result = rs.getString("ppn");
59+
}
5960
}
60-
connection.close();
6161
return result;
6262
}
6363

6464

6565
public List<String> doiToPpn(String doi) throws UncategorizedSQLException, SQLException {
6666
List<String> resultList = new ArrayList<>();
67-
Connection connection = dataSource.getConnection();
68-
PreparedStatement ps = connection.prepareStatement(requestDoi);
69-
ps.setString(1, doi.toLowerCase());
67+
try (Connection connection = dataSource.getConnection();
68+
PreparedStatement ps = connection.prepareStatement(requestDoi)) {
69+
ps.setString(1, doi.toLowerCase());
7070

71-
ResultSet rs = ps.executeQuery();
72-
while (rs.next()) {
73-
resultList.add(rs.getString("ppn"));
71+
ResultSet rs = ps.executeQuery();
72+
while (rs.next()) {
73+
resultList.add(rs.getString("ppn"));
74+
}
7475
}
75-
connection.close();
7676
return resultList;
7777
}
7878

0 commit comments

Comments
 (0)