1

I wanted to check into this before I attempt it. I am working on a couple threads which require fetching data from the database within the threads. Currently, based on all I've seen, I am creating a new database connection (TADOConnection) from within the threads. All works fine, except it would be great if I could obtain the connection object from somewhere outside the thread. Basically, I don't want to keep creating a new TADOConnection for each thread execution.

Is it possible to publish a TADOConnection property on the outside of the thread (thus assigning it when I create the thread) and then use that connection within the thread? All I would need to do then is create the TADODataSet inside the thread and assign its connection to this TADOConnection. I'm a little iffy about this, especially because I'm required to call CoInitialize and CoUninitialize when working with ADO in a thread.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
  • 4
    possible duplicate of [Is Delphi's TADOConnection thread-safe?](http://stackoverflow.com/questions/1241844/is-delphis-tadoconnection-thread-safe) Also, you *always* have to call `CoInitialize/CoUnitialize` in every individual thread that uses COM, as COM is thread context specific. – Ken White Nov 08 '11 at 20:27
  • Well, would it matter if the threads/s just created a TADOConnection and, well, just hung on to it for the duration of the app run? Do you need to continually create/destroy these threads? – Martin James Nov 08 '11 at 20:59
  • Already have my answer - the thing is I am creating these threads on an as-needed basis, which could mean 10 threads at once, or 1 thread every half hour. They do free themselves, they don't stay active, once it completes I have no use in it anymore. I just wanted to see if I could avoid creating it many times. – Jerry Dodge Nov 08 '11 at 21:05
  • 3
    Pool your threads and make them reentrant so you can reuse threads instead of freeing them after each task. – Remy Lebeau Nov 08 '11 at 21:41
  • I am actually doing 2-layer threading. I have one main thread, which yes, it's continuing in a loop. But the other ones are only created on demand, and are never specifically for one subject. Basically it's sending single requests to the server, waiting for a response, then triggering an event when the response is received. Once I get that response, I don't need it anymore. Each of these requests are sent to the server from its own thread. For example, if I need 3 numbers, each takes a long time to calculate, I send 3 requests at the same time, thus spawning 3 threads. – Jerry Dodge Nov 08 '11 at 22:13
  • It's a reporting tool for an inventory/point-of-sale software - it may have to get the total amounts of sales in 2011, 2010, and 2009, so it creates a thread to get the totals for each year. Once all 3 numbers are received, I can then put those results on a chart, or do whatever else I need with them. There's a wide variety of possible numbers that could be generated with this, for example, amount of money refunded in the past 3 months. – Jerry Dodge Nov 08 '11 at 22:48
  • The overall goal is to be able to crunch numbers 5 times faster by calling 5 threads at once, each getting a particular number. The current model has to wait for all those numbers to be calculated before it can respond. – Jerry Dodge Nov 08 '11 at 22:51

1 Answers1

1

The answer looks like no. Because the TADOConnection (and other ADO components) are COM based, they cannot be passed across threads. So in this case, I have no choice but to create a new TADOConnection from within each thread. The threads which continue running make use of this connection each time it loops, but the single-run threads make use of it just once.

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327