Dao and multithread???

Dear All,

I am using VC++5.0 to develop my project...
In searching the help at VC, I found this statement...

"DAO itself is not multithreaded, so you can't use the MFC DAO classes in multiple threads. Confine your DAO code to a single thread of execution."

What is meaning of this statement..I have an question about this sentence.

If I have built two worker threads in my application, can I open two different databases in these two different worker threads and perform some DAO operations???

If  no, what is the problem about this? Can I overcome this?

Furthermore, I have another question is :

in my application, in the main thread .....using the dao to open a database. Then I build one more worker thread...in this thread, using the CDaoOdbc class to open another database....can this be implemented? Any problem exist?

Thanks a lot for your opinion

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cplauAuthor Commented:
Sorry typing mistake in the question...

I mean use the CDatabase and CRecordSet classes..not the CDaoOdbc....this class is not existed.

If you are using CDatabase & CRecordset then you are not using DAO. That's plain and simple!. If u r using these classes then u r using ODBC for database connectivity.
Now coming to the main part of your questio..

"DAO itself is not multithreaded"..means these classes do not have any synchronization mechanisms built in( like Critical sections or mutexes)
 Now that means you can't have an object which might be..just might be accessed by more than one thread at a time.
For eg:-
    Suppose u have two threads ThreadX and ThreadY and you have a global Recordset object, RecZ, which holds some numbers.
 Say ThreadX is used to Update this table while ThreadY reads values from this table and displays it. Assume that U r using the same RecZ recordset object for both of these operations.
 Now it's possible that both of these thread can access RecZ at the same time and that would cause access violations.
 To avoid these things generally people would make their classes fully multithreaded. Now that means in each function which access the same data they would have some mechanism that would allow only one thread to pass through. All other threads would have to wait till the guy who's gone through completes his job.( see description for synchronization classes in MSDN )
  Now DAO classes don't have these kinda mechanisms built in( the guys who developed them didn't care about multithreading -:)..Juz kidding!) and if anybody is using them in a multithreaded program then it's the developers job to do the synchronization ..

Wooops.. boy!..I never thought It would be this long..'Nywayz I hope this clears u'r doubt...



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cplauAuthor Commented:
Dear anoops,

Do you mean that if I have two different recordset objects in two differernt threads, no problem will be existed if I use these two different recordset objects to perform different DAO operations on two different databases???

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Oh yea.. Absolutely no problems, because they are different objects.
 But if u r using 2 different Recordset objects to access the same table in the database U might have think about synchronization.
anoop is correct. If you are using recordest objects in different threads to access same data in the database, you need to provide syncronization. You can use mutex for this purpose.

First created a named mutex. Whenever a thread want to access the database, it should accuire the ownership of the mutex. After completing the job it should release the ownership to the mutex.

If during this time another thread tries to access database, it has to wait till the other thread accessing the database releases the ownership to the mutex.

cplauAuthor Commented:
Dear All,

I have used the AfxDaoInit() and AfxDaoTerm() in my worker thread to initialize the DAO Jet 3.5 engine.....

But I found if I perfrom some DAO operations in the main thread, and then closing the thread at the same time...
An error will be occured:

Unhandled exception in My.exe(DAO350.dll): 0xC0000005:
Access Violation.

What is the problem .....how can I use the funciton AfxDaoTerm() in closing the thread???

Thanks for your opinion!!!

One question at a time please... I guess chaining of questions are not allowed here..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.