The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Is ADO threadsafe?

We have a HTTP server made with Delphi that uses the Borland database engine (BDE). We want to replace the BDE with ADO. Is ADO threadsafe? I can't find anything to tell me that it is or isn't. With the BDE I had to use particular components and set specific properties to make it threadsafe. Do I have to do something similar with ADO?
cja Send private email
Monday, May 15, 2006
 
 
ADO is based on COM. As long as you follow the COM rules about threading (apartments and passing interface pointers between them) you don't need to worry about thread safety.

You may need to worry about performance, but that's a different question. ;-)
Chris Tavares Send private email
Monday, May 15, 2006
 
 
> ADO is based on COM. As long as you follow the COM rules about threading (apartments and passing interface pointers between them) you don't need to worry about thread safety.

> You may need to worry about performance, but that's a different question. ;-)

ADO supports free threading, so if your provider implemented a free-threaded object, then apartments shouldn't come into play, but you do have to know what you are doing.
newb uISV Send private email
Tuesday, May 16, 2006
 
 
You should be fine.

In the simple case, just don't share ADO connections between threads. You can do it in some instances, but that also depends on the OLEDB driver for the database in question.

Normally, you just call CoInitialize[Ex]() in the thread execute function, create your ADO connection, do what you need to to, close the connection, and then call CoUninitialize().

Make sure all ADO objects are destroyed before calling CoUninitialize(). ;-)
MBJ Send private email
Tuesday, May 16, 2006
 
 
If you're using an OLEDB provider to connect to your data source via ADO, you can follow two simple rules to take advantage of connection pooling:

1.) Use the exact same connection string on every connection object created in every thread.

2.) Keep at least one of those connection objects initialized (the connection should't be open, but the object has to be there) per process.

3.) Open each connection as late as possible and close it as early as possible.

Also, if you're just creating threads to query the database, you can let the connection take care of that by using asynchronous executions and client sided cursors (client sided is the most flexible).

If you need to pass recordsets between threads, you can disconnect them and serialize them first, then restore them in another thread after passing the serialized data over.
Wayne B Send private email
Wednesday, May 17, 2006
 
 
Sorry, 3 rules :)
Wayne B Send private email
Wednesday, May 17, 2006
 
 
Wayne, that's good advice, although it may not be applicable to a web server, as the primary thread of the web server typically knows nothing about what is going on in the CGI scripts, IDAPI DLLs, etc.
MBJ Send private email
Wednesday, May 17, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz