From 3b9f736c8a85a220db341149c3117a3b07e851bf Mon Sep 17 00:00:00 2001 From: Niveathika Date: Wed, 12 Jun 2024 08:24:34 +0530 Subject: [PATCH] Properly close resources for call procedure failure --- .../ballerina/stdlib/sql/nativeimpl/CallProcessor.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/CallProcessor.java b/native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/CallProcessor.java index 1ec68153..a539fc9b 100644 --- a/native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/CallProcessor.java +++ b/native/src/main/java/io/ballerina/stdlib/sql/nativeimpl/CallProcessor.java @@ -63,6 +63,7 @@ import static io.ballerina.stdlib.sql.utils.Utils.getDefaultStreamConstraint; import static io.ballerina.stdlib.sql.utils.Utils.getSqlQuery; import static io.ballerina.stdlib.sql.utils.Utils.updateProcedureCallExecutionResult; +import static io.ballerina.stdlib.sql.utils.Utils.closeResources; /** * This class holds the utility methods involved with executing the call statements. @@ -116,9 +117,9 @@ private static Object nativeCallExecutable(BObject client, BObject paramSQLStrin return ErrorGenerator.getSQLApplicationError( "SQL Client is already closed, hence further operations are not allowed"); } - Connection connection; - CallableStatement statement; - ResultSet resultSet; + Connection connection = null; + CallableStatement statement = null; + ResultSet resultSet = null; String sqlQuery = null; try { sqlQuery = getSqlQuery(paramSQLString); @@ -168,11 +169,14 @@ private static Object nativeCallExecutable(BObject client, BObject paramSQLStrin procedureCallResult.addNativeData(RESULT_SET_COUNT_NATIVE_DATA_FIELD, resultSetCount); return procedureCallResult; } catch (SQLException e) { + closeResources(isWithinTrxBlock, resultSet, statement, connection); return ErrorGenerator.getSQLDatabaseError(e, String.format("Error while executing SQL query: %s. ", sqlQuery)); } catch (ApplicationError e) { + closeResources(isWithinTrxBlock, resultSet, statement, connection); return ErrorGenerator.getSQLApplicationError(e); } catch (Throwable th) { + closeResources(isWithinTrxBlock, resultSet, statement, connection); return ErrorGenerator.getSQLError(th, String.format("Error while executing SQL query: %s. ", sqlQuery)); } } else {