I am trying to learn how to call stored procedure from springboot data jpa repository and returns a cursor from stored procedure. While I am implementing I am getting error like org.springframework.dao.InvalidDataAccessApiUsageException: You're trying to execute a @Procedure method without a surrounding transaction that keeps the connection open so that the ResultSet can actually be consumed; Make sure the consumer code uses @Transactional or any other way of declaring a (read-only) transaction] with root cause
My Stored Procedure
CREATE OR REPLACE PROCEDURE public.get_user_cursor(OUT user_cursor refcursor)
LANGUAGE 'plpgsql'
AS $BODY$
DECLARE
user_cursor refcursor;
BEGIN
OPEN user_cursor FOR SELECT nuser_id FROM users;
END;
$BODY$;
Repository Code
@Transactional(readOnly = true)
@Procedure(name = "Users.get_user_cursor")
List<Users> get_user_cursor();
Users.class
@NamedStoredProcedureQuery(
name = "Users.get_user_cursor",
procedureName = "get_user_cursor",
resultClasses = Users.class,
parameters = {
@StoredProcedureParameter(mode = ParameterMode.REF_CURSOR, name = "user_cursor", type = void.class)
}
)
@Table(name = "users")
public class Users implements Serializable {
// Model Codes
}
Even I tried both library import org.springframework.transaction.annotation.Transactional; and import jakarta.transaction.Transactional; and for just @transactional and with readOnly=true. While running application is getting starting without error. But when I calls the API end point, error propogating.
Can anyone suggest where I went wrong in implementation and how can I resolve this error please?
and returns a cursorYou are aware that this is pretty unusual? Why do you think that you need this?function, not aprocedure.