DAO multi-threading tips


Download sample project

Looks like DAO 3.5 has some support for multi-threading.

As stated by the MFC 4.2 documentation, the Dao database classes are not thread-safe, and that is indeed true.

I recently developed a server application the uses IO completion ports, which by nature, are using a number of threads. Because of that, I had to figure out a way to use DAO in different threads simultaneously. Here is how I did it:
// used a global critical section to prevent multiple threads initializing 
// the Dao database simultaneous.
CCriticalSection cs;

// Some thread function
// (In my server app, 4 thread using this function is started)
UINT MyThreadFunc (LPVOID pParam)
	CSingleLock lock(&cs, TRUE);

	// Initialize MFC Dao support

	CDaoDatabase db;
	CSomeDaoRecordset set(&db);

	// Open the database and recordset.
	catch(CException* pe)	
		return 0;

	// Allow other threads to initialize Dao

	// OK the database and recordset is now open

	while( bRunThisThread )
		// NOTE: You do NOT need to lock out other threads vhile calling
		// database functions here.

	// Now, lock the critical section again

	// Close recordset and database.

	// IMPORTANT: do NOT call AfxDaoTerm() !!!!
	return 0;

Now, when your application terminates, it will generate a protection error. To prevent that modify your CMyApp::ExitInstance().

int CMyApp::ExitInstance()
	// Insert your own cleanup code here

	return m_msgCur.wParam;

7 The MFC documentation says the you need to call AfxBeginThread to start worker threads, but this is not necessary at all times, it depends on what your doing in your thread function. (I use both AfxBeginThread() and ::CreateThread()).
7 I call AfxBeginThread() in my main InitInstance() function, but I am not really sure if this is necessary. Of course, you should do it if you need to access the database from your main thread.
7 You will not gain true multithreaded access to the database functions. Jet 3.5 serializes the function calls, but the threads do not have to worry about that.

Known problems:
1. I have discovered that this approach can sometimes prevent other applications from starting, like MS Word 97, they simply hang, but when i shutdown my multithreaded Dao app, they continues as though nothing has happened ?????
2. My spelling :-)
3. ::CoInitialize() will be called for every call to AfxDaoInit(), and will not all be matched by a call to :: ::CoUninitialize(), but i hope the Windows will clean up the mess.


  • No Different Threads---

    Posted by Legacy on 08/01/2003 12:00am

    Originally posted by: Siddharth

    I put this solution in my project and its seem to work for first thread and if i invoke another thread containing AfxDaoInit from the first thread its giving runtime error
    Abnormal Programme Termination but its running perfectly alright on Win2000 server but not on winME

  • Danke

    Posted by Legacy on 02/18/2003 12:00am

    Originally posted by: jan

    thank you for this elegant and usful piece of code.

  • provide good online coaching

    Posted by Legacy on 12/24/2002 12:00am

    Originally posted by: phani bhushan reddy

    respected sir,
    here iam happy to get this option , to share my views/problems with you. sir i found one problem while i was using brain bench site.i didnot know about com and dcom.
    i want to know about that if any chances are there for it
    please specify through mail .
    if my request is not glad to receive please excuse me.
    thanking you,
    i hope the best

  • TH

    Posted by Legacy on 08/09/2002 12:00am

    Originally posted by: Gabor M�lyi

    Thank You!

    Your's programs great, and able piece of my work.


  • Thank you

    Posted by Legacy on 02/25/2002 12:00am

    Originally posted by: Chetan

    Your article saved me from lots of Multi threading issues I encountered due to DAO.

    Thanks again.

  • Muchos Gracias!

    Posted by Legacy on 12/28/1999 12:00am

    Originally posted by: Zenon

    After racking my brain for hours on end, I finally consulted CodeGuru, and voil�, the answer was there waiting for me.

    Many thanks to John Bundgaard who just made my life a whole lot easier.

  • Restart IDE

    Posted by Legacy on 05/17/1999 12:00am

    Originally posted by: James Johnston

    Have you restarted the Visual Studio IDE?

  • Unlock the lock?

    Posted by Legacy on 01/15/1999 12:00am

    Originally posted by: Morten Tor Nielsen

    When the thread is done with the database the database objects are closed.

    That piece of code is executed in a monitor (the entrance is the lock.Lock() statement) but don't you need a lock.Unlock() statement after the closing of the DAO objects (or does the CSingleLock unlock when you exit scope??)

    Otherwise thanks for the tip - VERY Very helpfull


  • Mutli Threaded DAO

    Posted by Legacy on 01/13/1999 12:00am

    Originally posted by: Jeff Miller

    A client had a problem with the DAO code crashing when used in multiple threads. There were several million lines of code. To touch the minimal amount of code, I came up with a way to force all of the DAO calls to go through the GUI thread. If anyone is interested in this approach, I can post the code.

  • Stop other programs starting until Database program terminated

    Posted by Legacy on 11/18/1998 12:00am

    Originally posted by: Steve Westgate

    I had a similar problem of other programs not starting until the databse program was terminated and they then
    continued to startup.

    Looking at the help on DAO you need to add a message dispatcher routine in your threads loop.

    For example

    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))

  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date