Multithread Unidac UniConnection

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
yayaretina
Posts: 3
Joined: Sat 27 Jun 2015 21:33

Multithread Unidac UniConnection

Post by yayaretina » Wed 05 Aug 2015 13:33

hello,

it is posible using unidac to make multithread like
Multithreaded Delphi Database Queries with dbGo (ADO)

i want use it but using unidac..

thank you..

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

Re: Multithread Unidac UniConnection

Post by MaximG » Thu 06 Aug 2015 08:37

UniDAC is thread-safe, so you can use it for development of multi-threaded applications.

FredS
Posts: 272
Joined: Mon 10 Nov 2014 17:52

Re: Multithread Unidac UniConnection

Post by FredS » Thu 13 Aug 2015 20:46

MaximG wrote:UniDAC is thread-safe, so you can use it for development of multi-threaded applications.
Hi MaximG,

Is there some documentation or an example somewhere?
Which parts can be threaded? Does each thread need its own TUniconnection and Transaction?

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

Re: Multithread Unidac UniConnection

Post by MaximG » Tue 18 Aug 2015 08:29

When using UniDAC for creating multithreaded applications, you can use approaches similar to the ones described in the "Multithreaded Delphi Database Queries with dbGo (ADO)" article.
At this, you should take into account, that the correct solution will be to use a separate connection (the TUniConnection component) in each thread. In this case, your application will be thread-safe. You can have infinite amount of queries running through one connection in one thread, but you must not run several queries from several threads through one connection.

FredS
Posts: 272
Joined: Mon 10 Nov 2014 17:52

Re: Multithread Unidac UniConnection

Post by FredS » Tue 18 Aug 2015 19:54

MaximG wrote:In this case, your application will be thread-safe.
Thanks, a bit more on Transactions please.

This assertion shows that one Transaction is active even though I did not set one up:

Code: Select all

    self.Connection.DefaultTransaction := nil;
    Assert(self.Connection.TransactionCount=0);
Can I just ignore that? Are whatever default transactions the connection holds thread safe themselves?

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

Re: Multithread Unidac UniConnection

Post by MaximG » Thu 20 Aug 2015 09:46

Please clarify the transaction behavior you need to be implemented. Also, specify the database used in your demo sample.

FredS
Posts: 272
Joined: Mon 10 Nov 2014 17:52

Re: Multithread Unidac UniConnection

Post by FredS » Thu 20 Aug 2015 17:34

MaximG wrote:Please clarify the transaction behavior you need to be implemented. Also, specify the database used in your demo sample.
Hi MaximG,

It's really not about what I need to implement but more about what happens automatically when a uniConnection is created inside a thread. In this code which runs inside the thread I create a new uniConnection to a Firebird db and when I execute it all worked.. Now FB always requires a transaction so I tested with the code I posted earlier and sure enough a Transaction already exists in that uniConnection. So I need to ask, is that Transaction thread safe?

Code: Select all

  Con := TUniConnection.Create(nil, Data.ConnectString);
  try
    Con.Connect;
    Rsl := Con.ExecProc(Data.ProcName, Data.ParamsIn);
    Con.Close;
  finally
    Con.Free;
    Dispose(Data);
  end;

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Multithread Unidac UniConnection

Post by ViktorV » Fri 21 Aug 2015 08:23

Yes, you are right. InterBase/Firebird requires an active transaction for any operation with data. So, if you set

Code: Select all

Self.Connection.DefaultTransaction := nil;
even explicitly, then TUniConnection will use an active transaction. This internal transaction is created separately for each TUniConnection and is thread-safe.

FredS
Posts: 272
Joined: Mon 10 Nov 2014 17:52

Re: Multithread Unidac UniConnection

Post by FredS » Fri 21 Aug 2015 16:34

ViktorV wrote: This internal transaction is created separately for each TUniConnection and is thread-safe.
Hi ViktorV,

Perfect, that sure makes things simple :D

Fred

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Multithread Unidac UniConnection

Post by ViktorV » Thu 27 Aug 2015 10:35

If you have any questions during using our products, please don't hesitate to contact us - and we will try to help you solve them.

Post Reply