I've encountered an interesting situation experimenting with the autonomous_transaction. Consider the following situation (please note it's is not intended to be written this way: just proof of concept):
create table t
(
id int primary key,
changed date
)
/
create or replace trigger t_trig
before insert or update
on t
for each row
declare
PRAGMA AUTONOMOUS_TRANSACTION;
begin
:new.changed := sysdate;
commit;
end;
/
insert 开发者_Go百科into t(id, changed) values (1, sysdate);
insert into t(id, changed) values (2, sysdate);
The changed date as of current time:
SQL> select * from t;
ID CHANGED
--------- -----------------
1 19.09.11 15:29:44
2 19.09.11 15:32:35
Let's take a 5 sec break and then do the following:
update t set id = 2 where id = 1;
Obviously it will fail with constraint violation, but it doesn't change the changed
attribute as well:
SQL> select * from t;
ID CHANGED
--------- -----------------
1 19.09.11 15:29:44
2 19.09.11 15:32:35
My question is: why is this happening? I'm sure I misunderstand some basic concepts but I can't get the idea.
Thanks in advance for your help.
The PRAGMA AUTONOMOUS TRANSACTION saves the context, opens another session and makes something. Commit is a must, because otherwise the changes will be lost. You can understand that only the changes made in some block in the database makes sense in this session (autonomous).
So, in your trigger you do nothing. That variable, :new.changed is "changed" in another session, if we can say it in this mode. It is not changed for your update.
精彩评论