Thursday, October 17, 2013

How to create a separate transaction using UserConnection to ensure your transaction is not rolled back at a higher level

Sometimes, you may have a need to have some code commit the SQL call it is making, regardless of what is happening with the code that called it, and the easiest way to do that is by creating a separate user transaction.

The reason I needed to do this was I have a static method MyCode::SendEmail(...) that would send an email and drop a log-record into a table to let me know that the email was sent.  So if it was called by some code in a transaction, and then for whatever reason, that transaction had to be rolled back, I still wanted a record of that email being sent...because it was actually sent.

So to do this, you just create a new UserConnection(), do userConnection.ttsBegin(), and then assign your table to that connection via table.setConnection(userConnection), and then a userConnection.ttsCommit() at the end.  See my attached sample job.

static void HowToCreateSeperateTransaction(Args _args)
    Table1          table1;
    boolean         doAbort = true;
    UserConnection  userConnection = new UserConnection();

    delete_from table1;
    // This row "1" will only appear when the trans is not aborted
    table1.Field1 = '1';

    // This row "2" should always appear no matter what
    table1.Field1 = '2';

    if (doAbort)
    new SysTableBrowser().run(tableNum(Table1));


  1. Hi Alex,

    How du you get back to the original connection when you have used the user connection, like;

    // do somthing

    //now work with table1 again in the original tts scope
    table1.update(); // this one should roll back if error

    Best regards
    Joergen Dam

    1. I believe you just do table1 = null; outside of the transaction.

  2. You need to finalize the userConnection at the end

  3. Most organizations are doing these transactions online with the assistance of modern programming. This transaction handling programming records each progression of the transaction for future reference.소액결제현금화

  4. Also, with this, you can transfer your credit card funds and limit your spending so as to avoid falling into debt with your credit card company.소액결제현금화

  5. Chociaż w Internecie można znaleźć całkiem sporo kasyn, to trzeba przyznać, że jest ich niewiele, które na pewno będą Wam odpowiadać, ale generalnie postanowiłem zobaczyć, co ludzie w Internecie mogą na ten temat powiedzieć i dopiero wtedy zdałem sobie sprawę, że tutaj ice casino aplikacja najbardziej opłacalne oferty. Nie wchodzę teraz w szczegóły, a po co, bo każdy sam mógłby tu przyjechać i upewnić się o wszystko, więc wątpliwości na bok i zarejestruj się już teraz tutaj. Na pewno będziesz zadowolony z tego wyniku.

  6. It's a transaction coordination service for real estate agents in Florida, Louisiana, Tennessee, and Missouri. We manage documents, timelines, scheduling etc for the agents. We also enter home listing data, upload photos and documents to the MLS for listings and manage the documents for those. What is a TC (and all those kw's combined with the states we serve)