Possible defect: Java methods as stored procs run thrice when ran once

44 views
Skip to first unread message

virimchi m

unread,
Mar 21, 2025, 9:35:33 AMMar 21
to H2 Database
I have a Java static method defined as a stored proc 

When I run it through Spring JdbcTemplate,
JdbcTemplate's createCallableStatememt calls  H2 internal org.h2.command.dml.Call.prepare method.

This inturn calls "ResultInterface result = tableFunction.getValueTemplate(session);"

This leads to org.h2.expression.function.table.JavaTableFunction.getValueTemplate call

This leads to org.h2.schema.FunctionAlias.getTableValue

this in turn calls "execute" method

In this method, at line 495, it is invoking the stored procedure with null arguments


Subsequently, when jdbcTemplate calls action.doInCallableStatement(cs) , the java static method stored proc gets executed with actual arguments!!



Then it gets executed again with null arguments for the third time
This is the actual "execute" call from JdbcPreparedStatement clas.

Why does prepare call end up in excute call?


Evgenij Ryazanov

unread,
Mar 21, 2025, 10:26:29 PMMar 21
to H2 Database
Hello!

Take a look on Using a Function as a Table subsection in documentation, here is a link to User-Defined Functions and Stored Procedures section:
https://h2database.com/html/features.html#user_defined_functions
The mentioned subsection is below, unfortunately, it doesn't have an own anchor.

It describes how such functions need to be implemented.

They should have java.sql.Connection as their first parameter, and when passed connection has URL "jdbc:columnlist:connection", they don't need to return an actual data, they only need to provide an empty result set with properly configured columns. H2 needs to know names and data types of table columns during compilation of a query.

virimchi m

unread,
Mar 24, 2025, 8:15:49 AMMar 24
to H2 Database
Thank you for pointing me to the documentation.

I have spring Jdbc code that calls SQL server stored procs.
In order to improve the code coverage, I am writing unit tests that use H2 in-memory db and create H2 Stored procs that have same signature as the SQL server stored procs.

Passing Connection object is not feasible for me. Is there another option for this scenario?
Ex: I specify two methods in the alias declaration - one to get the ResultSet column definitions and the other is the actual call.

thank you
Manas

Reply all
Reply to author
Forward
0 new messages