Advantage Developer Zone

 
 
 

Achieve full concurrency in a multi-threaded Advantage application

Friday, July 14, 2006

When developing multi-threaded applications, it is important to note that all Advantage database operations associated with a single connection are serialized. If multiple threads try to perform database operations on tables associated with a single connection simultaneously, the execution of the tasks will be serialized and the database operations on the threads will not run concurrently. Only one thread will run at any one time, and the next thread will not run until the previous one has completed its task. If that task takes a long time, such as a lengthy SQL query or building an index, the other threads may be blocked for an unacceptably long period of time. For example, if an Advantage application has a single connection to an Advantage Server, two threads exist (thread-A and thread-B), two tables are open on the single connection (table-A and table-B), and index creation operations are performed on table-A in thread-A and on table-B in thread-B, those index creation operations will not occur concurrently. The Advantage connection will send one of the index creation operations to the Advantage server to be performed. Once that index creation operation has completed, the other index creation operation will be sent to the Advantage server to be performed. Even though the Advantage application has multiple threads, the two operations will occur sequentially because the threads are sharing a single Advantage connection.

To achieve full concurrency of Advantage database operations on multiple threads, perform those database operations on separate Advantage connection instances in each thread. If you have X threads in your application, obtain X Advantage connections in your application, and perform the database operations on a different connection for each thread. For example, if an Advantage application has two connections (connection-A and connection-B) to an Advantage Server, two threads exist (thread-A and thread-B), one table is open on each connection (table-A opened on connection-A and table-B opened on connection-B), and index creation operations are performed on table-A in thread-A and on table-B in thread-B, those index creation operations will occur concurrently. Both Advantage connection-A and Advantage connection-B will send the index creation operations to the Advantage server to be performed immediately. The Advantage server will perform those index creation operations concurrently. Furthermore, if multiple CPUs exist on the server, those index creation operations will occur in parallel.

With the Advantage TDataSet Descendant, you can obtain additional connections using the TAdsConnection component. With the Advantage OLE DB Provider and the Advantage ODBC Driver, you can obtain additional connections by instantiating additional connection objects.