From 79d49a0512b8d503367b091c792886f9d079e4b2 Mon Sep 17 00:00:00 2001 From: prathabanKavin Date: Wed, 31 May 2023 12:09:53 +0530 Subject: [PATCH] used try with resources --- .../impl/event/SQLServerEventConfigDAOImpl.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 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/dao/impl/event/SQLServerEventConfigDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/event/SQLServerEventConfigDAOImpl.java index 0a816cea05..8eedceb4d9 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/event/SQLServerEventConfigDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/event/SQLServerEventConfigDAOImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * Copyright (c) 2018-2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. * * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except @@ -51,8 +51,8 @@ public class SQLServerEventConfigDAOImpl extends AbstractEventConfigDAO { "CREATED_TIMESTAMP, " + "TENANT_ID) " + "VALUES (?, ?, ?, ?, ?)"; + List generatedIds = new ArrayList<>(); try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { - List generatedIds = new ArrayList<>(); for (EventConfig eventConfig : eventConfigList) { stmt.setString(1, eventConfig.getEventSource()); stmt.setString(2, eventConfig.getEventLogic()); @@ -60,13 +60,15 @@ public class SQLServerEventConfigDAOImpl extends AbstractEventConfigDAO { stmt.setTimestamp(4, timestamp); stmt.setInt(5, tenantId); stmt.executeUpdate(); - ResultSet generatedKeys = stmt.getGeneratedKeys(); - if (generatedKeys.next()) { - generatedIds.add(generatedKeys.getInt(1)); + + try (ResultSet generatedKeys = stmt.getGeneratedKeys()) { + if (generatedKeys.next()) { + generatedIds.add(generatedKeys.getInt(1)); + } } } - return generatedIds; } + return generatedIds; } catch (SQLException e) { String msg = "Error occurred while creating event configurations for the tenant id " + tenantId; log.error(msg, e);