When your shop runs 24/7 and you have very little scheduled downtime, changing trigger programs can be very hard to do. If you implement triggers in the normal fashion, when a change needs to be done, you are forced to either wait for scheduled downtime or kick everyone off the system. This is because, in order to compile the trigger program, you must have an exclusive lock on the object that the trigger program is attached to.
The technique I have come up with gets around that problem by using two programs for the trigger, instead of one. This works because you can recompile a called program at any time, and the program that is calling it does not care. The two files to create are a driver program that is specified on the Add Physical File Trigger (ADDPFTRG) command and a workhorse program that the driver calls.
The following example is based upon the trigger example from IBM's Information Center.
I have included only the source for the database file that has the trigger attached to it, which is ATMTRANS. Also, my trigger examples do not show any processing, just how to code the two programs.
The file layout for ATMTRANS is shown in Figure 1:
A R ATMTRANR A ATMID 5A COLHDG('ATM' 'MACHINE' 'ID') A ACCTID 5A COLHDG('ACCT' 'NUM.') A TCODE 1A COLHDG('TRANS' 'CODE') A AMOUNT 5S 0 COLHDG('AMT. DEP.' 'WITHDRAWN') A K ATMID |
|
Figure 1: ATMTRANS is the source for the database file that has the trigger attached to it.
The driver program TDRVATM is shown in Figure 2:
Hoption(*nodebugio) * This is the DRIVER program for a database trigger. This is the program * added by the ADDPFTRG command. This program will never need to be modified * unless the database file is modified. * * The purpose is only to receive the old record and new record, and call * the WORKHORSE program to do the actual processing. Dparm1 ds DPARM2 DS * The parameters come into the program C *ENTRY PLIST C PARM PARM1 C PARM PARM2 * Call the WORKHORSE program to do the actual work C CALL 'TWRKATM' C PARM PARM1 C PARM PARM2 * OK, done C EVAL *INLR = *ON |
|
Figure 2: This is the driver program for a database trigger.
The workhorse program TWRKATM is shown in Figure 3:
Hoption(*nodebugio) * This is the WORKHORSE program for a database trigger. This is the program * that will actually be doing the work. The reason they are separated is that * a called program can be modified and recompiled at any time. Dparm1 ds * Phys. File Name D FNAME 1 10 * Phys. File Library D LNAME 11 20 * Member Name D MNAME 21 30 * Trigger Event D TEVEN 31 31 * Trigger Time D TTIME 32 32 * Commit Lock Level D CMTLCK 33 33 * Reserved D RSVFILL1 34 36 * CCSID D CCSID 37 40B 0 * Reserved D RSVFILL2 41 48 * Offset to Orig Rec D ORECOFF 49 52B 0 * Length of Orig Rec D ORECLEN 53 56B 0 * Offset to Orig Null D ORECNULOFF 57 60B 0 * Length of Orig Null D ORECNULLEN 61 64B 0 *Offset to New Rec D NRECOFF 65 68B 0 * Length of New Rec D NRECLEN 69 72B 0 * Offset to New Null D NRECNULOFF 73 76B 0 * Length of New Null D NRECNULLEN 77 80B 0 * Reserved D RSVFILL3 81 96 * Old Record Image D OLDREC 97 112 * Old Record Null Map D OLDRECNULL 113 116 * New Record Image D NEWREC 117 132 * New Record Null Map D NEWRECNULL 133 136 * DPARM2 DS D LENGTH 1 4B 0 * Set up a Data Structure to put the Old and New records into. Use the * DDS of the file, since that's the easiest. Also, use PREFIX to make the * Old and New record fields unique. DOLDRECDS E DS EXTNAME(ATMTRANS) D PREFIX(O_) DNEWRECDS E DS EXTNAME(ATMTRANS) D PREFIX(N_) * The parameters come into the program C *ENTRY PLIST C PARM PARM1 C PARM PARM2 * Move the new record into the data structure C MOVE OLDREC OLDRECDS C MOVE NEWREC NEWRECDS C SELECT C WHEN TEVEN = '1' C EXSR @INSERT C WHEN TEVEN = '2' C EXSR @DELETE C WHEN TEVEN = '3' C EXSR @UPDATE C ENDSL C EVAL *INLR = *ON * Routine to be used when a record is inserted into the file C @INSERT BEGSR * * C ENDSR * Routine to be used when a record is deleted from the file C @DELETE BEGSR * * C ENDSR * Routine to be used when a record is updated in the file C @UPDATE BEGSR * * C ENDSR |
|
Figure 3: This is the workhorse program for a database trigger.
With this technique, whenever you need to change or even disable the trigger events, you only have to modify the workhorse program and re-compile. The only time the driver program would need to be modified is when the layout for the database file changes--which, if you were using the traditional trigger approach, would have to be done anyway.
Note: This example uses hard-coded buffers; for a soft-coded example,
please go to www.ibm.com/redbooks. The Redbook SG256403 contains an example on pages 345-350 for RPG.
--Tim Grove
Robert Weed Plywood Corp.
This email address is being protected from spambots. You need JavaScript enabled to view it.
Did this tip "trigger" a tip idea of your own?
Send your useful tips, tricks, or techniques to
This email address is being protected from spambots. You need JavaScript enabled to view it.
LATEST COMMENTS
MC Press Online