即使应用程序崩溃,触发器也会保持原子性吗

发布于 2024-09-26 09:25:17 字数 237 浏览 3 评论 0原文

我遇到的情况是,我有 2 个表,可以在其中执行插入、更新、删除操作。我引入了一个表audit_trail 来维护这两个表的更改日志。现在要在audit_trail 表中输入值,我已经编写了插入语句在任何一个表上进行任何更新、删除或插入之后。现在,如果其中一个表发生修改并且应用程序崩溃,那么根据我的方法,审核表插入将不会发生。所以我想知道我是否编写了一个触发器在此表上插入、更新或删除后的条件下,即使应用程序崩溃,这也会导致插入审计。DBMS 是 Oracle

I have a situation where I have 2 tables in which I can do Insert,Update,Delete.I'm introducing a table audit_trail to maintain a log of changes of this two tables.Now to enter values in audit_trail table I have written insert statements after any Update,Delete or Insert on either of the table.Now if an modification happened on one of the table and the application crashed then based on my method the Audit table insert will not happen.So I wanted to know if I write a trigger on the condition after insert or update or delete on this table then will this lead to an insert into audit even if the application crashes.DBMS is Oracle

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

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

发布评论

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

评论(2

爱给你人给你 2024-10-03 09:25:17

这是 AUTONOMOUS_TRANSACTION 编译指示。这允许我们在离散事务中发出 SQL,这意味着提交内容不会影响更广泛的事务。因此,即使数据库在用户发出显式提交(或其他)之前崩溃,您的审核消息也会被提交。

显然我不知道你想记录什么数据,但是编写一个像这样的过程并从触发器中调用它:

procedure write_audit
    (p_table_name in audit_table.table_name%type
        , p_action in audit_table.action%type )
is
    pragma autonomous_transaction;
begin
    insert into audit_table
        (id, table_name, ts, action)
    values
        (audit_id.nextval, p_table_name, systimestamp, p_action);
    commit;
end write_audit;

This is one of the few, possibly the only, valid uses for the AUTONOMOUS_TRANSACTION pragma. This allows us to issue SQL in a discrete transaction, which means that stuff gets committed without affecting the wider transaction. Consequently your audit messages will be committed even if the database crashes before the user issues an explicit commit (or whatever).

Obviously I don't know what data you want to log, but write a procedure like this and call it from your triggers:

procedure write_audit
    (p_table_name in audit_table.table_name%type
        , p_action in audit_table.action%type )
is
    pragma autonomous_transaction;
begin
    insert into audit_table
        (id, table_name, ts, action)
    values
        (audit_id.nextval, p_table_name, systimestamp, p_action);
    commit;
end write_audit;
冬天的雪花 2024-10-03 09:25:17

您需要将插入移动到存储过程中,并从审计跟踪触发器中调用它。

在存储过程内部启用“自主事务”,然后您可以在存储过程内部提交,而不会影响触发触发器的“外部”事务。

详细信息请参阅手册:
http://download.oracle.com/docs /cd/B19306_01/appdev.102/b14261/sqloperations.htm#sthref1514

You need to move your insert into a stored procedure and call that from within the audit trail trigger.

Inside the stored procedure enable "autonomous transaction", and then you can commit inside the stored procedure without affecting the "outer" transaction that fired the trigger.

For details see the manual:
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/sqloperations.htm#sthref1514

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