Skip to content

Commit

Permalink
Properly close resources for call procedure failure
Browse files Browse the repository at this point in the history
  • Loading branch information
niveathika committed Jun 12, 2024
1 parent 1910f27 commit bc83e1a
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 bc83e1a

Please sign in to comment.