INF: Multithreaded Reentrancy and DB-LIBRARY

Last reviewed: April 28, 1997
Article ID: Q99680

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);


Additional query words: thread dblib
Keywords : kbprg SSrvDB_Lib
Version : 4.2 | 4.2
Platform : OS/2 WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 28, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.