记录一致性错误日志可以大大简化PL/SQL调试

开发者在线 Builder.com.cn 更新时间:2007-06-04作者:builder.com.cn 来源:

PL/SQL是基于古老的Ada程序设计语言的,因此在创建一个包时,需要提供两部分:规格说明(接口)部分和主体部分接口的真正实现。对于我们简单的错误日志记录包,规格说明部分只提供一个单一过程:

CREATE OR REPLACE package pkg_error is

procedure log (p_error_code errorlog.code%type,

p_error_message errorlog.message%type,

p_package errorlog.package_name%type default null,

p_procedure errorlog.procedure_name%type,

p_location errorlog.location%type default null,

p_parameters errorlog.parameters%type default null);

end pkg_error;

在主体部分,可以包含任意多个你想要的过程和函数。至少,它必须实现规格说明中的过程和函数。在本例中,包的主体部分只实现规格说明中的一个过程:

CREATE OR REPLACE package body pkg_error is

procedure log (p_error_code errorlog.code%type,

p_error_message errorlog.message%type,

p_package errorlog.package_name%type default null,

p_procedure errorlog.procedure_name%type,

p_location errorlog.location%type default null,

p_parameters errorlog.parameters%type default null) is

pragma autonomous_transaction;

begin

insert

into errorlog

(time,

code,

message,

package_name,

procedure_name,

location,

parameters)

values (sysdate,

p_error_code,

p_error_message,

p_package,

p_procedure,

p_location,

p_parameters);

commit;

end log;

end pkg_error;

其中pragma autonomous_transaction部分是十分重要的,因为你保证log方法能够将日志数据提交到数据表中,否则要提交过程中导致错误发生的任何相关信息。这就是pragma段完成的功能。它告诉Oracle将该过程作为原子操作,不能影响早期调用堆栈中的任何信息。

用户评论

  • 用户名
  • 评论内容