PL/SQL异常和Java程序

发布于 2024-08-25 05:46:15 字数 238 浏览 3 评论 0原文

业务逻辑在 pl/sql 包过程和函数中编码。 Java 程序调用 pl/sql 包过程和函数来完成数据库工作。

每当引发异常时,pl/sql 程序都会将异常存储到 Oracle 表中。

我的java程序如何获得异常,因为异常不是从pl/sql传播到java,而是被持久化到oracle表,并且过程/函数只返回1或0。

抱歉,我应该更早添加这个约束,避免了这种混乱。与许多遗留项目一样,我们没有修改存储过程的自由。

Business logic is coded in pl/sql packages procedures and functions.
Java programs call pl/sql packages procedures and functions to do database work.

pl/sql programs store exceptions into Oracle tables whenever an exception is raised.

How would my java programs get the exceptions since the exception instead of being propagated from pl/sql to java is getting persisted to a oracle table and the procs/functions just return 1 or 0.

Sorry folks i should have added this constraint much earlier and avoided this confusion. As with many legacy projects we don't have the freedom to modify the stored procedures.

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

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

发布评论

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

评论(4

予囚 2024-09-01 05:46:15

假设您无法更改 PLSQL 代码,则必须监视该表。当然,只有当错误表存储某种会话或使用标识符时,这才有效。

Assuming you can't change the PLSQL code, you'll have to monitor the table. And of course, that will only work if the error table stores some sort of session or use identifier.

终难遇 2024-09-01 05:46:15

java.sql.CallableStatement 抛出 java.sql.SQLException。如果您的存储过程抛出异常,您的 Java 代码将会知道它。调用存储过程的代码必须处理此异常。

您可以选择:您可以让存储过程抛出异常,也可以让 Java 代码检查返回值,并在返回错误代码时在表中查询异常。

但无论如何,例外都不会“丢失”。您可以从 JVM 或表中获取它。

我投票支持 JVM 因为它更容易。更少的 PL/SQL 代码,更少的 Java 代码。

java.sql.CallableStatement throws java.sql.SQLException. If your stored proc throws an exception, your Java code will know about it. The code that calls the stored proc will have to handle this exception.

You have a choice: you can either have the stored proc throw the exception or have the Java code check the return value and query the table for the exception if the error code is returned.

But the exception isn't "lost" either way. You get it from the JVM or the table.

I vote for the JVM because it's easier. Less PL/SQL code, less Java code.

揽月 2024-09-01 05:46:15

“正在持久化到 oracle 表”
您可以在该表上创建一个拒绝插入的触发器。例如,如果错误表包含“ERROR_DESCRIPTION”列,则可以有一个 BEFORE INSERT ON error_table FOR EACH ROW 触发器,该触发器在

PL/SQL 代码记录错误时 执行 RAISE_APPLICATION_ERROR(-20001,:NEW.ERROR_DESCRIPTION) ,这将因替换错误而失败,如果幸运的话,这将传播到 Java 层。

这是一个丑陋的黑客,但如果你确实无法更改代码,它可能会起作用。

"is getting persisted to a oracle table"
You could create a trigger on that table that rejects the insert. For example, if the error table contains an 'ERROR_DESCRIPTION' column, you could have a BEFORE INSERT ON error_table FOR EACH ROW trigger which does a RAISE_APPLICATION_ERROR(-20001,:NEW.ERROR_DESCRIPTION)

When the PL/SQL code goes to log the error, that will fail with the replacement error and that will, if you are lucky, get propogated to the Java layer.

It is an ugly hack, but if you truly can't change the code, it may work.

断念 2024-09-01 05:46:15

简而言之,如果您使用支持方面的框架,那么创建一个方面来检查相应表中的异常就会很容易。如果没有,那么您可以编写类似于此代码的内容:

        ResultSet exRs = null;      

        try {
            connection.setAutoCommit(false);
            Statement statement = connection.createStatement();
            statement.execute(normalSql);
            exRs = statement.executeQuery(exceptionSql);
            exRs.next();
        } catch (SQLException e) {
            e.printStackTrace();
            connection.rollback();
        }

        if (null != exRs.getString(exceptionColumn))
            connection.commit();
        else
            connection.rollback();

抱歉,我无法更具体。

Simply if you use a framework that supports aspects, it would be easy to make an aspect that checks for the exception in the appropriate table. If not, then you could write something similar to this code:

        ResultSet exRs = null;      

        try {
            connection.setAutoCommit(false);
            Statement statement = connection.createStatement();
            statement.execute(normalSql);
            exRs = statement.executeQuery(exceptionSql);
            exRs.next();
        } catch (SQLException e) {
            e.printStackTrace();
            connection.rollback();
        }

        if (null != exRs.getString(exceptionColumn))
            connection.commit();
        else
            connection.rollback();

Sorry I couldn't be more specific.

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