Home » SQL & PL/SQL » SQL & PL/SQL » How to prevent trigger from firing on failed insert (Oracle 8,2)
How to prevent trigger from firing on failed insert [message #653038] Fri, 24 June 2016 10:56 Go to next message
laddg
Messages: 9
Registered: May 2005
Junior Member
I am inexperienced, but learning fast.

I have a trigger that fires on 'after insert'. It works well.

When a record failed to insert (for integrity constraints), I assumed that it would not fire since it was an 'after', but it does.

How can I prevent this trigger from firing on a failed insert? Can I test inside the trigger for success of the insert? Maybe as part of the 'when' clause?

Thanks in advance.

DJG
Re: How to prevent trigger from firing on failed insert [message #653042 is a reply to message #653038] Fri, 24 June 2016 11:02 Go to previous messageGo to next message
John Watson
Messages: 8930
Registered: January 2010
Location: Global Village
Senior Member
The way constraints work is that the row (please don't say "record" when you mean "row") is inserted, then the constraint is checked, and then if necessary the insert is rolled back. Whatever your trigger is doing should be rolled back too. The exception is if your trigger is doing something really stupid, such as an autonomous transaction or sending an email.
Almost certainly you are using trigges in an inappropriate way if this is a problem. Most applictions never need a trigger, and using them to implement business logic is usually a very bad idea.
Re: How to prevent trigger from firing on failed insert [message #653043 is a reply to message #653038] Fri, 24 June 2016 11:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

What's the problem? if the INSERT fails the trigger job is rolled back as well... unless you do not transactional things in it... li,e AUTONOMOUS_TRANSACTION... and in this case... this is YOUR fault.

Re: How to prevent trigger from firing on failed insert [message #653044 is a reply to message #653043] Fri, 24 June 2016 11:08 Go to previous messageGo to next message
laddg
Messages: 9
Registered: May 2005
Junior Member
Maybe I did not give enough information. The process is:

A row is inserted into a table by any of a large number of different processes
Once the row is inserted, the trigger creates a web event which then transmits the row to a remote service

If the row is not inserted properly, I do not want to call the web service
Essentially, if the insert fails, I do not want to fire the trigger

Does that make sense?

[Updated on: Fri, 24 June 2016 11:08]

Report message to a moderator

Re: How to prevent trigger from firing on failed insert [message #653045 is a reply to message #653044] Fri, 24 June 2016 11:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
the trigger creates a web event which then transmits the row to a remote service


This is NOT transactional.
Create a job (using DBMS_JOB) that will create the web event is the transaction is committed. If the INSERT fails just roll back to before the INSERT this is what SAVEPOINT are made for.


Re: How to prevent trigger from firing on failed insert [message #653046 is a reply to message #653044] Fri, 24 June 2016 11:14 Go to previous messageGo to next message
John Watson
Messages: 8930
Registered: January 2010
Location: Global Village
Senior Member
Yes, it makes sense, and is a bad design. As you have discovered! Really, triggers are not meant for this sort of thing. Some sites have rules banning such use completely.

A better design would be be to go via a queue. Your trigger can enqueue a message (which will be rolled back if the insert fails) and then after commit a job process can dequeue the message and use it to call the web service, asynchronously with regard to the transaction. That way you decouple the external event from the internal event, and have full control of it.
Re: How to prevent trigger from firing on failed insert [message #653047 is a reply to message #653046] Fri, 24 June 2016 11:34 Go to previous messageGo to next message
laddg
Messages: 9
Registered: May 2005
Junior Member
Thank you all for the feedback. I will have to learn about jobs...
Re: How to prevent trigger from firing on failed insert [message #653048 is a reply to message #653045] Fri, 24 June 2016 11:35 Go to previous messageGo to next message
John Watson
Messages: 8930
Registered: January 2010
Location: Global Village
Senior Member
A simpler solution than mine. And 99 times out of a 100, "simpler" means "better".
Re: How to prevent trigger from firing on failed insert [message #653049 is a reply to message #653048] Fri, 24 June 2016 11:40 Go to previous messageGo to next message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Not if the insert rate is high, in this case a queue is a better solution: only one job fire for many inserts.

Re: How to prevent trigger from firing on failed insert [message #653050 is a reply to message #653049] Fri, 24 June 2016 12:44 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>How to prevent trigger from firing on failed insert (Oracle 8,2)
HUH?

post complete results from SQL below

SELECT * FROM V$VERSION;
Re: How to prevent trigger from firing on failed insert [message #653051 is a reply to message #653050] Fri, 24 June 2016 12:51 Go to previous messageGo to next message
laddg
Messages: 9
Registered: May 2005
Junior Member
DOHHH!!!! 9.2 not 8.2

Oracle9i Enterprise Edition Release 9.2.0.6.0 - 64bit Production
PL/SQL Release 9.2.0.6.0 - Production
"CORE 9.2.0.6.0 Production"
TNS for HPUX: Version 9.2.0.6.0 - Production
NLSRTL Version 9.2.0.6.0 - Production
Re: How to prevent trigger from firing on failed insert [message #653052 is a reply to message #653051] Fri, 24 June 2016 13:21 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
Is there a reason you haven't upgraded to oracle 12? version 9 is not only unsupported, but it doesn't even have extended support.
Re: How to prevent trigger from firing on failed insert [message #653053 is a reply to message #653052] Fri, 24 June 2016 13:23 Go to previous messageGo to next message
laddg
Messages: 9
Registered: May 2005
Junior Member
Completely out of my control. I know the company is upgrading databases regularly (we have hundreds), just haven't gotten to this one yet.
Re: How to prevent trigger from firing on failed insert [message #653059 is a reply to message #653053] Sat, 25 June 2016 07:02 Go to previous message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
laddg wrote on Fri, 24 June 2016 13:23
Completely out of my control. I know the company is upgrading databases regularly (we have hundreds), just haven't gotten to this one yet.



I appreciate that you may not have any control, but saying they are "upgrading databases regularly" seems a bit of dodge. We must have different ideas about what is meant by "regularly". I haven't even SEEN a 9i database in 10 years. There are now no less than THREE major releases superseding 9i. Even if "regularly" means "regularly, once every decade" they would at least be to 10g by now.

Smile

[Updated on: Sat, 25 June 2016 07:03]

Report message to a moderator

Previous Topic: Table update through nested loop issue in function
Next Topic: column with data_type CLOB does not appear in user_ind_columns, why?
Goto Forum:
  


Current Time: Tue Apr 23 04:57:52 CDT 2024