The information in this article applies to:
SUMMARY
Beginning with MFC 4.2, it is possible to use the MFC ODBC classes within
the multithreaded environment of an ISAPI DLL. Versions of MFC ODBC classes
prior to Visual C++ 4.2 were not thread-safe and should not be used in a
ISAPI DLL.
MORE INFORMATIONGeneral Rules for Using MFC ODBC in a ISAPI DLLAn ISAPI DLL is running as part of an NT Service. Therefore, all the rules and best practices using MFC ODBC in a NT Service apply. Specifically:
How ISAPI and Internet Information Server WorkInternet Information Server (IIS) maintains a thread pool to handle requests made to an ISAPI DLL. If a request is made of an ISAPI DLL, the thread will call the DLL's entry point (HttpExtensionProc) and follow it to whatever the extension does until it returns. If multiple users are hitting the page, then the DLL entry point gets called by an additional thread before the prior thread (or threads) have returned.If a user hits "refresh" repeatedly, the browser ignores the responses to any outstanding requests and submits a new request. Because of the "stateless" nature of HTTP, there is no way for the server to know that the browser is not still waiting for the request. Therefore, when a user hits "refresh" repeatedly, he or she is emulating a large group of users hitting the DLL simultaneously. You can use CCriticalSection to provide safety in your code, and hit the refresh button of a browser repeatedly to verify that your code and its underlying components are thread-safe. Use of Critical Sections will help create thread-safe code, but it also will impact performance of the ISAPI DLL. If a user keeps hitting refresh, more and more threads will be blocked waiting for the original thread (and then successive threads) to exit the critical section. Debugging TipsThere is a separate Microsoft Knowledge Base article that lists useful debugging tips for ISAPI DLLs. However, the following three areas stand out:Tip 1: IIS and Caching of ISAPI DLLs Internet Information Server will load an ISAPI DLL the first time the DLL is called, and will then cache it. It will not unload the ISAPI DLL until IIS stops. When you are making frequent changes to an ISAPI DLL, it can be time consuming to start and stop IIS with each rebuild of the DLL. There is a registry entry, however, that will force IIS to flush the ISAPI DLL from memory after it is no longer used. Consult the IIS 2.0 on-line documentation, Chapter 10 "Configuring Registry Entries," for details on how to use the CacheExtensions registry entry. Note that CacheExtensions should only be used for debugging and not in a production environment for performance reasons. Tip 2: Validating Thread Safety with a Console Application You may also want to validate your code in a multithreaded environment other than an ISAPI DLL. Sample code is provided below for a console application that spawns threads and performs database operations within them. This will help determine if a problem is in your code or in the underlying ODBC components used by your code. Debugging a console application can also be easier than debugging a ISAPI DLL. For tips on using MFC ODBC in a console application, see the REFERENCES section below. Tip 3: Identifying the Reason for Network Errors When attempting operations with MFC ODBC, you may get an exception thrown with information such as this:
A error message with "[Error=1326]" usually relates to a network error. You won't find these error numbers explained in the ODBC documentation. For the sample message given above, you can discover what the network error is with the following command: net helpmsg 1326 Sample CodeSample 1: Using CCriticalSection within an ISAPI DLLThe following sample code demonstrates how to use a global instance of CCriticalSection to protect use of MFC ODBC classes with an MFC ISAPI DLL:
Sample 2: Validating Code in a Console Application
The console application below emulates the multithreaded environment of an ISAPI DLL. This provides a simple way to debug your code as well as verify that the code and its underlying ODBC components are thread-safe. Use the following steps to build this test code:
REFERENCES
Q156138 HOWTO: Use MFC ODBC/DAO, DAO SDK in NT Service or with ThreadsFor more information about MFC in a console application, please see the following articles in the Microsoft Knowledge Base: Q152696 HOWTO: Use the MFC Database Classes in Console ApplicationsFor more information about debugging tips for ISAPI DLL, please see the following article in the Microsoft Knowledge Base: Q152054 Tips for Debugging ISAPI DLLs Additional query words: MfcDatabase kbusage kbMFC kbISSAPI kbVC400 kbVC500 kbVC600 kbODBC
Keywords : kbService |
Last Reviewed: October 4, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |