views:

103

answers:

1

Hi,

Is there a way to do this?. I found adding,

DBMS_LOCK.sleep() 

to the beginning of the trigger code by googling, but it blocks the insert itself from happening. I would like to insert the data but the trigger should be fired only after an arbitrary delay. Thanks.

+5  A: 

It would help if we knew why you want this delay, and what the trigger is supposed to do after the delay. However, one possibility is to use the DBMS_JOB package in the trigger to create a job that runs at a time a little after the insert. For example:

create trigger trg
after insert on tab
for each row
declare
  jl_ob number;
begin
  dbms_job.submit
    ( job => l_job
    , what => 'myproc(:new.id);'
    , next_date => sysdate+1/24/60 -- One minute later
    );
end;

Alternatively, the trigger could insert a row into a special table, and a DBMS_JOB that runs on a schedule e.g. every 10 minutes could process rows in the table that are more than X minutes old.

Tony Andrews
in 10g and up, you'd use DBMS_SCHEDULER. Further, invocations of DBMS_JOB/DBMS_SCHEDULER are controlled by transactions, so the job would not be scheduled until after the transaction committed, which is not the same as after the row was inserted.
Adam Musch
True, I must learn to say DBMS_SCHEDULER in future! As for the transaction control there is the option of using an autonomous transaction IFF it doesn't matter that the job runs even if the triggering statement rolls back. I would say it has to be one or the other.
Tony Andrews
Actually, in researching using DBMS_SCHEDULER I found that it performs an implicit commit, which means it can't be used in a situation where you'd want to have it do something that is potentially rolled back. As for DBMS_JOB being deprecated (which it is) Tom Kyte said this year "I cannot say anything other than I would be very surprised to see [DBMS_JOB] go away and I'll be filing a bug immediately when and if I ever receive a beta that doesn't have it..." (http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:25405608521707). So I'll leave my answer as it is.
Tony Andrews
Tom Kyte also says of DBMS_SCHEDULR (in the same page) that it 'is a bit "overkill" perhaps for a one time job.' One of the many things I love about Tom is his refreshing pragmatism!
Tony Andrews
I stand corrected; I got bit enough with the DBMS_JOB commit requirement that I wasn't aware of the implicit commit in DBMS_SCHEDULER. However, invoking that in a trigger could cause as many problems as it solves.
Adam Musch
@Adam, since Oracle won't permit a commit in a trigger, it won't allow DBMS_SCHEDULER in a trigger either (except as an autonomous transaction, and you REALLY don't want to go there).
Gary