Recent

Author Topic: sqldb and threads  (Read 4903 times)

Max V. Terentiev

  • New Member
  • *
  • Posts: 30
sqldb and threads
« on: June 28, 2017, 02:29:22 pm »
Hi,

I need to read/write SQLite database from several threads.

Should I create TSQLite3Connection in each thread ?

Or I can use single connection in many threads ? Should I use CriticalSections in this case ?

Thanks for help !

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: sqldb and threads
« Reply #1 on: June 28, 2017, 04:21:27 pm »
not all functions of sqlite are thread safe .... create several connections in a pool and use them in the threads... do not use critical sections on one connection because if the app is heavely db active you will loose the advantages of multithreading ...
one possible solutions is to serialize the access to sqlite in one thread and use a event mechanisme to signal the threads that the db operations are executed ...
Speak postscript or die!
Translate to pdf and live!

Max V. Terentiev

  • New Member
  • *
  • Posts: 30
Re: sqldb and threads
« Reply #2 on: June 28, 2017, 05:23:01 pm »
Sqlite have different compile-time threading options, including serialized where one connection can be shared betweern threads and multithreaded where each thread must have own connection.

But what about sqldb ? Is it threadsafe ?

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: sqldb and threads
« Reply #3 on: June 28, 2017, 06:46:52 pm »
no idea about sdldb... why not use raw sqlite.dll? you can take a look at rqlite witch is a distribited sqlite and acess i rest based
Speak postscript or die!
Translate to pdf and live!

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: sqldb and threads
« Reply #4 on: June 30, 2017, 10:57:58 pm »
But what about sqldb ? Is it threadsafe ?
AFAIK it just relies on the underlying DBMS.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: sqldb and threads
« Reply #5 on: June 30, 2017, 11:16:45 pm »
But what about sqldb ? Is it threadsafe ?
AFAIK it just relies on the underlying DBMS.
That is not enough. For example lcl relies on the underline widgetset as well but it is not thread safe, even when the underline widgetset is.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Max V. Terentiev

  • New Member
  • *
  • Posts: 30
Re: sqldb and threads
« Reply #6 on: July 03, 2017, 02:15:55 pm »
But in the end. Should I create should I create TSQLite3Connection in each thread ? Or I can use (in threads) single connection from main DataModule ?


mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: sqldb and threads
« Reply #7 on: July 04, 2017, 08:26:46 am »
You have to create TSQLite3Connection in each thread.
But be carefull. As I mentioned earlier, I don't know is the database will be locked for another transaction is the database is opened.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Max V. Terentiev

  • New Member
  • *
  • Posts: 30
Re: sqldb and threads
« Reply #8 on: July 06, 2017, 01:22:26 pm »
Thanks for help !

 

TinyPortal © 2005-2018