Synchronizing Database File Changes in a Single Job
Commitment control is a means of grouping file operations as a single unit so that you can synchronize changes to database files in a single job.
Before you can start commitment control, you must ensure that all the database files you want processed as one unit are in a singlecommitment control environment. All the files within this environment must be journaled to the same journal. Use the CL commands Create Journal Receiver (CRTJRNRCV), Create Journal (CRTJRN) and Start Journal Physical File (STRJRNPF) to prepare for the journaling environment.
- Start Commitment Control (STRCMTCTL)
- CALL program-name
- End Commitment Control (ENDCMTCTL)
You can use commitment control to define and process several changes to database files as a single transaction.
Example:
- Prepare the journaling environment:
- On the command line, enter:
This creates the physical file T1520DD5 using the DDS source shown below:CRTPF FILE(QTEMP/T1520DD5) SRCFILE(QCPPLE/QADDSSRC)Figure 1. T1520DD5 — DDS Source for Daily Transactions A R PURCHASE A ITEMNAME 30 A SERIALNUM 10 - On the command line enter:
This creates the physical file T1520DD6 using the DDS source shown below:CRTPF FILE(QTEMP/T1520DD6) SRCFILE(QCPPLE/QADDSSRC)Figure 2. T1520DD6 — DDS Source for Monthly Transactions A R PURCHASE A ITEMNAME 30 A SERIALNUM 10 - On the command line, enter:
This creates the physical file NFTOBJ for notification text.CRTPF FILE(MYLIB/NFTOBJ) RCDLEN(19)Note: Notification text is sent to the file NFTOBJ when the ILE C program T1520COM that uses commitment control is run. - On the command line, enter:
This creates the display file T1520DD7 using the DDS source shown below:CRTDSPF FILE(QTEMP/T1520DD7) SRCFILE(QCPPLE/QADDSSRC)Figure 3. T1520DD7 — DDS Source for a Purchase Order Display A DSPSIZ(24 80 *DS3) A REF(QTEMP/T1520DD5) A INDARA A CF03(03 'EXIT ORDER ENTRY') A R PURCHASE A 3 32'PURCHASE ORDER FORM' A DSPATR(UL) A DSPATR(HI) A 10 20'ITEM NAME :' A DSPATR(HI) A 12 20'SERIAL NUMBER :' A DSPATR(HI) A ITEMNAME R I 10 37 A SERIALNUM R I 12 37 A 23 34'F3 - Exit' A DSPATR(HI) A R ERROR A 6 28'ERROR: Write failed' A DSPATR(BL) A DSPATR(UL) A DSPATR(HI) A 10 26'Purchase order entry ended' - On the command line, enter:
This creates the journal receiver JRNRCV.CRTJRNRCV JRNRCV(MYLIB/JRNRCV)Note: Journal entries are placed in JRNRCV when the application is run. - On the command line, enter:
This creates the journal JRN and attaches the journal receiver JRNRCV to it.CRTJRN JRN(MYLIB/JRN) JRNRCV(MYLIB/JRNRCV) - On the command line, enter:
This starts journaling the changes that are made to T1520DD5 and T1520DD6 in the journal JRN.STRJRNPF FILE(QTEMP/T1520DD5 QTEMP/T1520DD6) JRN(MYLIB/JRN) IMAGES(*BOTH) - On the command line, enter:
This creates the program T1520COM using the program source shown below:CRTBNDC PGM(MYLIB/T1520COM) SRCFILE(QCPPLE/QACSRC)/* This program illustrates how to use commitment control using the */ /* _Rcommit() function and to rollback a transaction using the */ /* _Rollbck() function. */ #include <stdio.h> #include <recio.h> #include <stdlib.h> #include <string.h> #define PF03 2 #define IND_OFF '0' #define IND_ON '1' int main(void) { char buf[40]; int rc = 1; _SYSindara ind_area; _RFILE *purf; _RFILE *dailyf; _RFILE *monthlyf; /* Open purchase display file, daily transaction file and monthly */ /* transaction file. */ if ( ( purf = _Ropen ( "*LIBL/T1520DD7", "ar+,indicators=y" )) == NULL ) { printf ( "Display file did not open.\n" ); exit ( 1 ); } if ( ( dailyf = _Ropen ( "*LIBL/T1520DD5", "wr,commit=y") ) == NULL ) { printf ( "Daily transaction file did not open.\n" ); exit ( 2 ); } if ( ( monthlyf = _Ropen ( "*LIBL/T1520DD6","ar,commit=y") ) == NULL ) { printf ( "Monthly transaction file did not open.\n" ); exit ( 3 ); } /* The associate separate indicator area with the purchase file. */ _Rindara ( purf, ind_area ); /* Select the purchase record format. */ _Rformat ( purf, "PURCHASE" ); /* Invite the user to enter a purchase transaction. */ /* The _Rwrite function writes the purchase display. */ _Rwrite ( purf, "", 0 ); _Rreadn ( purf, buf, sizeof(buf), __DFT ); /* While the user is entering transactions, update daily and */ /* monthly transaction files. */ while ( rc && ind_area[PF03] == IND_OFF ) { rc = (( _Rwrite ( dailyf, buf, sizeof(buf) ))->num_bytes ); rc = rc && ( _Rwrite ( monthlyf, buf, sizeof(buf) ))->num_bytes; /* If the databases were updated, then commit transaction. */ /* Otherwise, rollback the transaction and indicate to the */ /* user that an error has occurred and end the application. */ if ( rc ) { _Rcommit ( "Transaction complete" ); } else { _Rrollbck ( ); _Rformat ( purf, "ERROR" ); } _Rwrite ( purf, "", 0 ); _Rreadn ( purf, buf, sizeof(buf), __DFT ); } }The
_Ropen()function opens the purchase display file, the daily transaction file, and the monthly transaction file. The_Rindara()function identifies a separate indicator area for the purchase file. The_Rformat()function selects the purchase record format defined in T1520DD7. The_Rwrite()function writes the purchase order display. Data that is entered updates the daily and monthly transaction files T1520DD5 and T1520DD6. The transactions are committed to these database files that use the_Rcommit()function.
- On the command line, enter:
- Run program T1520COM under commitment control. On the command
line, enter:
STRCMTCTL LCKLVL(*CHG) NFYOBJ(MYLIB/NFTOBJ (*FILE)) CMTSCOPE(*JOB)
The display appears as follows:CALL PGM(MYLIB/T1520COM)PURCHASE ORDER FORM ITEM NAME : SERIAL NUMBER : F3 - Exit - Fill out the online Purchase Order Form, using the following sample
data:
TABLE 12345 BENCH 43623 CHAIR 62513After an item and serial number are entered, T1520DD5 and T1520DD6 files are updated. The daily transaction file T1520DD5 file contains the sample data after all three purchase order items are entered.
- End commitment control. On the command line, enter:
ENDCMTCTLThe journal JRN contains entries that correspond to changes that are made to T1520DD5 and T1520DD6.