postgresqlspring-datasimplejdbccall

SimpleJdbcCall fails to execute stored procedure in Postgresql


I am getting exception while using SimpleJdbcCall to call the below procedure in postgres 16.4.

Here is my Stored Procedure:

CREATE OR REPLACE PROCEDURE sum_two_numbers(
    IN num1 NUMERIC,
    IN num2 NUMERIC,
    OUT result NUMERIC
)
LANGUAGE plpgsql
AS '
BEGIN
    -- Perform the sum and store the result in the OUT parameter
    result := num1 + num2;
END;
';

Libraries:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>6.0.13</version>
</dependency>
org.postgresql postgresql 42.6.0 runtime

Here is my code:

@Test
void test() {

    SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate);
    simpleJdbcCall.withProcedureName("sum_two_numbers")
        .declareParameters(new SqlParameter("num1", Types.NUMERIC),
            new SqlParameter("num2", Types.NUMERIC),
            new SqlOutParameter("result", Types.NUMERIC))
        .withoutProcedureColumnMetaDataAccess(); // fails even if I remove this


    Map<String, Object> inParams = new HashMap<>();
    inParams.put("num1", 4);        // Parameter names should match the stored procedure parameters
    inParams.put("num2", 5);
    Map<String, Object> resultMap = simpleJdbcCall.execute(inParams);
}

I have tried all options available in SimpleJdbcCall but no success yet.

The Error:

org.springframework.jdbc.BadSqlGrammarException: CallableStatementCallback; bad SQL grammar [{call sum_two_numbers(?, ?, ?)}]

    at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:104)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:82)
    at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1572)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:1207)
    at org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:1246)
    at org.springframework.jdbc.core.simple.AbstractJdbcCall.executeCallInternal(AbstractJdbcCall.java:412)
    at org.springframework.jdbc.core.simple.AbstractJdbcCall.doExecute(AbstractJdbcCall.java:395)
    at org.springframework.jdbc.core.simple.SimpleJdbcCall.execute(SimpleJdbcCall.java:193)
    at com.example.spring.jdbc.callprocedures.CallProceduresIntegrationTest.test(CallProceduresIntegrationTest.java:127)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: org.postgresql.util.PSQLException: ERROR: sum_two_numbers(numeric, numeric) is a procedure
  Hint: To call a procedure, use CALL.
  Position: 15
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrotirResponse(QueryExecutorImpl.java:2713)

The call to the procedure fails no matter what options I have tried so far with SimpleJdbcCall. I thought there is some problem with the parameters so I removed all the parameters from the SP and tried again by removing SQL parameters from the Java code as well. But still, I get the sam error. I have even tried with other versions of POstgres Driver but no luck.


Solution

  • I had to use a parameter in the jdbc URL -jdbc:postgresql://localhost:65172/test?escapeSyntaxCallMode=callIfNoReturn

    The value "callIfNoReturn" makes the driver auto-detect the SQL statement as a procedure or a function. Once, It was set the driver detected the call as a procedure.

    There are two other values related to this parameter, that can referred in this SO question