Article ID: 160906 - Last Review: June 1, 2005 - Revision: 4.1 How to use MFC 4.2 ODBC classes in an ISAPI DLLThis article was previously published under Q160906 On This PageSUMMARY 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. The remainder of this article explores the use of MFC-based Database classes within an ISAPI DLL and includes the following:
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:
* [State=01000][Error=1326][Microsoft][ODBC SQLServer
Driver][dbnmpntw]ConnectionOpen (CreateFile()).
* [State=08001][Error=1326][Microsoft][ODBC SQL Server Driver]Unable
to connect to datasource.
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: 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:
REFERENCESFor more information about thread safety with MFC ODBC, please see: "ODBC Classes and Threads" in the Visual C++ Books On-Line, or the following article in the Microsoft Knowledge Base: 156138
(http://support.microsoft.com/kb/156138/
)
Limitations of DAO, DAO SDK in NT service or with threads
For more information about MFC in a console application, click the following article numbers to view the articles in the Microsoft Knowledge Base:
KB 152969: How to use the MFC database classes in console applications
150764
(http://support.microsoft.com/kb/150764/
)
CString::LoadString fails in console application
APPLIES TO
| Article Translations
|
Back to the top
