The information in this article applies to:
- Microsoft SQL Server Programmer's Toolkit, version 4.2
SUMMARY
DB-Library (DB-Lib) functions and routines that access the same DBPROCESS
are not reentrant across multiple threads. Therefore, be sure that you
serialize all DB-Lib 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-Lib 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-Lib functions that do not take a DBPROCESS pointer or a
DBCURSOR handle as the first parameter. For example, dberrhandle and
dbsettime.
- Protect DB-Lib functions wherever you pass a null DBPROCESS
pointer. For example, dbsetopt.
- Protect the dbopen function because it returns a DBPROCESS pointer.
DBPROCESS pointers can potentially be null and can therefore use
DB-LIBRARY global variables.
For example:
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);
|