Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle: How to execute an insert trigger without delaying the insert response?

The trigger below is delaying my insert response. How can I prevent this?

create or replace
TRIGGER GETHTTPONINSERT
BEFORE INSERT ON TABLENAME
FOR EACH ROW 
Declare
  --   
BEGIN
  -- The inserted data is transfered via HTTP to a remote location
END;

EDIT People are telling me to do batch jobs, but I would rather have the data earlier than having 100% consistency. The advantage of the trigger is that it happens as soon as the data arrives, but I can't afford the insert response delay.

like image 676
Jader Dias Avatar asked Dec 02 '22 07:12

Jader Dias


2 Answers

One approach is to have the trigger create a dbms_job that runs once (each) time to perform the http transfer. The dbms_job creation is relatively quick and you can think of this as effectively spawning a new thread in parallel.

See http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:7267435205059 for further info - his example deals with sending email, but the idea is the same.

like image 70
dpbradley Avatar answered Dec 18 '22 22:12

dpbradley


There is a perfect solution for this exact situation called Database Change Notification.

You can think of it almost exactly like an async trigger.

You use the DBMS_Change_Notification package to tell oracle which tables to watch and what to do when a change occurs. You can monitor for DML and DDL, you can have Oracle batch the changes (i.e. wait for 10 changes to occur before firing). It will call a sproc with an object containing all the rowids of the changed rows... you can decide how to handle, including calling HTTP. It will not have to finish for the insert to commit. Documentation for 10gR2

like image 21
Stephanie Page Avatar answered Dec 18 '22 21:12

Stephanie Page