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;
}
}
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...
ReplyDeleteCorrect 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
ReplyDeleteMay 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?
ReplyDeleteThe 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// reset any open transaction
ReplyDeleteif (appl.ttsLevel() > 0)
{
ttsabort;
}
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?
ReplyDeleteAfter 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.
ReplyDeletethanks for the input
ReplyDelete