#1 2024-06-04 12:13:44

jienyuan
Member
Registered: 2017-07-22
Posts: 5

External table BatchSend raises SharedTransaction exception.

I have two external SQL Table. Everything works fine when I use ORM add/update directly.

However, when I try using Batch to control transaction, exception occurs after BatchSend.

I am wondering whether is Batch control suitable for external tables?

Below is the code and log capture. Any idea is appreciated.

Thank you.

var
  aUId: RawUtf8;
  aBatch: TRestBatch;
  aLog: TOrmMyLog;
  aUser: TOrmUTbl;
begin
  aUId:= TSQLMyAuthUser(Session.User).UId;
  aBatch:=TRestBatch.Create(Sender.Orm,nil,10000);
  aLog:=TOrmMyLog.New(Ctxt,'SESSION');
  with aLog do begin
    DATA_1:='True';
    Data_2:='STANDARD';
  end;
  //Sender.Add(aLog,True);
  aBatch.Add(aLog,True);
  aUser:=TOrmUTbl.Create;
  Sender.Retrieve(csFilter,[],[aUId],aUser);
  with aUser do 
  begin
      Updated:=20240604;
      Invalidates:=0;
      LoggedIn:=True;
      LastLogin:=Now;
  end;
  //Sender.Update(aUser,aFields);
  aBatch.Update(aUser,aFields);  
  Sender.BatchSend(aBatch);
end;
2024/6/4 11:34:53.624	Trace	    MyRestServer.TMyRestServerDB(04fff520) BatchSend {"TRestBatch(04bd3de0)":{Count:2,SizeBytes:212}}
2024/6/4 11:34:53.624	Enter	    mormot.orm.server.TRestOrmServerBatchSend(04b647e0).EngineBatchSend  inlen=212
2024/6/4 11:34:53.624	Enter	       mormot.db.sql.zeos.TSqlDBZeosConnection(04be4a10).StartTransaction
2024/6/4 11:34:53.624	Leave	       00.001.169
2024/6/4 11:34:53.624	SQL	       mormot.db.sql.zeos.TSqlDBZeosStatement(0509b080) Execute t=1.99ms wr=1 q=insert into dbo.MyLOG (ID,LOGTIME,OPERATOR,ADDR,LOGACTION,DATA_1,DATA_2,MACHINEID,HOSTNAME) values ([32...],['2024-06-04T19:34:47.267'...],['USR'...],['127.0.0.1'...],['SESSION'...],['True'...],['STANDARD'...],['0'...],[''...])
2024/6/4 11:34:53.624	Cache	       mormot.db.raw.sqlite3.TSqlDatabase(04be4668)  cache flushed
2024/6/4 11:34:53.624	DB	       mormot.db.sql.zeos.TSqlDBZeosStatement(0509b350) Prepare t=530us q=update dbo.UTBL set UPDATED=?,LOGGEDIN=?,INVALIDATES=?,LASTLOGIN=? where ID=?
2024/6/4 11:34:53.624	SQL	       mormot.db.sql.zeos.TSqlDBZeosStatement(0509b350) Execute t=5.87ms wr=1 q=update dbo.UTBL set UPDATED=['20240604'...],LOGGEDIN=[1...],INVALIDATES=[0...],LASTLOGIN=['2024-06-04T19:34:47'...] where ID=[2...]
2024/6/4 11:34:53.768	Exception	       ESqlDBException {Message:"Unexpected TSqlDBZeosConnectionProperties.SharedTransaction(1,2)",Statement:null} [R0:MyServices] at 6852ff mormot.core.text.pas ESynException.RaiseUtf8 (9240)  
2024/6/4 11:34:59.880	Exception	       ESqlDBException {Message:"Unexpected TSqlDBZeosConnectionProperties.SharedTransaction(1,3)",Statement:null} [R0:MyServices] at 6852ff mormot.core.text.pas ESynException.RaiseUtf8 (9240)  

Last edited by jienyuan (2024-06-04 12:16:33)

Offline

#2 2024-06-04 13:02:47

Márcio Baroni
Member
From: Brasil
Registered: 2015-10-07
Posts: 34

Re: External table BatchSend raises SharedTransaction exception.

I have the same problem in the same scenario...
I solved it by changing the function "TSqlDBConnectionProperties.SharedTransaction" em "mormot.db.sql.pas"

diff --git "a/src/db/mormot.db.sql.pas" "b/src/db/mormot.db.sql.pas"
index 27e1c0b5..a239890b 100644
--- "a/src/db/mormot.db.sql.pas"
+++ "b/src/db/mormot.db.sql.pas"
@@ -3721,7 +3721,9 @@ begin
               MoveFast(fSharedTransactions[i + 1], t^, (n - i) * SizeOf(t^));
               SetLength(fSharedTransactions, n);
               found := true;
-            end;
+            end
+            else
+               exit;
           end;
           break;
         end

Offline

#3 2024-06-04 13:16:49

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: External table BatchSend raises SharedTransaction exception.

Nice catch.

Please try
https://github.com/synopse/mORMot2/commit/8ba58c7d

Thanks a lot for the feedback.

Offline

Board footer

Powered by FluxBB