How to properly use unidac in a multithreading environment?

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
tommy
Posts: 11
Joined: Thu 21 Jul 2016 21:52

How to properly use unidac in a multithreading environment?

Post by tommy » Mon 12 Sep 2016 15:50

Hello,

please let me ask some questions about how to use the UniDAC components in a multithreading application.

First: What is the proper way of using TUniLoader? Do I have to do some commit after calling Load? Currently, I use the following code:

Code: Select all

conn := TUniConnection.Create (NIL);
conn.ProviderName := 'SQLite';
conn.Database := ...
conn.SpecificOptions.Values['ForceCreateDataBase'] := 'True';
conn.SpecificOptions.Values['DateFormat'] := 'yyyy-mm-dd';
conn.SpecificOptions.Values['TimeFormat'] := 'hh24:mi:ss';
conn.SpecificOptions.Values['UseUnicode'] := 'True';
conn.SpecificOptions.Values['EnableSharedCache'] := 'True';
conn.AutoCommit := TRUE;
conn.Connect;
loader := TUniLoader.Create (NIL);
loader.Connection := conn;
loader.TableName := 'xyz';
loader.CreateColumns;
loader.OnPutData := @OnSaveSunPath; // data is inserted there
loader.Options.QuoteNames := TRUE;
loader.Load;
loader.Free;
conn.Free;
After closing the database, it does not contain all data, but only 269000 out of 269208. TUniLoader itself does not contain a commit method so do I need conn.Commit after loader.Load? It works, but I get other problems when I do that.

Second: According to this document Sqlite should by default operate in serialized mode which is thread-safe without any restrictions. For speed reasons, I wanted to create only a single instance of TUniConnection and use it within all threads. However, when concurrent database writes occur, I get frequent errors "cannot commit - no transaction is active". This happens regardless of whether or not I use manual commits.

So my question is: do I have to create a new instance of TUniConnection in each thread?

It would be kind if I could get some hints about what I'm doing wrong. Thank you.

MaximG
Devart Team
Posts: 1822
Joined: Mon 06 Jul 2015 11:34

Re: How to properly use unidac in a multithreading environment?

Post by MaximG » Wed 14 Sep 2016 08:38

We have answered you in the forum. Please view the following topic:
viewtopic.php?f=28&t=34009

Post Reply