Link to home
Start Free TrialLog in
Avatar of cplau
cplau

asked on

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

Avatar of cplau
cplau

ASKER

Sorry typing mistake in the question...

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

ASKER CERTIFIED SOLUTION
Avatar of anoops
anoops

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cplau

ASKER

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???

Thanks...
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.

Vicky
Avatar of cplau

ASKER

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..