tags:

views:

83

answers:

3

How can I get the equivalent of an "on commit" trigger after inserting some rows into a table?

After inserting several rows into a table, I would like to send a message to an external process that there are rows ready to process. Using a statement-level trigger causes one message per insert, and I would like to send just one message saying "there are rows to be processed."

+5  A: 

Create a job. It won't actually be submitted until a commit occurs. (Note: DBMS_SCHEDULER is usually better than DBMS_JOB, but in this case you need to use the old DBMS_JOB package.)

declare
  jobnumber number;
begin
  dbms_job.submit(job => jobnumber, what => 'insert into test values(''there are rows to process'');');
  --do some work here...
  commit;
end;
/
jonearles
+1: simple, efficient.
Vincent Malgrat
Is there a way I can use this to coalesce inserts? I would like to process multiple inserts, and then do one "it's ready" step.
Mark Harrison
How is the work being done? In a single transaction that you control (through a stored procedure or something), or just a simple insert statement by the users? Would a table-level AFTER trigger work?
jonearles
If there are a lot of transactions that you have no control over, maybe you could set the job to a minute in the future and have triggers check for the existence of the job in DBA_JOBS so they don't submit duplicates.
jonearles
+1  A: 

You can set a flag to say "I've sent the message". To be sure you 'reset' the flag on commit, use dbms_transaction.local_transaction_id then you can simply do a

IF v_flag IS NULL OR dbms_transaction.local_transaction_id != v_flag THEN
  v_flag := dbms_transaction.local_transaction_id;
  generate message
END IF;
Gary
+1  A: 

Using Oracle Advanced Queueing you can enqueue an array of records with a listener on the queue table.

The records will load, and the listener can then kick of any process you wish, even a web service call

http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28419/d_aq.htm#i1001754

Stephen Bealer