Wednesday, December 27, 2017

Reset tts level - An unbalanced x++ ttsbegin/ttscommit pair has been detected

Sometimes a user may receive the message of "An unbalanced X++ ttsbegin/ttscommit pair has been been detected"

The following job will clear the issue and reset the tts level. I did not create this job originally and it is wildly used if you do a simple search on the internet. I am just posting this on here because I find myself looking for the script every now again 

static void resetTTS(Args _args)
{
    if (appl.ttsLevel() > 0)
    {
        info(strFmt("Level %1 aborted", appl.ttsLevel()));
        ttsAbort;
    }
}

8 comments:

  1. This code is very misleading! ttsabort aborts any transaction and results in a ttslevel of 0. Your code (which is the same as http://www.theaxapta.com/2013/08/error-unbalanced-x-ttsbeginttscommit.html and http://natepaine.blogspot.de/2010/02/reset-tts-level-to-zero.html) suggests that ttsabort lowers the transactional level only by one - this is very dangerous for others...

    ReplyDelete
  2. Correct as with any job you need to understand the ramifications of running any custom code. In my cause I tend to use this in a scenario where a try/catch was not implemented correctly and no ttsabort code existed within the custom code therefore leaving ax in an unusable state. This police of code is literally everywhere on the internet which is why I do not claim it as my own. I use this blog as a place holder for random projects I may work on that I tend to use as a reference

    ReplyDelete
  3. May I ask what made you think it would only reset the tts level by 1 when the code is in a while loop so if it is at level 5 it would walk it back to 0?

    ReplyDelete
  4. The problem is you do not need the loop - calling ttsabort in a job / runnable class is fine. The code example suggests a different behavior of ttsabort than it actually has.

    ReplyDelete
  5. // reset any open transaction
    if (appl.ttsLevel() > 0)
    {
    ttsabort;
    }

    ReplyDelete
  6. Interesting I've experienced the issue where I had to issue multiple ttsabort's to walk it back to 0 (example pressing play/run on a job 5x) May I ask what version you are running? Also if you do a search for 'An unbalanced X++ TTSBEGIN?TTSCOMMIT pair has been detected' you will find this same code and not just a single ttsabort. However a single would work if the level was 1. Are you confused on what this does because I said it will reset the ttslevel instead of something like it will abort the current transactions and walk the tts level back to 0 to correct an unbalanced ttsbegin/ttscommit transactional statement?

    ReplyDelete
  7. After further testing you are correct Volker the while loop is unnecessary. Although it only executes once because the tts level is no longer 0 it is overkill and not optimized and I like optimized code.

    ReplyDelete