无法调用 Java 中的存储过程

Can't call stored procedure in Java

我在PostgreSQL中写了一个stored procedure。我尝试在 java 中使用此过程,它抛出“org.postgresql.util.PSQLException: ERROR: cursor " < unnamed portal 1 > " does not exist”异常。

程序:

CREATE OR REPLACE FUNCTION subject_show(session_id CHARACTER VARYING,OUT result_cursor refcursor, OUT total_record INTEGER, OUT total_search_record INTEGER  ) AS $$
        BEGIN
            total_record:=23;
            total_search_record:=22;
            OPEN result_cursor FOR SELECT "ID","NAME"   FROM "SUBJECTS" ;
    END;
    $$ LANGUAGE plpgsql;

调用程序,在java

...

callableStatement = conn.prepareCall("{ call subject_show(?,?,?,?) }");
callableStatement.setString(1, sessionID);
callableStatement.registerOutParameter(2, Types.REF);
callableStatement.registerOutParameter(3, Types.INTEGER);
callableStatement.registerOutParameter(4, Types.INTEGER);
callableStatement.executeUpdate();
System.out.println(callableStatement.getObject(3));
System.out.println(callableStatement.getObject(4));
rs = (ResultSet) callableStatement.getObject(2);
...

我发现, 如果您关闭了自动提交,我只能这样做。游标是 只在事务中有效,因此一旦驱动程序提交 游标不再有效。因此我收到错误。

我通过在函数中添加@Transactional 解决了这个问题