#1 2016-09-29 08:25:54

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

About thread-safety about using TSQLRestBatch with TSQLRestServerDB

Hello ab,

I have recently noticed this code example:

https://github.com/synopse/mORMot/blob/ … hIndex.dpr

Which you specifically wrote for answering a stackoverflow.com question - you have broaden my view on the mORMot's capability with two of your answers there, thanks smile

One quick question, I read some of your code:

var
  db: TSQLRestServerDB;

  procedure DoInsert;
  var i: integer;
      rec: TSQLEntry;
      batch: TSQLRestBatch;
  begin
    rec := TSQLEntry.Create;
    batch := TSQLRestBatch.Create(db,TSQLEntry,10000,[boExtendedJSON]);
    try
      for i := 0 to COUNT-1 do begin
        ......
        batch.Add(rec,true);
      end;
      db.BatchSend(batch);
    finally
      ......
    End;

Assume I have multiple threads doing the same insertion, each thread has its own TSQLRestBatch instance, but all are operating on the same TSQLRestServerDB instance. Is it thread-safe to do that?

Thanks.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#2 2016-09-29 09:43:50

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

Re: About thread-safety about using TSQLRestBatch with TSQLRestServerDB

Yes, since each TSQLRestBatch will be processed one at a time, in its own sequential BatchSend execution.

Offline

#3 2016-09-29 10:10:23

edwinsn
Member
Registered: 2010-07-02
Posts: 1,218

Re: About thread-safety about using TSQLRestBatch with TSQLRestServerDB

Thanks ab, very helpful!


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#4 2020-08-08 19:33:41

Vitaly
Member
From: UAE
Registered: 2017-01-31
Posts: 168
Website

Re: About thread-safety about using TSQLRestBatch with TSQLRestServerDB

A clarifying question:
Should it work the same thread-safe with BatchSend and parallel CRUD operations (at the same time from other threads) with the same TSQLRestServerDB?
So far it seems that we're getting problems particularly in such situation (Zeos, FB 2.5.9):

20200808 19261957  +            uNextDB.TNextDB(03eed790).EngineBatchSend  inlen=585
20200808 19261957  +                    SynDBZeos.TSQLDBZEOSConnection(020f6610).StartTransaction
20200808 19261957 SQL           SynDBZeos.TSQLDBZEOSStatement(020b76d0) ExecutePrepared 104us select AVTALETEKSTID as ID,AVTALETEKST as AptText from AVTALETEKST where AVTALETEKSTID=55
20200808 19261957 DB            SynDBZeos.TSQLDBZEOSStatement(020b6e10) Prepare 35us select SMSID as ID,SMSId,TLF as PhoneNumber,SMSText,DATO as SMSDate,TID as SMSTime,TimeID from SMSOUTBOX where TimeID=?
20200808 19261957 EXCOS         EExternalException (e06d7363) [TNextHttpSvr 8210/THttpApiSvr] at 7fff427a3e49
20200808 19261957  -                    00.007.439

It highly depends on the load. But if we change everything from TSQLRestBatch.Add/Update/...  to using TSQLRestServerDB.Add/Update/... all goes fine.
Does it mean that we should always choose between using Batches and "direct" CRUD methods to make it safe, or have a separate TSQLRestServerDB instance for working with Batches for example?

upd: just in case - settings we're using (although with other settings it seems working the same): USE_SYNCOMMONS, hard_commit, default tmThreadPool

Last edited by Vitaly (2020-08-08 20:05:25)

Offline

#5 2020-08-10 15:52:46

Vitaly
Member
From: UAE
Registered: 2017-01-31
Posts: 168
Website

Re: About thread-safety about using TSQLRestBatch with TSQLRestServerDB

Vitaly wrote:

upd: just in case - settings we're using (although with other settings it seems working the same): USE_SYNCOMMONS, hard_commit, default tmThreadPool

Sorry for the misleading information. We've found in code that there was tmMainConnection ThreadingMode set.
We need to check it more, but so far with tmThreadPool all seems to work fine. My bad sad

Offline

#6 2020-08-10 17:12:34

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

Re: About thread-safety about using TSQLRestBatch with TSQLRestServerDB

tmMainConnection will use the very same connection from all threads, so it should be unsafe from multiple threads.

Offline

Board footer

Powered by FluxBB