Limitations of DAO, DAO SDK in NT Service or with threads (156138)
The information in this article applies to:
- The Microsoft Foundation Classes (MFC), when used with:
- Microsoft Visual C++, 32-bit Editions 4.0
- Microsoft Visual C++, 32-bit Editions 4.1
- Microsoft Visual C++, 32-bit Enterprise Edition 4.2
- Microsoft Visual C++, 32-bit Professional Edition 4.2
This article was previously published under Q156138 SUMMARY
With the release of MFC Open Database Connectivity (ODBC) in Visual C++
4.2, you can use both MFC and the MFC ODBC classes within the multithreaded
environment of a Windows NT Service. Neither the MFC Data Access Objects
(DAO) classes nor the DAO SDK are thread-safe because the underlying Jet
engine is not thread-safe and cannot be used in a Windows NT Service.
The techniques presented here are valid for any multithreaded environment
with the MFC ODBC classes, not just a Windows NT Service.
The remainder of this article explores the following three areas relating
to using MFC-based database classes within a Windows NT Service:
- How a Windows NT Service works
- How to use MFC ODBC in a multithreaded environment
- Why MFC DAO or the DAO SDK cannot be used in a multithreaded environment
MORE INFORMATIONHow a Windows NT Service Works
A Windows 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 must also be able to operate in the secondary
thread.
How to Use MFC ODBC in a Multithreaded Environment
With 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:
Why MFC DAO or DAO SDK Cannot Be Used in a Multithreaded Environment
DAO version 3.x is a single-threaded in-process 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, the "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 Knowledge Base article listed in the REFERENCES section of this
article for more information.
REFERENCES
For additional information, please see the following articles in the
Microsoft Knowledge Base:
169395
PRB: Thread Safety for DAO/Jet 3.5
System Data Sources136481
INF: Running ODBC Applications as Windows NT Services
Impersonating a Valid Windows NT User96005
Validating User Accounts (Impersonation)
Using MFC Database Classes in a Console Application152696
HOWTO: Using the MFC Database Classes in Console Applications
Why DAO Cannot Be Used in a Multithreaded Environment151407
PRB: DAO Must Be Used in Primary Thread
Using MFC ODBC with an ISAPI DLL160906
HOWTO: Use MFC 4.2 ODBC Classes in an ISAPI DLL
Modification Type: | Major | Last Reviewed: | 6/19/2006 |
---|
Keywords: | kbcode kbDatabase kbinfo kbService KB156138 |
---|
|