Definitely an issue that arises more often than not in real world applications, and definitely one that pops up on our forum from time to time.
Firstly, a bit of the fundamentals. MyComponents are derived from the standard Borland TCustomConnection and TDataset classes, and it is therefore important to realize that if these base classes' methods are not thread protected (using synchronization), neither are those of the MyComponents. That said, the MyComponents are quite safe to use in an isolated manner from and within threads (TThread). Moreover, the MyComponents classes actually makes special provision for you to create threaded connections to the MySQL server.
Also to be kept in mind, is that all Borlands' GUI VCL runs in the main application thread, and therefore is it not possible to create a dataset in a thread and have it be connected to a DBGrid for example. However, the TMySQLDataset does have a thread open command called Execute, which will effectively open the dataset in a different thread, and then only enable itself for GUI VCL access, see your MyComponents documentation
on this method and more.
So, what does all this mean in layman's terms? Well, in short, if your thread "owns" the TMySQLDataset/Query, then should you have no problem using it in a normal way from the thread. You can do this in a number of ways, ex:
* A synchronized "pool" of datasets from which a TThread might "borrow" a dataset in an organized manner (you can even use the TMySQLServer's flying datasets
for this, see articles on this forum)
* Another way is to create the actual dataset in the thread itself, i.e. as a local thread variable.
There are only two rules for using a dataset in a thread:
1. You absolutely must
set the ShareConnection property to False, so that it will create itself a new MySQL connection and not share a connection with other thread and non-threaded datasets or the TMySQLServer
2. You absolutely have
to open it only in the TThread.Execute method in order for it to open a MySQL connection only at that time using the thread's ThreadID and ProcessID to do so.
If you do this, will you have only joy with MyComponents and threads. A lot more information is available on this forum (simply do a search for "thread") as well as various TMySQLDataset properties in your MyComponents manual
, ex ShareConnection
, Execute method
, Options property