通过数据库链接获取 ResultSet/RefCursor

发布于 2024-08-27 08:20:29 字数 1432 浏览 5 评论 0原文

通过 dblink 调用存储过程的答案来看,似乎如果您通过远程数据库链接进行 SP 调用,则无法调用存储过程并取回 ResultSet/RefCursor。我们也在使用 Oracle 10g。

我们可以通过链接成功获取单值结果,并且可以成功调用 SP 并在本地获取结果,但从远程 DB 读取 ResultSet 时,我们会得到相同的“ORA-24338:语句句柄未执行”错误。

我的问题 - 使用存储过程有什么解决方法吗?共享视图是更好的解决方案吗?管道行?

示例存储过程:

CREATE OR REPLACE PACKAGE BODY example_SP
IS

  PROCEDURE get_terminals(p_CD_community   IN  community.CD_community%TYPE,
                          p_cursor         OUT SYS_REFCURSOR)
  IS
  BEGIN
    OPEN p_cursor FOR
    SELECT cd_terminal
    FROM terminal t, community c
    WHERE c.cd_community = p_CD_community
    AND t.id_community = c.id_community;
  END;

END example_SP;
/

在本地运行但不能远程运行的 Java 代码示例:

 Connection conn = DBConnectionManagerFactory.getDBConnectionManager().getConnection();
    CallableStatement cstmt = null;
    ResultSet rs = null;
    String community = "EXAMPLE";

    try
    {
        cstmt = conn.prepareCall("{call example_SP.get_terminals@remote_address(?,?)}");
        cstmt.setString(1, community);
        cstmt.registerOutParameter(2, OracleTypes.CURSOR);

        cstmt.execute();

        rs = (ResultSet)cstmt.getObject(2);

        while (rs.next())
        {
              LogUtil.getLog().logInfo("Terminal code=" + rs.getString( "cd_terminal" ));
        }

    }

From the answers to calling a stored proc over a dblink it seems that it is not possible to call a stored procedure and get the ResultSet/RefCursor back if you are making the SP call across a remote DB link. We are also using Oracle 10g.

We can successfully get single value results across the link, and can successfully call the SP and get the results locally but we get the same 'ORA-24338: statement handle not executed' error when reading the ResultSet from the remote DB.

My question - is there any workaround to using the stored procedure? Is a shared view a better solution? Piped rows?

Sample Stored Procedure:

CREATE OR REPLACE PACKAGE BODY example_SP
IS

  PROCEDURE get_terminals(p_CD_community   IN  community.CD_community%TYPE,
                          p_cursor         OUT SYS_REFCURSOR)
  IS
  BEGIN
    OPEN p_cursor FOR
    SELECT cd_terminal
    FROM terminal t, community c
    WHERE c.cd_community = p_CD_community
    AND t.id_community = c.id_community;
  END;

END example_SP;
/

Sample Java code that works locally but not remotely:

 Connection conn = DBConnectionManagerFactory.getDBConnectionManager().getConnection();
    CallableStatement cstmt = null;
    ResultSet rs = null;
    String community = "EXAMPLE";

    try
    {
        cstmt = conn.prepareCall("{call example_SP.get_terminals@remote_address(?,?)}");
        cstmt.setString(1, community);
        cstmt.registerOutParameter(2, OracleTypes.CURSOR);

        cstmt.execute();

        rs = (ResultSet)cstmt.getObject(2);

        while (rs.next())
        {
              LogUtil.getLog().logInfo("Terminal code=" + rs.getString( "cd_terminal" ));
        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任性一次 2024-09-03 08:20:29

选项 1. 从 Java 直接连接到远程数据库,而不是通过本地数据库。更简单,但由应用程序来协调两个单独的事务。如果一个数据库仅用于读取而不是写入,我会走这条路。

您可以使用直接查询或存储过程和引用游标。我通常会选择前者,除非有充分的理由添加存储过程层。

选项2.使用数据库链接直接查询本地数据库。

选项 3. 与 (2) 相同,但将查询隐藏在本地数据库上存储的视图(或同义词)中。

选项 4。如果结果集足够小,您可以让本地数据库上的过程调用远程数据库上的过程。远程过程将以 XML 或结构化 CLOB(例如 JSON)的形式返回结果,该结果可以由本地过程或 java 层“解码”。

Option 1. Go for a direct connection from Java to the remote database rather than going through the local database. Simpler, but it is up to the application to co-ordinate the two separate transactions. If one database is just used for reads and not writes, I'd go this route.

You can use with a straight query or a stored procedure and ref cursor. I'd generally go with the former unless there is a good reason to add in a stored procedure layer.

Option 2. Go for a direct query in the local database using the database link.

Option 3. As (2), but hide the query in a view (or synonym) stored on the local database.

Option 4. If the result set is small enough, you could have a procedure on the local database call a procedure on the remote database. The remote procedure would return the result as XML or a structured CLOB (eg JSON) which could be 'decoded' by either the local procedure or the java layer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文