View previous topic :: View next topic |
Author |
Message |
Bithead Advanced

Joined: 03 Jan 2003 Posts: 550 Topics: 23 Location: Michigan, USA
|
Posted: Wed Oct 22, 2003 1:04 pm Post subject: DB2 Triggers |
|
|
I need to propagate some DB2 table changes made by a vendor application to a number of other applications so I have set up some triggers and stored procedures as follows:
1. Trigger_1 will execute when the vendor application changes a field on table_A (Trigger_1 is defined as AFTER UPDATE OF);
2. Trigger_1 invokes StoredProcedure_1 which will update a value on table_B (StoredProcedure_1 is defined as COMMIT NO);
3. Trigger_2 will execute when the table_B is updated (Trigger_2 is defined as AFTER UPDATE OF);
4. Trigger_2 invokes StoredProcedure_2 which will update a value on table_C (StoredProcedure_2 is defined as COMMIT NO);
If I activate only Trigger_1, I can see any changes made to table_A appearing on table_B;
If I activate only Trigger_2, I can manually update table_B and see the changes on table_C;
If I activate both triggers, then the vendor application hangs until I cancel the DB2 thread.
What is going on here? |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Wed Oct 22, 2003 1:45 pm Post subject: |
|
|
Bithead,
I guess the stored procedure definitions for commit are not defined properly.I mean if you activate TRIGGER_1 it should update only TABLE_A since the stored procedure is defined as COMMIT NO.But you are seeing the updates of Table_B.
Did you define it as
Code: |
COMMIT ON RETURN NO
|
When you define it in the above manner, control returns to the DB2 stored procedures address space, and from there to the DB2 system. If the stored procedure definition contains COMMIT ON RETURN NO, DB2 does not commit or roll back any changes from the SQL in the stored procedure until the calling program executes an explicit COMMIT or ROLLBACK statement. If the stored procedure definition contains COMMIT ON RETURN YES, and the stored procedure executed successfully, DB2 commits all changes.
So If you activate TRIGGER_1 , the following series of actions are done.
1. Updates table_A
2. Invokes SP_1
3. Activates Trigger_2 ( result of step2) which update TABLE_B
4. Invokes SP_2 which updates Table_c
Now when you invoke TRIGGER_1 & TRIGGER_2 at the same time, I guess it is an infinite loop each trigger trying to get an exclusive hold of the resource.
Hope this helps...
kolusu |
|
Back to top |
|
 |
Bithead Advanced

Joined: 03 Jan 2003 Posts: 550 Topics: 23 Location: Michigan, USA
|
Posted: Wed Oct 22, 2003 1:50 pm Post subject: |
|
|
Kolusu,
If I code COMMIT ON RETURN YES, then I am committing the changes before the vendor application does. If it has to rollback, it can't.
How do I code it to get it to cascade properly without issuing the commit? |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
|
Back to top |
|
 |
Bithead Advanced

Joined: 03 Jan 2003 Posts: 550 Topics: 23 Location: Michigan, USA
|
Posted: Wed Oct 22, 2003 3:01 pm Post subject: |
|
|
Kolusu,
I have a copy of the documentation but thanks anyway. I omitted to say that I have other processes that update table_B and these need to be propagated to table_C (but not table_A). Hence the need for two triggers.
The IFI looks like it needs a task running all the time or can be run in batch. I already have a batch solution but it is not real-time. I would like to avoid the task if at all possible.
I will have to look at combining the stored procedures into 1 so that a change to table_A will propagate to table_B and table_C. I will have to change the batch job that also changes table_B to also populate table_C.
It is not what I was hoping for but I can say that about alot of things..
Thanks for your help. |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Wed Oct 22, 2003 3:40 pm Post subject: |
|
|
Bithead,
Can you add a column to Table_b? It will be just a 1 byte flag column.change the trigger_2 to kick off when updating the new column.
Vendor updates TABLE_A and this will kick off SP_1 which updates TABLE_B and within SP_1 itself invoke SP_2 which will update TABLE_C.So if everything goes well the changes are committed.
Now the manual update process will update the usual column of TABLE_B and also the new column with some value. Now trigger_2 will kick off as soon as the new column in TABLE_B is updated.The trigger_2 will now update the Table_C
Hope this helps...
cheers
kolusu |
|
Back to top |
|
 |
Bithead Advanced

Joined: 03 Jan 2003 Posts: 550 Topics: 23 Location: Michigan, USA
|
Posted: Wed Oct 22, 2003 3:59 pm Post subject: |
|
|
Sounds intruiging. I will try it. |
|
Back to top |
|
 |
manojagrawal Beginner

Joined: 25 Feb 2003 Posts: 124 Topics: 29
|
Posted: Wed Oct 22, 2003 7:09 pm Post subject: |
|
|
Quote: |
Kolusu said "Now when you invoke TRIGGER_1 & TRIGGER_2 at the same time, I guess it is an infinite loop each trigger trying to get an exclusive hold of the resource".
|
Dont you think trigger 1 and trigger 2 are not invoked concurrently and invoked one after the other,i.e, only after trigger 1 completes does it update tableB and that invokes trigger 2. Moreover, trigger 1 does an update and as trigger 2 would be doing just a read, i doubt if the resource problem would come into the picture. Any comments??? _________________ Thanks & Regards,
Manoj. |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Thu Oct 23, 2003 5:03 am Post subject: |
|
|
Manoj,
I think you overlooked this part of bithead's reply which was posted 3:01 Pm
Quote: |
I omitted to say that I have other processes that update table_B and these need to be propagated to table_C (but not table_A). Hence the need for two triggers.
|
Bithead had 2 process, one which is vendor driven and the other batch driven. The vendor process starts with update of table_a and ends with update of table_c.
The batch process starts with update of table_b and ends with update of table_c.
At some point time a vendor updates table_A which will kick off trigger_1 which in turn will kick off trigger_2. At the same time a batch process may also be running which will update table_b there by kicking off trigger_2.
Now in this situation you might end up in a resource dead-lock
Hope the above explanation makes it clear now
kolusu |
|
Back to top |
|
 |
manojagrawal Beginner

Joined: 25 Feb 2003 Posts: 124 Topics: 29
|
Posted: Thu Oct 23, 2003 8:34 am Post subject: |
|
|
Hi Kolusu,
Yep! I got that now  _________________ Thanks & Regards,
Manoj. |
|
Back to top |
|
 |
Bithead Advanced

Joined: 03 Jan 2003 Posts: 550 Topics: 23 Location: Michigan, USA
|
Posted: Thu Oct 23, 2003 8:35 am Post subject: |
|
|
Kolusu,
I have scheduled the batch process to run when the online application is down. I will still look at triggering off the 1 byte field and have duplicate code as my original scenario does not appear to work. |
|
Back to top |
|
 |
|
|