From 87b98a6cb55719eb0b7b408243d899e22bccce43 Mon Sep 17 00:00:00 2001 From: pasindu Date: Tue, 3 Sep 2024 09:46:05 +0530 Subject: [PATCH] Use try with resources for dao layer --- .../mgt/dao/impl/GenericOperationDAOImpl.java | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index fa4b5c4427..3df2f41aac 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -130,21 +130,21 @@ public class GenericOperationDAOImpl implements OperationDAO { public int updateOperationByDeviceTypeAndInitialStatus(String deiceType, String initialStatus, String requiredStatus) throws OperationManagementDAOException { - PreparedStatement stmt = null; int numOfRecordsUpdated; - try { - long time = DeviceManagementDAOUtil.getCurrentUTCTime(); - Connection connection = OperationManagementDAOFactory.getConnection(); + long time = DeviceManagementDAOUtil.getCurrentUTCTime(); - String sql = "UPDATE DM_ENROLMENT_OP_MAPPING SET STATUS=?, UPDATED_TIMESTAMP=? WHERE DEVICE_TYPE=?"; + String sql = "UPDATE DM_ENROLMENT_OP_MAPPING SET STATUS=?, UPDATED_TIMESTAMP=? WHERE DEVICE_TYPE=?"; - if (initialStatus == null) { - sql += " AND STATUS IS NULL"; - } else { - sql += " AND STATUS=?"; - } + if (initialStatus == null) { + sql += " AND STATUS IS NULL"; + } else { + sql += " AND STATUS=?"; + } - stmt = connection.prepareStatement(sql); + try ( + Connection connection = OperationManagementDAOFactory.getConnection(); + PreparedStatement stmt = connection.prepareStatement(sql) + ) { stmt.setString(1, requiredStatus); stmt.setLong(2, time); stmt.setString(3, deiceType); @@ -156,8 +156,6 @@ public class GenericOperationDAOImpl implements OperationDAO { } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while update device mapping operation status " + e.getMessage(), e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt); } return numOfRecordsUpdated; }