Query in Thread

Discussion of open issues, suggestions and bugs regarding LiteDAC (SQLite Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Schlueter
Posts: 16
Joined: Thu 29 Aug 2013 06:57

Query in Thread

Post by Schlueter » Thu 29 Aug 2013 07:10

Hi,

I want to execute a TLiteQuery in a TThread (Delphi XE3) for updating tabledata. At same time there will be probably a query in the main thread (for same or different table). Can I use the same TLiteConnection for both threads? Is it Thread-Safe?

Should I use separate TLiteConnections, one for the main thread and one for the background thread, if I want to protect the background query to be blocked by the main queries?

Best regards
Rolf Schlueter

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Query in Thread

Post by AlexP » Thu 29 Aug 2013 09:15

Hello,

Yes, LiteDAC allows work in threads via one connection: for parallel access to a DB from different threads, extended options were added to delimit simultaneous access to a DB: BusyTimeout, EnableSharedCache, ReadUncommitted. You can find more detailed information on these options in the LiteDAC help.

P.S. Moreover, SQLite thread-safety depends on the value of the SQLITE_THREADSAFE option, that was set when building the SQLite library. In the Direct mode, we use the value "1" supposing full thread-safety.You can refer to the SQLite developers site for more detailed information: http://www.sqlite.org/compile.html#threadsafe ,
http://www.sqlite.org/threadsafe.html .

Schlueter
Posts: 16
Joined: Thu 29 Aug 2013 06:57

Re: Query in Thread

Post by Schlueter » Thu 29 Aug 2013 11:45

Thanks a lot for the fast answer

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Query in Thread

Post by AlexP » Thu 29 Aug 2013 11:50

Hello,

If you have any other questions, feel free to contact us.

Schlueter
Posts: 16
Joined: Thu 29 Aug 2013 06:57

Re: Query in Thread

Post by Schlueter » Tue 17 Sep 2013 16:43

Hi,
if I perform an UPDATE on a table in the one thread, and at same time a SELECT on a (different) table in another thread, sometimes I get the exception 'database is locked' and a rollback is performed automatically.
How can I prevent my application from this exception, perhaps with a TCriticalSection?
What settings do you recommend for BusyTimeout, EnableSharedCache and ReadUncommited?

Best regards
Rolf Schlueter

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Query in Thread

Post by AlexP » Thu 19 Sep 2013 09:33

Hello,

You can set the ReadUncommitted option to True and increase the BusyTimeout property value.

Bestgtester
Posts: 1
Joined: Mon 29 Dec 2014 12:21

Re: Query in Thread

Post by Bestgtester » Mon 29 Dec 2014 12:31

Hello,

could you provide source code to see TLitequery executed in a thread.
As far I know, TLitequery is a TComponent (VCL), so it should not be declared in a thread
and not even used in a thread. This is the same problem with TLiteSql (=TComponent) but in the demo you provide, it seems that you can execute it in a thread and I don't understand how it is possible. Do you use "synchronize" method ?

Thanks for your help.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Query in Thread

Post by AlexP » Tue 30 Dec 2014 11:22

Please describe in more details the behavior you want to implement in threads - and we will try to help you resolve the issue.

Post Reply