Monday 15 December 2014

How to resolve unbalanced ttsBegin/ttsCommit X++ statements

If you have written some code in AX that have an uneven number of ttsBegin/ttsCommit statements or while running the code, after the execution of ttsBegin, some run time error comes and the rest of code which contains the matching ttsCommit statement is not executed. This results in an AX error message of unbalanced X++ ttsBegin/ttsCommit pair.
To solve this error, one solution is just to restart the AOS service. Another solution is to execute the following code in a job.

static void FixTTS(Args _args) 

    while (appl.ttsLevel() > 0) 
    { 
        info(strfmt("Level %1 aborted",appl.ttsLevel())); 
        ttsAbort; 
    } 
}


To avoid this error in future, care must be taken to write equal number of ttsBegin and ttsCommit statements. Also, its better to use ttsBegin/ttsCommit in a try catch block. So that we can use the throw statement in case of an error. The throw statement automatically initiates a ttsAbort which is a database transaction rollback i.e all changes in the current transaction are being discarded.

No comments:

Post a Comment