1 /* ====== BEGIN INCLUDE FILE          rmdb_start_transaction.incl.pl1 =========================== */
  2 
  3 /*
  4                     BEGIN_DESCRIPTION
  5 
  6    A generalized routine accessed by 5 MRDS modules  (create_mrds_db.pl1,
  7    rmdb_create_index.pl1, rmdb_create_relation.pl1, rmdb_delete_index.pl1,
  8    rmdb_delete_relation.pl1) that must start transactions if none are present.
  9    The intent is that it be executed as inline code.  The variable
 10    mstxn_transactions_needed must be set prior to entering this code.
 11    In most cases a simple assignment from dbcb_data.transactions_needed
 12    will suffice.  Included are procedures called mstxn_cleanup and
 13    mstxn_any_other.  These procedures must be called by cleanup and any_other
 14    handlers in the program.  Such handlers should be established just prior to
 15    the inclusion of this code and disabled just following the inclusion of
 16    rmdb_finish_transaction.  Directly prior to establishing the handlers
 17    mstxn_txn_id must be set to "0"b.  This must be done even though this
 18    include file does the same, because this code might not have been entered
 19    yet when the handler is invoked.  Directly following this include file the
 20    contents of mstxn_code should be examined.  If zero, then either the
 21    transaction was successfully started or no transaction was required.  If the
 22    mrds_finish_transaction code is referenced in general error handling
 23    situations where the possibility exists that the code in this include file
 24    has not been executed, it is necessary to initialize mstxn_txn_id to "0"b at
 25    the beginning of the program.
 26 
 27                     END_DESCRIPTION
 28 
 29    Written 82-09-28 by Paul W. Benjamin.
 30    Modified 82-12-09 by PWB to include mstxn_any_other.
 31    Modified 83-01-07 by PWB to not reference the dbcb.
 32    Modified 83-01-10 by PWB to add a call to continue_to_signal_ to the
 33                      any_other handler in situations where the module did
 34                      not start the transaction.
 35    Modified 83-05-05 by PWB to abandon when abort fails.
 36    Modified 83-05-18 by PWB to use mstxn_temp_code in calls to abandon and
 37                      and abort.
 38    Modified 83-05-19 by PWB to handle transaction_deadlock and
 39                      transaction_bj_full conditions.
 40    Modified 84-02-04 by PWB to add trailing underscores to the 2 conditions
 41                      and to handle transaction_lock_timeout_.
 42    Modified 85-04-14 by Thanh Nguyen: Changed this include file name to
 43                      rmdb_start_transaction.incl.pl1 with the same content
 44                      for create_mrds_db.pl1, rmdb_create_index.pl1,
 45                      rmdb_delete_index.pl1, rmdb_delete_relation.pl1.  Because
 46                      these four modules do not contain the dbcb structure, but
 47                      we need to reference to dbcb for the rest of other mrds
 48                      modules.
 49 */
 50 
 51 dcl continue_to_signal_ entry (fixed bin(35));
 52 dcl dm_error_$no_current_transaction fixed bin (35) ext static;
 53 dcl error_table_$null_info_ptr fixed bin(35) ext static;
 54 dcl find_condition_info_ entry (ptr, ptr, fixed bin(35));
 55 dcl mstxn_code fixed bin (35);
 56 dcl mstxn_retries fixed;
 57 dcl mstxn_temp_code fixed bin (35);
 58 dcl mstxn_transactions_needed bit (1) aligned;
 59 dcl mstxn_txn_id bit (36) aligned;
 60 dcl transaction_manager_$abandon_txn entry (bit (36) aligned, fixed bin (35));
 61 dcl transaction_manager_$abort_txn entry (bit (36) aligned, fixed bin (35));
 62 dcl transaction_manager_$begin_txn entry (fixed bin, fixed bin (35), bit (36) aligned, fixed bin (35));
 63 dcl transaction_manager_$get_current_txn_id entry (bit (36) aligned, fixed bin (35));
 64 dcl transaction_manager_$handle_conditions entry ();
 65 dcl 1 mstxn_condition_info like condition_info;
 66 %page;
 67 %include dm_tm_modes;
 68 %skip;
 69 %include condition_info;
 70 %page;
 71           mstxn_code = 0;
 72           mstxn_txn_id = "0"b;
 73 
 74           if ^mstxn_transactions_needed                     /* only need transactions */
 75                then goto mstxn_exit;                        /* for protected page files */
 76 
 77           mstxn_retries = 0;
 78           call transaction_manager_$get_current_txn_id (mstxn_txn_id, mstxn_code);
 79           if mstxn_code ^= dm_error_$no_current_transaction /* and if none already in progress */
 80                then do;
 81                mstxn_txn_id = "0"b;                         /* you didn't start it, it's none of your business */
 82                goto mstxn_exit;
 83           end;
 84 
 85           call transaction_manager_$begin_txn (TM_NORMAL_MODE, 0, mstxn_txn_id, mstxn_code);
 86 
 87 mstxn_cleanup:
 88 proc;
 89 
 90 /*  This procedure MUST be called by a cleanup handler. */
 91 
 92           if mstxn_txn_id ^= "0"b
 93           then do;
 94                call transaction_manager_$abort_txn (mstxn_txn_id, mstxn_temp_code);
 95                if mstxn_temp_code ^= 0
 96                     then call transaction_manager_$abandon_txn (mstxn_txn_id, mstxn_temp_code);
 97           end;
 98 
 99      end mstxn_cleanup;
100 
101 mstxn_any_other:
102 proc;
103 
104 /*  This procedure MUST be called by an any_other handler. */
105 
106           if mstxn_txn_id ^= "0"b
107           then do;
108                call find_condition_info_ (null (), addr(mstxn_condition_info), mstxn_temp_code);
109                if mstxn_condition_info.condition_name = "transaction_deadlock_"
110                     then do;
111                     mftxn_code = dm_error_$lock_deadlock;
112                     goto mftxn_check_code;
113                end;
114                else if mstxn_condition_info.condition_name = "transaction_bj_full_"
115                     | mstxn_condition_info.condition_name = "transaction_lock_timeout_"
116                     then do;
117                     mftxn_code = dm_error_$bj_journal_full;
118                     goto mftxn_check_code;
119                end;
120                else call transaction_manager_$handle_conditions;
121           end;
122           else call continue_to_signal_ (mstxn_code);       /* code returned will always be zero */
123      end mstxn_any_other;
124 
125 mstxn_exit:
126 
127 /* ------ END INCLUDE FILE              rmdb_start_transaction.incl.pl1 --------------------------- */