[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 709
  • Last Modified:

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

0
cplau
Asked:
cplau
  • 3
  • 3
1 Solution
 
cplauAuthor Commented:
Sorry typing mistake in the question...

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

0
 
anoopsCommented:
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...

Anoop

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

Thanks...
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
anoopsCommented:
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.
0
 
V_BapatCommented:
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
0
 
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!!!

0
 
anoopsCommented:
One question at a time please... I guess chaining of questions are not allowed here..
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now