ILE C/C++ Programmer's Guide


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.

Once the journaling environment is established, you can use the following commands:

You can use commitment control to define and process several changes to database files as a single transaction.

Example:

The following example uses commitment control. Purchase orders are entered and logged in two files, T1520DD5 for daily transactions, and T1520DD6 for monthly transactions. Journal entries that reflect the changes that are made to T1520DD5 and T1520DD6 are kept in the journal JRN.

  1. Prepare the journaling environment:
    1. On the command line, enter:

      CRTPF FILE(QTEMP/T1520DD5) SRCFILE(QCPPLE/QADDSSRC)

      This creates the physical file T1520DD5 using the DDS source shown below:

      Figure 133. T1520DD5 -- DDS Source for Daily Transactions



      A R PURCHASE
      A ITEMNAME 30
      A SERIALNUM 10
    2. On the command line enter:

      CRTPF FILE(QTEMP/T1520DD6) SRCFILE(QCPPLE/QADDSSRC)

      This creates the physical file T1520DD6 using the DDS source shown below: type:

      Figure 134. T1520DD6 -- DDS Source for Monthly Transactions



      A R PURCHASE
      A ITEMNAME 30
      A SERIALNUM 10
    3. On the command line, enter:

      CRTPF FILE(MYLIB/NFTOBJ) RCDLEN(19)

      This creates the physical file NFTOBJ for notification text.

      Note:
      Notification text is sent to the file NFTOBJ when the ILE C program T1520COM that uses commitment control is run.
    4. On the command line, enter:

      CRTDSPF FILE(QTEMP/T1520DD7) SRCFILE(QCPPLE/QADDSSRC)

      This creates the display file T1520DD7 using the DDS source shown below:

      Figure 135. 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'
    5. On the command line, enter:

      CRTJRNRCV JRNRCV(MYLIB/JRNRCV)

      This creates the journal receiver JRNRCV.

      Note:
      Journal entries are placed in JRNRCV when the application is run.
    6. On the command line, enter:

      CRTJRN JRN(MYLIB/JRN) JRNRCV(MYLIB/JRNRCV)

      This creates the journal JRN and attaches the journal receiver JRNRCV to it.

    7. On the command line, enter:


      STRJRNPF FILE(QTEMP/T1520DD5 QTEMP/T1520DD6) JRN(MYLIB/JRN)
      IMAGES(*BOTH)

      This starts journaling the changes that are made to T1520DD5 and T1520DD6 in the journal JRN.

    8. On the command line, enter:

      CRTBNDC PGM(MYLIB/T1520COM) SRCFILE(QCPPLE/QACSRC)

      This creates the program T1520COM using the program source shown below:

      Figure 136. T1520COM -- ILE C Source to Group File Operations Using Commitment Control


      
      
      /* 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.

  2. Run program T1520COM under commitment control. On the command line, enter:

    STRCMTCTL LCKLVL(*CHG) NFYOBJ(MYLIB/NFTOBJ (*FILE)) CMTSCOPE(*JOB)


    CALL PGM(MYLIB/T1520COM)

    The display appears as follows:

    +--------------------------------------------------------------------------------+
    |                               PURCHASE ORDER FORM                              |
    |                                                                                |
    |                                                                                |
    |                                                                                |
    |                                                                                |
    |                                                                                |
    |                                                                                |
    |                   ITEM NAME     :                                              |
    |                                                                                |
    |                   SERIAL NUMBER :                                              |
    |                                                                                |
    |                                                                                |
    |                                                                                |
    |                                                                                |
    |                                                                                |
    |                                                                                |
    |                                                                                |
    |                                                                                |
    |                                                                                |
    |                                                                                |
    |                                 F3 - Exit                                      |
    |                                                                                |
    +--------------------------------------------------------------------------------+
  3. Fill out the online Purchase Order Form, using the following sample data:

    TABLE 12345
    BENCH 43623
    CHAIR 62513

    After 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.

  4. End commitment control. On the command line, enter:

    ENDCMTCTL

    The journal JRN contains entries that correspond to changes that are made to T1520DD5 and T1520DD6.


[ Top of Page | Previous Page | Next Page | Table of Contents ]