简体   繁体   English

无法调用 Java 中的存储过程

[英]Can't call stored procedure in Java

I write a stored procedure in PostgreSQL .我在PostgreSQL写了一个stored procedure I try to use, this procedure in java, it throw ' org.postgresql.util.PSQLException: ERROR: cursor " < unnamed portal 1 > " does not exist ' exception.我尝试在 java 中使用此过程,它抛出 ' org.postgresql.util.PSQLException: ERROR: cursor " < unnamed portal 1 > " does not exist ' 异常。

procedure:程序:

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;

Call procedure, in java :调用程序,在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);
...

I found that, I can only do this if you have autocommit turned off. 我发现,只有在您关闭自动提交功能后,我才能这样做。 Cursors are only valid within a transaction , thus as soon as the driver commits the cursor is no longer valid. 游标仅在一个事务中有效,因此一旦驱动程序提交,游标就不再有效。 Thus the error I am receiving. 因此,我收到的错误。

I solved this in my code by adding @Transactional to the function我通过将 @Transactional 添加到 function 在我的代码中解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM