#1 2023-01-24 12:56:33

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 274

is zeos and mysql threadsafe with mormot?

This is the result from a procedure running from a TSynBackgroundTimer using zeos 8.0-fixes trunk and MariaDB
after a fresh start of my program, no exception was reported previously from TsynLog

Exception OS: 20230124 11362514  ! EXCOS EAccessViolation (c0000005) [pchr BgdTmr] at 7328b162 
ZDbcMySqlStatement.pas TZAbstractMySQLPreparedStatement.InternalRealPrepare (1441)
ZDbcMySqlStatement.pas TZAbstractMySQLPreparedStatement.Prepare (697)
ZDbcMySqlStatement.pas TZAbstractMySQLPreparedStatement.ExecuteQueryPrepared (1048)
mormot.db.sql.zeos.pas TSqlDBZeosStatement.ExecutePrepared (1179)
mormot.orm.sql.pas TRestStorageExternal.ExecuteDirect (1798)
mormot.orm.sql.pas TRestStorageExternal.TableHasRows (1443)
mormot.orm.server.pas TRestOrmServer.TableHasRows (1846)
mormot.core.threads.pas TSynBackgroundTimer.EverySecond (2481)
mormot.core.threads.pas TSynBackgroundThreadProcess.ExecuteLoop (2387)
mormot.core.threads.pas TSynBackgroundThreadAbstract.Execute (2075)

Zeos connection is created like the following, where zopts has only the location of LibMariadb.dll (32bit):

myzurl:=TZURL.Create('zdbc:mysql://',server,port,db,rootuser,rootpass,zopts);
dbmormot:=TSQLDBZEOSConnectionProperties.CreateWithZURL(myzurl,dMySQL,true);
dbmormot.ConnectionTimeOutMinutes:=450;
dbmormot.DeleteConnectionInOwnThread:=true;

I usually find a lot of exceptions that have to do with multithreading use of zeos connectio like the following:
https://gist.github.com/dkounal/095e653 … 48eb24ead5

Is something needed to zeos to work in threadsafe mode?
Thank you in advance

Last edited by dcoun (2023-01-26 18:10:34)

Offline

#2 2023-01-24 13:50:25

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 13,504
Website

Re: is zeos and mysql threadsafe with mormot?

Please follow the forum rules and don't put too much text in the forum posts, like source or stack traces.

There is not enough information here.
How do you create the connection instance? A single connection is not thread safe: you have to use TSqlDBZeosConnectionProperties.ThreadSafeConnection (or NewThreadSafeStatementPrepared or similar) to retrieve each connection in each thread.

Offline

#3 2023-01-24 22:51:14

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 274

Re: is zeos and mysql threadsafe with mormot?

mm=TOrmModel.Create([Tormtable1, Tormtable2],'url');
OrmMapExternal(mm,Tormtable1,dbmormot,'table1');
OrmMapExternal(mm,Tormtable2,dbmormot,'table2');
srv:=TRestServerDB.Create(mm);

srv will be used by different threads. Is this my mistake?

Can I use also from different threads the following?

dbmormot.executeinlined('an sql command',false);

Last edited by dcoun (2023-01-24 22:51:50)

Offline

#4 2023-01-25 21:19:49

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 274

Re: is zeos and mysql threadsafe with mormot?

I have read the following:
https://synopse.info/forum/viewtopic.php?id=3352
https://synopse.info/forum/viewtopic.php?id=5788
I am still not sure if an interface function should create or not, each time called, a TrestserverDB that maps tables in a external database. I am not using user authentication and sessions.
Can the same TrestserverDB instance (with tables mapped to an external database) be accessed from multiple threads? Should I use a lock when accessing it for read/write?
I have not understand the use of TSynDictionary in the above discussion, do you mean that a thread can pop up a TrestserverDB instance from TSynDictionary, during an interface call and re-insert it when finished?
Thank you in advance

Offline

#5 2023-01-27 11:25:21

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 274

Re: is zeos and mysql threadsafe with mormot?

Looking the source and using the debugger to trace, when using TSQLDBZEOSConnectionProperties, this is derived from TSqlDBConnectionPropertiesThreadSafe and together with Mariadb, this should not be a problem
Reading documentation...: https://synopse.info/files/html/Synopse … ml#TITL_25 in 11.7.1
I have added the following after creating TrestserverDB:
  srv.AcquireExecutionMode[execOrmGet]:=amBackgroundThread;
  srv.AcquireExecutionMode[execOrmWrite]:=amBackgroundThread;
Also I added in the end of the thread from TSynBackgroundTimer:
dbmormot.EndCurrentThread;

The above existed inside my code and was removed in the past.

Now I do not have the above errors but I still have seldomly errors like the following I reported in the past and probably it is a problem from zeos
https://synopse.info/forum/viewtopic.php?id=6376

My best wishes for the remaining of the week

Offline

Board footer

Powered by FluxBB