Oracle 存储过程中记录错误消息

发布于 2024-08-08 15:47:07 字数 103 浏览 5 评论 0原文

我们计划使用 Oracle DBMS 调度程序包将存储过程配置为每天作为批处理作业运行。我们想知道发生错误时记录错误消息的最佳方法是什么。是否可以选择记录到临时表?或者有更好的选择。提前致谢。

We plan to configure a stored procedure to run as a batch job daily using Oracle DBMS scheduler package. We would like to know what would be the best way to log an error message when there is an error occured. Is logging to a temporary table an option? or is there a better option. Thanks in advance.

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

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

发布评论

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

评论(4

女中豪杰 2024-08-15 15:47:07

登录临时表可能是一个很好的解决方案。例如,您可以创建一个简单的日志记录表,如下所示:

create table PROCESSING_LOG(
    MESSAGE_DATE timestamp,
    MESSAGE_TEXT varchar2(4000)
); 

如果您决定滚动自己的日志记录并登录到表中,您可能会选择自治事务路线。

自治事务是可以独立于您所在的当前事务而提交的事务。

这样您就可以将所需的所有信息记录并提交到日志表中,而与存储过程或批处理父事务的成功或失败无关。

CREATE OR REPLACE PROCEDURE "SP_LOG" (
    P_MESSAGE_TEXT VARCHAR2
) IS
  pragma autonomous_transaction;
BEGIN

    DBMS_OUTPUT.PUT_LINE(P_MESSAGE_TEXT);

    INSERT INTO PROCESSING_LOG (
        MESSAGE_DATE,
        MESSAGE_TEXT
    ) VALUES (
        SYSDATE,
        P_MESSAGE_TEXT
    );
    COMMIT;

END;
/

然后,如果您像这样调用它,即使出现故障,您仍然可以获得提交到日志表的消息并回滚事务:

BEGIN
  SP_LOG('Starting task 1 of 2');
  
  ... code for task 1 ...

  SP_LOG('Starting task 2 of 2');

  ... code for task 2 ...

  SP_LOG('Ending Tasks');

  ... determine success or failure of process and commit or rollback ... 

 ROLLBACK;
END;
/

您可能希望使用对您的代码有意义的异常来整理它,但这就是一般想法是,调用 SP_LOG 时写入的数据会持续存在,但父事务仍然可以回滚。

Logging into a temporary table can be a good solution. For example, you could create a simple logging table like this:

create table PROCESSING_LOG(
    MESSAGE_DATE timestamp,
    MESSAGE_TEXT varchar2(4000)
); 

If you decide to roll your own logging and log into a table you might go the Autonomous Transaction route.

An Autonomous Transaction is a transaction that can be commited independently of the current transaction you are in.

That way you can log and commit all the info you want to your log table independently of the success or failure of your stored procedure or batch process parent transaction.

CREATE OR REPLACE PROCEDURE "SP_LOG" (
    P_MESSAGE_TEXT VARCHAR2
) IS
  pragma autonomous_transaction;
BEGIN

    DBMS_OUTPUT.PUT_LINE(P_MESSAGE_TEXT);

    INSERT INTO PROCESSING_LOG (
        MESSAGE_DATE,
        MESSAGE_TEXT
    ) VALUES (
        SYSDATE,
        P_MESSAGE_TEXT
    );
    COMMIT;

END;
/

Then if you call it like this, you can still get messages committed to your log table even if you have a failure and roll back your transaction:

BEGIN
  SP_LOG('Starting task 1 of 2');
  
  ... code for task 1 ...

  SP_LOG('Starting task 2 of 2');

  ... code for task 2 ...

  SP_LOG('Ending Tasks');

  ... determine success or failure of process and commit or rollback ... 

 ROLLBACK;
END;
/

You may want to tidy it up with exceptions that make sense for your code, but that is the general idea, the data written in the calls to SP_LOG persists, but the parent transaction can still be rolled back.

草莓酥 2024-08-15 15:47:07

您说您对数据库环境没有太多控制权来安装日志记录包 - 如果是这种情况,那么您将只能查询 DBA_SCHEDULER_JOB_RUN_DETAILS中的信息DBA_SCHEDULER_JOB_LOG 系统视图 - 您可以在此处查看执行历史记录(或在当前用户的相应视图ALL_SCHEDULER_JOB_RUN_DETAILSALL_SCHEDULER_JOB_LOG 中)。

未处理的异常将显示在 ADDITIONAL_INFO 列中。如果您需要通知,您可以轮询这些视图并生成电子邮件。

Oracle 19 文档:

You say that you don't have a lot of control over the DB environment to install logging packages - if this is the case then you'll be limited to querying the information in the DBA_SCHEDULER_JOB_RUN_DETAILS and DBA_SCHEDULER_JOB_LOG system views - you'll be able to see the history of executions here (or in the corresponding views for the current user, ALL_SCHEDULER_JOB_RUN_DETAILS and ALL_SCHEDULER_JOB_LOG).

Unhandled exceptions will show up in the ADDITIONAL_INFO column. If you need notification you can poll these views and generate email.

Documentation for Oracle 19:

请持续率性 2024-08-15 15:47:07

您可以使用 log4plsql http://log4plsql.sourceforge.net/ 并稍后通过配置更改来更改选择,而不是代码更改

log4plsql 页面给出了它可以记录的各个位置的列表。

它还取决于如何在您的环境中监视应用程序和系统 - 如果有一个标准方法,例如我工作的企业添加使用 irc 进行监视 - 那么您可能需要一个调用该方法的函数。

You could use log4plsql http://log4plsql.sourceforge.net/and change the choice later by configuration changes not code changes

The log4plsql page gives a list of various places it can log.

It also depends how applications and systems are monitored in your environment - if there is a standard way fir example a business I worked add used used irc for monitoring - then you might want a function that calls to that.

仅此而已 2024-08-15 15:47:07

这取决于您如何处理错误:如果您只需要收到通知,电子邮件是最好的选择;如果您需要手动继续处理错误,该表是不错的选择。

that depends on how you will deal with errors: if you just need to be notified, the email is the best option; if you need to manually continue process the error, the table is good choice.

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