Threading DbExpress Solutions

I only made access to the "GUI" database in Delphi with the DbExpress components, but now I would like to execute one query in the background. I read some where TSQLConnection is not thread safe and I need to create a new connection for each thread. I see that CloneConnection is in TSQLConnection, but it helps to claim that the new connections belong to the original connection.

So,

1) What is the correct way to execute TSQLQuery located in a data stream?

2) Can I use TSQLConnection.CloneConnection? Should I release the cloned connections or leave them intact when the thread runs out?

A simple code sample (or URL) will be very helpful.

+3
source share
1 answer

This does not apply to DbExpress (with which I have no experience), but the best idea for moving some functionality to the background thread is to first develop it in the main thread, debug it, and as soon as you make sure it is fine, then move it.

What you can do is put everything on the TDataModule. If the data module can work on its own, without needing any other components in other forms or data modules, and you create it in the background thread, then you can be sure that everything will work. You do not need to clone the connection, as it will be created and destroyed in the same thread where all access to the database will be performed.

Some important tips for working with databases:

  • , / . Application .

  • . , TDataSource , .

  • SendMessage PostMessage, .

  • OLE, OleInitialize() OleUnitialize() .

, OmniThreadLibrary AsyncCalls. , , .

+4

Source: https://habr.com/ru/post/1702627/


All Articles