Skip to content

Commit

Permalink
Merge pull request #715 from ballerina-platform/patch
Browse files Browse the repository at this point in the history
Properly close resources for call procedure failure
  • Loading branch information
niveathika authored Jun 12, 2024
2 parents c7cf7b6 + 3b9f736 commit b1c955f
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b1c955f

Please sign in to comment.