As getAll method returns a Stream, we cannot close the involved
resources (Connection, Statement and resultSet) until the stream is closed by the consumer. So try-with-resources is not an option as per sonarqube’s recommendation. But it is still recommended to close statement and result set. When connection pool used, connection is not closed when close() called. It is just returned to the pool. Using //NOSONAR to avoid false blocker issue.
This commit is contained in:
parent
cbba487ff8
commit
a1a4088087
@ -65,8 +65,8 @@ public class DbCustomerDao implements CustomerDao {
|
|||||||
Connection connection;
|
Connection connection;
|
||||||
try {
|
try {
|
||||||
connection = getConnection();
|
connection = getConnection();
|
||||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM CUSTOMERS");
|
PreparedStatement statement = connection.prepareStatement("SELECT * FROM CUSTOMERS"); //NOSONAR
|
||||||
ResultSet resultSet = statement.executeQuery();
|
ResultSet resultSet = statement.executeQuery(); //NOSONAR
|
||||||
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE,
|
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE,
|
||||||
Spliterator.ORDERED) {
|
Spliterator.ORDERED) {
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ public class DbCustomerDao implements CustomerDao {
|
|||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, false).onClose(() -> mutedClose(connection));
|
}, false).onClose(() -> mutedClose(connection, statement, resultSet));
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new Exception(e.getMessage(), e);
|
throw new Exception(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
@ -92,8 +92,10 @@ public class DbCustomerDao implements CustomerDao {
|
|||||||
return dataSource.getConnection();
|
return dataSource.getConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void mutedClose(Connection connection) {
|
private void mutedClose(Connection connection, PreparedStatement statement, ResultSet resultSet) {
|
||||||
try {
|
try {
|
||||||
|
resultSet.close();
|
||||||
|
statement.close();
|
||||||
connection.close();
|
connection.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user