INF: Multithreaded Reentrancy and DB-Library

ID: Q99680


The information in this article applies to:
  • Microsoft SQL Server Programmer's Toolkit, version 4.2


SUMMARY

DB-Library functions and routines that access the same DBPROCESS are not reentrant across multiple threads. Therefore, be sure that you serialize all DB-Library calls that access the same DBPROCESS in multithreaded applications you develop.

In applications where each thread uses a separate DBPROCESS, it is not necessary to serialize the DB-Library calls. However, there is one important exception: you must serialize any calls that involve global variables. You can do this by using a synchronization method such as a flag variable, semaphore, or event.


MORE INFORMATION

The following guidelines will help you determine where to protect global variables:

  • Protect error and message handling functions installed by dberrhandle and dbmsghandle, because these are global to the entire DB-Library application. These should be protected with a separate synchronization object to avoid colliding with the protection around certain DB-Library calls below.


  • Protect DB-Library functions that do not take a DBPROCESS pointer or a DBCURSOR handle as the first parameter (for example, dberrhandle and dbsettime).


  • Protect DB-Library functions wherever you pass a null DBPROCESS pointer (for example, dbsetopt).


  • Protect the dbopen function if you do not use per-process error and message handlers (dbproerrhandle/dbprocmsghandle) and install them using the LOGINREC pointer (for example, dbprocerrhandle(pLogin, myErrorHandler);).


Examples for different platforms appear below.

For OS/2:


// Make this variable global to the entire application
   HSEM semDblib;

// This code would be in a thread that uses DB-LIBRARY
   DosSemRequest(&semDblib, SEM_INDEFINITE_WAIT);
   pDbproc = dbopen(pLoginRec, "myserver");
   DosSemClear(&semDblib); 

For the Win32 API:

// make this variable global to the entire application
   HANDLE hOpenEvent;

// create the event handle at application startup
// have it set on creation, with auto-reset
   hOpenEvent = CreateEvent(NULL,FALSE,TRUE,NULL);

// this code would be in a thread that uses DB-LIBRARY
// it waits for other threads to complete, opens a
// connection, then sets the event so other threads
// can continue
   WaitForSingleObject(hOpenEvent,INFINITE);
   pDbproc = dbopen(pLoginRec,"myserver");
   SetEvent(hOpenEvent);

// close the event handle at application exit
   CloseHandle(hOpenEvent); 

Additional query words: thread dblib db-lib

Keywords : kbprg SSrvDB_Lib SSrvProg
Version : WINDOWS:4.2
Platform : WINDOWS
Issue type :


Last Reviewed: March 16, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.