Manage active and prepared transactions using scripting
In normal circumstances, transactions should run and complete (commit or roll back) automatically, without the need for intervention. However, in some circumstances, we might have to resolve a transaction manually. For example, we might want to roll back a transaction that is stuck polling a resource manager that you know will not become available again in the desired time frame.
If we choose to complete a transaction on an appserver, it is recorded as having completed in the transaction service logs for that server, so it is not eligible for recovery during server start up. If we complete a transaction, you are responsible for cleaning up any in-doubt transactions on the resource managers affected.
The TransactionService Managed Bean (MBean), see API documentation - Application programming interfaces (package: Public MBean Interfaces, class: TransactionService), is used to list transactions in various states by invoking one of the following methods:
listOfTransactions: Lists all non-completed transactions. Under no circumstances should you attempt to alter the state of active transactions (using commit or rollback for example) listManualTransactions: Lists transactions awaiting admin completion. We can choose to commit or rollback transactions in this state listRetryTransactions: Lists transactions with some resources being retried. We can choose to finish (stop retrying) transactions in this state listHeuristicTransactions: Lists transactions that have completed heuristically. We can choose to clear the transaction from the list listImportedPreparedTransactions: Lists transactions that have been imported and prepared but not yet committed. We can choose to commit or rollback transactions in this state
Each entry in the returned list contains the following attributes:
- Local Transaction Identifier
- Status, which can be interpreted by calling getPrintableStatus on the Transaction MBean
- Global Transaction Identifier
- Heuristic Outcome, which can take one of the following values:
- 8 (HEURISTIC_COMMIT)
- 9 (HEURISTIC_ROLLBACK)
- 10 (HEURISTIC_MIXED)
- 11 (HEURISTIC_HAZARD)
The TransactionService MBean (see API documentation - Application programming interfaces (package: Public MBean Interfaces, class: TransactionService)) can also be used to gather more information about the properties of the transaction service, by obtaining the following attributes:
transactionLogDirectory The directory into which the log file(s) used for transaction service are placed totalTranLifetimeTimeout The total lifetime of the transaction (in seconds) before the container rolls it back. This value applies to Container Managed Transaction (CMT) beans only asyncResponseTimeout The total lifetime of the transaction (in seconds) before the container rolls it back enableFileLocking Enables the use of file locks when opening the transaction service recovery log enableProtocolSecurity Causes transaction service protocol messages to be sent securely maximumTransactionTimeout The maximum transaction timeout allowed for imported transactions. This value applies to Container Managed Transaction (CMT) beans, Bean Managed Transaction (BMT) beans, and imported transactions clientInactivityTimeout The number of seconds a transaction can remain inactive before it is rolled back heuristicRetryLimit The maximum number of times to retry transaction completion heuristicRetryWait The number of seconds to wait between retrying transaction completion httpProxyPrefix The HTTP prefix for WS-Transaction port SOAP addresses httpsProxyPrefix The HTTPS prefix for WS-Transaction port SOAP addresses propogatedOrBMTTranLifetimeTimeout The number of seconds a transaction can remain inactive before it is rolled back LPSHeuristicCompletion The transaction completion action to be taken when the outcome is unknown
The Transaction MBean (see API documentation - Application programming interfaces (package: Public MBean Interfaces, class: Transaction)) can be used to commit, rollback, finish or remove from the list of heuristically completed transactions depending on the transaction's state, by invoking one of the following methods:
commit: Heuristically commits the transaction rollback: Heuristically rolls back the transaction finish: Stops retrying resources for the transaction removeHeuristic: Clears the transaction from the list
The Transaction MBean can also be used to gather more information about a transaction, by invoking the following methods:
- getPrintableStatus: Return the transaction status
- getGlobalTranName: Get the global identifier for the transaction
- listResources: List the resources for the transaction
The following script is an example of how to use the TransactionService MBean and Transaction MBean. The script should be run only against an appserver, and not against the deployment manager or node agent.
Example
Working with manual transactions: example jacl script.
# get the TransactionService MBean set servicembean [$AdminControl queryNames type=TransactionService,*] # get the Transaction MBean set mbean [$AdminControl queryNames type=Transaction,*] set input 0 while {$input >= 0} { # invoke the listManualTransactions method set tranManualList [$AdminControl invoke $servicembean listManualTransactions] if {[llength $tranManualList] > 0} { puts "----Manual Transaction details---------------" set index 0 foreach tran $tranManualList { puts " Index= $index tran= $tran" incr index } puts "----End of Manual Transactions ---------------" puts "Select index of transaction to commit/rollback:" set input [gets stdin] if {$input < 0} { puts "No index selected, exiting." } else { set tran [lindex $tranManualList $input] set commaPos [expr [string first "," $tran ]-1] set localTID [string range $tran 0 $commaPos] puts "Enter c to commit or r to rollback Transaction $localTID" set input [gets stdin] if {$input=="c"} { puts "Committing transaction=$localTID" $AdminControl invoke $mbean commit $localTID } if {$input=="r"} { puts "Rolling back transaction=$localTID" $AdminControl invoke $mbean rollback $localTID } } } else { puts "No Manual transactions found, exiting" set input -1 } puts " " }Working with manual transactions: example Jython script.
import sys def wsadminToList(inStr): outList=[] if (len(inStr)>0 and inStr[0]=='[' and inStr[-1]==']'): tmpList = inStr[1:-1].split(" ") else: tmpList = inStr.split("\n") #split for Windows or Linux for item in tmpList: item = item.rstrip(); #remove any Windows "\r" if (len(item)>0): outList.append(item) return outList #endDef servicembean = AdminControl.queryNames("type=TransactionService,*" ) mbean = AdminControl.queryNames("type=Transaction,*" ) input = 0 while (input >= 0): tranList = wsadminToList(AdminControl.invoke(servicembean, "listManualTransactions" )) tranLength = len(tranList) if (tranLength > 0): print "----Manual Transaction details---------------" index = 0 for tran in tranList: print " Index=" , index , " tran=" , tran index = index+1 #endFor print "----End of Manual Transactions ---------------" print "Select index of transaction to commit/rollback:" input = sys.stdin.readline().strip() if (input == ""): print "No index selected, exiting." input = -1 else: tran = tranList[int(input)] commaPos = (tran.find(",") -1) localTID = tran[0:commaPos+1] print "Enter c to commit or r to rollback transaction ", localTID input = sys.stdin.readline().strip() if (input == "c"): print "Committing transaction=", localTID AdminControl.invoke(mbean, "commit", localTID ) #endIf elif (input == "r"): print "Rolling back transaction=", localTID AdminControl.invoke(mbean, "rollback", localTID ) #endIf else: input = -1 #endelse #endElse else: print "No transactions found, exiting" input = -1 #endElse print " " #endWhile
Related tasks
Manage active and prepared transactions
Use the transaction service