1 /* ====== BEGIN INCLUDE FILE           mrds_finish_transaction.incl.pl1 =========================== */
  2 
  3 
  4 
  5 /****^  HISTORY COMMENTS:
  6   1) change(85-11-17,Dupuis), approve(85-12-16,MCR7314),
  7      audit(86-02-04,Brunelle), install(86-02-05,MR12.0-1013):
  8      This entry is being made to cover the change made on 85-05-06 by Thanh
  9      Nguyen. The code now checks a local flag. (mrds error list #136).
 10                                                    END HISTORY COMMENTS */
 11 
 12 
 13 /*
 14                     BEGIN_DESCRIPTION
 15 
 16    A generalized routine accessed by all MRDS modules that begin and commit
 17    transactions.  The intent is that it be executed as inline code. It is
 18    assumed that mrds_start_transaction was executed previously in the
 19    procedure.  Prior to this include file the program should assign the value
 20    of its local error code to mftxn_code.  The program utilizing this
 21    include file must supply a function labeled should_rollback that returns
 22    bit (1).  This routine should examine the error code mftxn_code and whether
 23    the transaction should be aborted or rolled back.  "0"b means abort and "1"b
 24    means rollback.  This procedure may choose to simply return "0"b as it
 25    appears that MRDS will generally NOT rollback transactions.  This routine
 26    does rollback and restart if the before journal is full but only attempts it
 27    once.  A procedure labelled restore_significant_data must also be supplied,
 28    where any data that was saved prior to the transaction is restored.  A
 29    procedure consisting solely of a return statement can be supplied if
 30    necessary.  After execution of this include file, mftxn_code must be
 31    examined.  If it was 0 before entering the code and is non-zero afterward,
 32    then the commit has failed.  Otherwise it will be unchanged.
 33 
 34                     END_DESCRIPTION
 35 
 36    Written 82-09-30 by Paul W. Benjamin.
 37    Modified 83-01-13 by PWB to add retry on deadlocks and to return a non-zero
 38                      error code only when the transaction is in an error state.
 39    Modified 83-02-04 by PWB to reset transaction id to 0 upon completion.
 40    Modified 83-05-05 by PWB to abort when rollback fails, abandon when abort
 41                      fails, and to abort rather than rollback when bj is full.
 42    Modified 83-05-18 by PWB to use mftxn_temp_code in calls to abandon, abort
 43                      and rollback.
 44    Modified 83-05-19 by PWB to add mftxn_check_code label.  It is transferred
 45                      to by the mstxn_any_other procedure.
 46    Modified 85-04-14 by Thanh Nguyen not to commit the transaction in case of
 47                      the user already started his own transaction.
 48    Modified 85-05-06 by Thanh Nguyen to synchronize this include file between
 49                      the directory >ldd>include and >exl>mrd>i.
 50 */
 51 
 52 dcl dm_error_$bj_journal_full fixed bin(35) ext static;
 53 dcl dm_error_$lock_deadlock fixed bin(35) ext static;
 54 dcl mftxn_code fixed bin (35);
 55 dcl mftxn_temp_code fixed bin (35);
 56 dcl transaction_manager_$commit_txn entry (bit (36) aligned, fixed bin (35));
 57 dcl transaction_manager_$rollback_txn entry (bit (36) aligned, fixed bin (17), fixed bin (35));
 58 
 59 
 60           if mstxn_txn_id = "0"b | user_started_transaction = "1"b    /* No transaction or we did not started it */
 61                then do;
 62                mftxn_code = 0;
 63                goto mftxn_exit;
 64           end;
 65 mftxn_check_code:
 66           if mftxn_code = 0
 67                then do;
 68                call transaction_manager_$commit_txn (mstxn_txn_id, mftxn_code);
 69                if mftxn_code ^= 0
 70                     then do;
 71                     call transaction_manager_$abort_txn (mstxn_txn_id, mftxn_temp_code);
 72                     if mftxn_temp_code ^= 0
 73                          then call transaction_manager_$abandon_txn (mstxn_txn_id, mftxn_temp_code);
 74                end;
 75           end;
 76           else do;
 77                call restore_significant_data;
 78                if mftxn_code = dm_error_$lock_deadlock      /* retry just once if deadlock */
 79                     & mstxn_retries < 1
 80                     then do;
 81                          mstxn_retries = mstxn_retries + 1;
 82                          call transaction_manager_$rollback_txn (mstxn_txn_id, 0, mftxn_temp_code);
 83                          if mftxn_temp_code ^= 0
 84                               then do;
 85                               call transaction_manager_$abort_txn (mstxn_txn_id, mftxn_temp_code);
 86                               if mftxn_temp_code ^= 0
 87                                    then call transaction_manager_$abandon_txn (mstxn_txn_id, mftxn_temp_code);
 88                          end;
 89                          else do;
 90                               mstxn_code = 0;
 91                               goto mstxn_exit;              /* go back and try again */
 92                          end;
 93                     end;
 94                else if should_rollback ()                   /* let the program decide */
 95                     then do;
 96                     call transaction_manager_$rollback_txn (mstxn_txn_id, 0, mftxn_temp_code);
 97                     if mftxn_temp_code ^= 0
 98                          then do;
 99                          call transaction_manager_$abort_txn (mstxn_txn_id, mftxn_temp_code);
100                          if mftxn_temp_code ^= 0
101                               then call transaction_manager_$abandon_txn (mstxn_txn_id, mftxn_temp_code);
102                     end;
103                     else do;
104                          mstxn_code = 0;
105                          goto mstxn_exit;                   /* go back and try again */
106                     end;
107                end;
108                else do;
109                     call transaction_manager_$abort_txn (mstxn_txn_id, mftxn_temp_code);
110                     if mftxn_temp_code ^= 0
111                          then call transaction_manager_$abandon_txn (mstxn_txn_id, mftxn_temp_code);
112                end;
113           end;
114           mstxn_txn_id = "0"b;                              /* should never be nonzero unless there is a txn */
115 mftxn_exit:
116 
117 /* ------ END INCLUDE FILE              mrds_finish_transaction.incl.pl1 --------------------------- */