diff --git a/snowflakedb_queries/src/main/java/com/testsigma/addons/util/FetchResponse.java b/snowflakedb_queries/src/main/java/com/testsigma/addons/util/FetchResponse.java index bf8e374..6bcb21d 100644 --- a/snowflakedb_queries/src/main/java/com/testsigma/addons/util/FetchResponse.java +++ b/snowflakedb_queries/src/main/java/com/testsigma/addons/util/FetchResponse.java @@ -13,12 +13,12 @@ public class FetchResponse { public static ResponseData execute(String url, String query, Logger logger) throws Exception { StringBuilder resultStringBuilder = new StringBuilder(); String value = ""; + Statement statement = null; + ResultSet resultSet = null; try { - - Statement statement = SnowflakeDBConnection.getConnection(url); + statement = SnowflakeDBConnection.getConnection(url); logger.info("Successfully set JDBC_QUERY_RESULT_FORMAT='JSON'"); logger.info("Executing query: " + query); - ResultSet resultSet; resultSet = statement.executeQuery(query); resultStringBuilder = new StringBuilder(); if (resultSet.next()) { @@ -39,6 +39,8 @@ public static ResponseData execute(String url, String query, Logger logger) thro resultSet.close(); return new ResponseData(resultStringBuilder.toString(), value); } catch (Exception e) { + Objects.requireNonNull(statement).close(); + Objects.requireNonNull(resultSet).close(); String errorMessage = ExceptionUtils.getStackTrace(e); logger.info(errorMessage); throw new Exception("Error occurred while executing the given query: " + errorMessage); diff --git a/snowflakedb_queries/src/main/java/com/testsigma/addons/util/FetchResponseData.java b/snowflakedb_queries/src/main/java/com/testsigma/addons/util/FetchResponseData.java index 18efc5b..4dc2f81 100644 --- a/snowflakedb_queries/src/main/java/com/testsigma/addons/util/FetchResponseData.java +++ b/snowflakedb_queries/src/main/java/com/testsigma/addons/util/FetchResponseData.java @@ -5,15 +5,18 @@ import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; +import java.util.Objects; public class FetchResponseData { public static String execute(String url, String query, Logger logger) throws Exception { + Statement statement = null; + ResultSet resultSet = null; try{ StringBuilder resultStringBuilder = null; - Statement statement = SnowflakeDBConnection.getConnection(url); + statement = SnowflakeDBConnection.getConnection(url); logger.info("Successfully set JDBC_QUERY_RESULT_FORMAT='JSON'"); logger.info("Executing query: " + query); - ResultSet resultSet = statement.executeQuery(query); + resultSet = statement.executeQuery(query); resultStringBuilder = new StringBuilder(); ResultSetMetaData metaData = resultSet.getMetaData(); int columnCount = metaData.getColumnCount(); @@ -42,6 +45,8 @@ public static String execute(String url, String query, Logger logger) throws Exc return resultStringBuilder.toString(); } catch (Exception e){ + Objects.requireNonNull(statement).close(); + Objects.requireNonNull(resultSet).close(); throw new Exception(e); } }