sql-serverdelphitransactionsdevartunidac

Why do I get the following error when trying to do a TUniQuery.Post: "Cannot create new connection because in manual or distributed transaction mode"


I'm using Delphi 12.2 and SQL Server. I have code that was working perfectly before, and just started giving me this error when running:

Cannot create new connection because in manual or distributed transaction mode

This is my code:

RemoteConnection.StartTransaction; // A TUniConnection
QBatch.Open; // A TUniQuery
QBatch.Append;
QBatch.FieldByName('batch_data').AsInteger := BatchData;
QBatch.Post; // <-- error happens here!
RemoteConnection.Commit;

I do not understand why I get this error. I have confirmed that the QBatch.Connection property is set to RemoteConnection. This code was working. I don't see how I'm creating a new connection.

What could trigger this error?


EDIT:

As a little more information, this is in a separate thread from the main thread. I'm not sure if that could play into it, but there's only one place that RemoteConnection.Connect() is ever called and that's at the beginning of execution. I've confirmed that it is not getting called again.

Why would Post() try to create a new connection? Is there some sort of option that could be the cause?


EDIT 2:

Upon closer inspection, it appears this bug might have appeared when we updated from Delphi 11 to 12.2. I still don't understand why or how to fix it though.


Solution

  • I discovered the issue. Even though the error was happening on this line, it was caused higher up in the code in a section not in the example:

    ...
    QBatch.Open;
    QGetLastBatchNumber.Open;
    ...
    

    These were two TUniQueries that both accessed the same table called BATCH with different sql statements. So the error was actually that I was trying to make two connections to the same table in a transaction. I do not know why this wasn't an issue before Delphi 12.2 and I do not know exactly why the error triggered on QBatch.Post as opposed to when I opened both tables, but if you have a similar error, you can look to see if you have two different TUniQueries open that connect to the same table within a single transaction.