简体   繁体   English

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

[英]Log error messages in Oracle stored procedure

We plan to configure a stored procedure to run as a batch job daily using Oracle DBMS scheduler package. 我们计划使用Oracle DBMS调度程序包将存储过程配置为每天作为批处理作业运行。 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. 提前致谢。

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. 您可能希望使用对您的代码有意义的异常来整理它,但这是一般的想法,在SP_LOG调用中写入的数据仍然存在,但父事务仍然可以回滚。

You could use log4plsql http://log4plsql.sourceforge.net/ and change the choice later by configuration changes not code changes 您可以使用log4plsql http://log4plsql.sourceforge.net/并稍后通过配置更改而不是代码更改来更改选择

The log4plsql page gives a list of various places it can log. log4plsql页面列出了它可以记录的各种位置。

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. 它还取决于您的环境中应用程序和系统的监控方式 - 如果有一个标准的方式,我所工作的业务添加了使用过的irc进行监控 - 那么您可能需要一个调用它的函数。

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. 如果您需要手动继续处理错误,该表是不错的选择。

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. 您说您没有很多控制数据库环境来安装日志包 - 如果是这种情况,那么您将仅限于查询dba_scheduler_job_run_details和dba_scheduler_job_log系统视图中的信息 - 您将能够在这里查看处决的历史。 Unhandled exceptions will show up in the ADDITIONAL_INFO column. 未处理的异常将显示在ADDITIONAL_INFO列中。 If you need notification you can poll these views and generate email. 如果您需要通知,可以轮询这些视图并生成电子邮件。

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

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