简体   繁体   English

函数在PL / SQL中返回游标 - 在Java端或PL / SQL端关闭它?

[英]Function returning cursor in PL/SQL - close it on Java's side or PL/SQL's side?

I am calling a stored function in PL/SQL using Java that returns a REF CURSOR : 我使用返回REF CURSOR Java调用PL / SQL中的存储函数:

FUNCTION getApprovers RETURN approvers_cursor IS
    approvers approvers_cursor;
BEGIN
    OPEN approvers FOR
        select * from role where role_name = 'APPROVER';
    RETURN approvers;
END;

If I close the cursor before the RETURN approvers 如果我在RETURN approvers之前关闭光标

CLOSE approvers;
RETURN approvers;

I get a warning in Java - Cursor is Closed . 我在Java中收到警告 - Cursor is Closed

It seems that I cannot close the cursor in the PL/SQL stored function. 看来我无法关闭PL / SQL存储函数中的光标。 Where then does the closing of the cursor take place? 那么光标的关闭在哪里发生?

As a matter of fact, ResultSet s are Cursors. 事实上, ResultSet 游标。

 //JAVA                            //PL/SQL
 PreparedStatement ps = con.prepareStatement(
            "select * from role where role_name = ?");
 ps.setString(1, "APPROVER");
 ResultSet rs = ps.executeQuery(); //OPENS the cursor
 while(rs.next()) {                //FETCHES next row from cursor
    //Handle resultset
 }
 rs.close();                       //CLOSES the cursor

Of cause since Java 7 you could do this with try-with-resource and hide the close() calls - but for clarity of what equals what written old-style here 自Java 7以来,你可以通过try-with-resource和隐藏close()调用来实现这一点 - 但是为了清楚起见什么等于在这里写的旧式

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

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