Limitations of DAO, DAO SDK in NT Service or with ThreadsLast reviewed: February 12, 1998Article ID: Q156138 |
The information in this article applies to:
SUMMARYWith the release of MFC ODBC in Visual C++ 4.2, you can use both MFC and the MFC ODBC classes within the multithreaded environment of an NT Service. Neither the MFC DAO classes nor DAO SDK are thread-safe because the underlying Jet engine is not thread-safe and cannot be used in an NT Service. The techniques presented here are valid for any multithreaded environment with the MFC ODBC classes, not just an NT Service. The remainder of this article explores the following three areas relating to use of MFC-based Database classes within an NT Service:
MORE INFORMATION
How an NT Service WorksAn NT Service consists of code that is roughly similar to the following:
VOID WINAPI Main( VOID ) { ... if (!StartServiceCtrlDispatcher(...)) AddToMessageLog(TEXT("StartServiceCtrlDispatcher failed.")); ... }When a service is started, the Service Control Manager waits for the primary thread of the service to invoke a call to StartServiceCtrlDispatcher(). This call triggers the creation of a named pipe whose purpose is to provide a communication channel between the service and the Service Control Manager. The primary thread acts as a control dispatcher for the service. The service receives control requests from the Service Control Manager via the control handler (callback function). In addition, the StartServiceCtrlDispatcher() invokes a secondary thread that contains the entry point of the service as specified by the SERVICE_TABLE_ENTRY structure. If the StartServceCtrlDispatcher() call succeeds, the StartServiceCtrlDispatcher() call in the primary thread does not return until all running services in the process have terminated. Any attempt to use database classes within a running service must not only be thread-safe, but they must also be able to operate in the secondary thread.
How to Use MFC ODBC in a Multithreaded EnvironmentWith the release of Visual C++ 4.2, both MFC and the MFC ODBC database code is safe to use in a multithreaded environment. However, you should keep the following points in mind:
DAO version 3.x is a single-threaded inproc server and was originally written for Access 1.x and 2.0 and Visual Basic 3.0--both of which are single-threaded. This means that clients (MFC DAO or DAO SDK) can use DAO via custom interfaces only from the first thread in the process that initialized COM—that is, 'primary' thread. If the client has already initialized COM in the primary thread and then calls CoCreateInstance in the secondary thread asking for the IDAODBEngine interface, CoCreateInstance fails. This fails because COM sees that DAO is not marked as apartment or free threaded in the registry, calls DllGetClassObject from the primary thread, gets the IDAODBEngine interface, tries to marshal it back to the secondary thread, and fails. See the KB article listed in the REFERENCES section below for details.
REFERENCESFor additional information, please see the following articles in the Microsoft Knowledge Base:
ARTICLE-ID: Q169395 TITLE : PRB: Thread Safety for DAO/Jet 3.5 System Data Sources
ARTICLE-ID: Q136481 TITLE : INF: Running ODBC Applications as Windows NT Services Impersonating a Valid NT User
ARTICLE-ID: Q96005 TITLE : Validating User Accounts (Impersonation) Using MFC Database Classes in a Console Application
ARTICLE-ID: Q152696 TITLE : HOWTO: Using the MFC Database Classes in Console ApplicationsWhy DAO Can't Be Used in a Multithreaded Environment
ARTICLE-ID: Q151407 TITLE : PRB: DAO Must Be Used in Primary Thread Using MFC ODBC with an ISAPI DLL
ARTICLE-ID: Q160906 TITLE : HOWTO: Use MFC 4.2 ODBC Classes in an ISAPI DLL Password Keywords : MfcDAO MfcDatabase kbcode Technology : kbMfc Version : Winnt:4.0,4.1,4.2 Platform : winnt Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |