SetGlobal (Transaction subroutine): Difference between revisions

From m204wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
{{Template:Transaction:SetGlobal subtitle}}
{{Template:Transaction:SetGlobal subtitle}}
This method is part of [[Janus Two-Phase Commit]] support.
==Syntax==
==Syntax==
{{Template:Transaction:SetGlobal syntax}}
{{Template:Transaction:SetGlobal syntax}}

Revision as of 05:22, 14 January 2020

Add branch to global transaction (Transaction class)

This method is part of Janus Two-Phase Commit support.

Syntax

%(Transaction):SetGlobal( xid, branchId)

Syntax terms

%(Transaction) The class name in parentheses denotes a shared method. SetGlobal can also be invoked via a Transaction object variable, which may be Null.
xid A string indicating the XID (global cross-system id) for the global transaction. Presumably this would have been sent to the running thread by the orchestrator thread.
branchId A string that uniquely (within the global transaction) identifies the branch.

Usage notes

SetGlobal does not communicate with the the coordinator. This is deferred until the first update after the SetGlobal call, at which point the XID and branch id are sent to the coordinator. If there is an error at this point (for example if the XID is not valid) the request is canceled before any updates are performed.

If the global transaction orchestrator is a Model 204 thread, it would probably have obtain an XID via the NewGlobal function.

Generally, one would probably not want multiple transaction branches of the same global transaction running in the same Online since these would likely hit some of the same records and so encounter record locking conflicts with each other. If that is, in fact the case, it is sufficient to make the branch id simply the Online job name:

%(transaction):SetGlobal(%xid, $view("JOBNM"))

Of course, one should be sure that no non-204 branches in the global transaction accidentally use a 204 job name as a branch id. One advantage of using the job name for the branch id is that doing so would catch accidental creation of multiple branches of the same global transaction in the same Online – trying to start a branch with a name already in use in the global transaction results in an error from the coordinator which results in request cancellation.

If, in fact, one wishes to allow multiple branches of the same global transaction in the same Online, one can add the user number to ensure uniqueness of the branch id within an Online:

%(transaction):SetGlobal(%xid, $view("JOBNM") "." $user)

SetGlobal can only be called once until the transaction is committed or backed out. Multiple calls before then results in request cancellation.

Examples

In the following example, a thread receives an XML document on a WebSocket connection, extracts and XID from that document, starts a branch of the global transaction and then stores a record to start the transaction:

%doc is object xmlDoc %xid is string len 64 . . . %doc = new %doc:loadXml(%socket:receiveWebsocket) if %doc:qname("/*") eq "startTransaction" then %xid = %doc:value("/startTransaction/@xid")  %(transaction):setBranch(%xid, $view("JOBNM")) in logfile store record userid = $userid time = %(system):currentTimeString("YYYYMMDDHHMISSXXX") xid = %xid ... end store ... end if