TP Example

Here is a transaction processing example that lets you exercise the concept. If you use this example, be mindful that the functions "holdit" and "trestart" are included as tools to allow you access to information within a transaction which would normally be hidden from users. These types of functions would not normally appear in production code. Comments have been inserted into the code to explain the function of various segments.

       trans
       ;This sets up the program constants
       ;for doit and trestart
        n
        s $p(peekon,"V",51)=""
        s $p(peekon,"V",25)="Peeking inside Job "_$j
        s $p(peekoff,"^",51)=""
        s $p(peekoff,"^",25)="Leaving peeking Job "_$j
        ;This establishes the main loop
        s CNFLTMSG="Conflict, please reenter"
        f r !,"Name: ",nam q:'$l(nam) d
        .i nam="?" d q
        ..w !,"Current data in ^trans:",! d:$d(^trans) q
        ...zwrite ^trans
        .f s ok=1 d q:ok w !,$C(7),CNFLTMSG,$C(7),!
        ..s old=$g(^trans(nam),"?")
        ..i old="?" w !,"Not on file" d q
        ...;This is the code to add a new name
        ...f d q:data'="?"
        ....r !,"Enter any info using '#' delimiter: ",!,data 
        ...i data="" w !,"No entry made for ",nam q
        ...TSTART ():SERIAL i $$trestart ;$$trestart for demo
        ...i $d(^trans(nam)) s ok=^trans(nam)=data TRO q
        ...s ^trans(nam)=data
        ...TCOMMIT:$$doit ;$$doit for demo
        ..;This is the beginning of the change and delete loop
        ..f d q:fld=+fld!'$l(fld) w " must be numeric"
        ...w !,"Current data: ",!,old
        ...r !,"Piece no. (negative to delete record) : ",fld
        ..i 'fld w !,"no change made" q
        ..;This is the code to delete a new name
        ..i fld<0 d q ; delete record
        ...f d q:"YyNn"[x
        ....w !,"Ok to delete ",nam," Y(es) or N(o) <N>? " 
        ....r x s x=$e(x)
        ...i "Yy"'[x!'$l(x) w !,"No change made" q
        ...TSTART ():SERIAL i $$trestart ;$$trestart for demo
        ...i $g(^trans(nam),"?")'=old TROLLBACK s ok=0 q
        ...kill ^trans(nam)
        ...TCOMMIT:$$doit; $$doit for demo
        ..;This is the code to change a field
        ..f r !,"Data: ",data q:data'="?"&(data'["#") d
        ...w " must not be a single '?' or contain any '#'"
        ..TSTART ():SERIAL i $$trestart ;$$trestart for demo
        ..i '$d(^trans(nam)) s ok=0 TROLLBACK q
        ..i $p(^trans(nam),"#",fld)=$p(old,"#",fld) d q
        ...s ok=$p(^trans(nam),"#",fld)=data TROLLBACK
        ..s $p(^trans(nam),"#",fld)=data
        ..TCOMMIT:$$doit; $$doit for demo
        q
        doit()
        ;This inserts delay and an optional 
        ;rollback only to show how it works
        w !!,peekon d disp
        f d q:"CR"[act
        .r !,"C(ommit), R(ollback), or W(ait) <C>? ",act
        .s act=$tr($e(act),"cr","CR")
        .i act="?" d disp
        i act="R" TROLLBACK w !,"User requested DISCARD"
        w !,peekoff,!
        q $TLEVEL

        trestart()
        ;This is only to show what is happening
        i $TRESTART d
        .w !!,peekon,!,">>>RESTART<<<",! d disp w !,peekoff,!
        q 1

        disp
        w !,"Name: ",nam
        w !,"Original data: ",!,old,!,"Current data: "
        w !,$g(^trans(nam),"KILLED!")
        q
        

Generally, this type of program would be receiving data from multiple sessions into the same global.