From a1a40880873bc40f977a3cef7d5b9f45c70c9312 Mon Sep 17 00:00:00 2001 From: "mahendran.mookkiah" Date: Sun, 13 Aug 2017 10:19:48 -0400 Subject: [PATCH] =?UTF-8?q?As=20getAll=20method=20returns=20a=20Stream,=20?= =?UTF-8?q?we=20cannot=20close=20the=20involved=20resources=20(Connection,?= =?UTF-8?q?=20Statement=20and=20resultSet)=20until=20the=20stream=20is=20c?= =?UTF-8?q?losed=20by=20the=20consumer.=20So=20try-with-resources=20is=20n?= =?UTF-8?q?ot=20an=20option=20as=20per=20sonarqube=E2=80=99s=20recommendat?= =?UTF-8?q?ion.=20But=20it=20is=20still=20recommended=20to=20close=20state?= =?UTF-8?q?ment=20and=20result=20set.=20When=20connection=20pool=20used,?= =?UTF-8?q?=20connection=20is=20not=20closed=20when=20close()=20called.=20?= =?UTF-8?q?It=20is=20just=20returned=20to=20the=20pool.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using //NOSONAR to avoid false blocker issue. --- dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java b/dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java index c5dc7da3f..6e93207cc 100644 --- a/dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java +++ b/dao/src/main/java/com/iluwatar/dao/DbCustomerDao.java @@ -65,8 +65,8 @@ public class DbCustomerDao implements CustomerDao { Connection connection; try { connection = getConnection(); - PreparedStatement statement = connection.prepareStatement("SELECT * FROM CUSTOMERS"); - ResultSet resultSet = statement.executeQuery(); + PreparedStatement statement = connection.prepareStatement("SELECT * FROM CUSTOMERS"); //NOSONAR + ResultSet resultSet = statement.executeQuery(); //NOSONAR return StreamSupport.stream(new Spliterators.AbstractSpliterator(Long.MAX_VALUE, Spliterator.ORDERED) { @@ -82,7 +82,7 @@ public class DbCustomerDao implements CustomerDao { throw new RuntimeException(e); } } - }, false).onClose(() -> mutedClose(connection)); + }, false).onClose(() -> mutedClose(connection, statement, resultSet)); } catch (SQLException e) { throw new Exception(e.getMessage(), e); } @@ -92,8 +92,10 @@ public class DbCustomerDao implements CustomerDao { return dataSource.getConnection(); } - private void mutedClose(Connection connection) { + private void mutedClose(Connection connection, PreparedStatement statement, ResultSet resultSet) { try { + resultSet.close(); + statement.close(); connection.close(); } catch (SQLException e) { e.printStackTrace();