[fpc-pascal] DB Access and Multithread

Michael Van Canneyt michael at freepascal.org
Fri Jan 30 23:13:53 CET 2015



On Fri, 30 Jan 2015, Fabrício Srdic wrote:

> Hello,
> 
> The DB access framework of the fpc is not thread safe.
> 
> So, how can i build a multithreaded application that requires access to database?

Create 1 connection per thread. 
Do not share connections (and datasets connected to it) over threads.

> If each one of my threads use its own set of DB access components, that is, don´t share any instance of db access components, this can
> be a secure workaround?

Yes, if they use a connection per thread.

The main problem is that the 'Database' and 'Transaction' properties of 
TDBDataset and descendents (TBufDataset and TSQLQuery) are not thread safe:
The TDatabase class maintains a list of connected datasets. 
The management of this list is not thread-safe, it needs to be changed to a 
TThreadList. This is on my todo list, but is part of a bigger rework.

Note that the underlying DB client library also may not be thread safe.

Michael.


More information about the fpc-pascal mailing list