I have ProcessA and ProcessB. Codes are different in these
two processes. Now, these two are trying to update database. So,
I tried synchronize the database operations by using CMutex.
But, it's failing at CSingleLock(&mutex, TRUE) that time
database was not locked by any other app.
What do you mean by "failing"? What is the error code? Also, don't
forget to create named mutex object, so it will be visible
Is is correct approach?
Yes, this is very popular approach to guard common resource
Is there any better solution than this?
Usually database connectiviy itself provides necessary means to
synchronize DB access. For example, if two processes update two
independent tables or just read some table, then there is no
reason to block any of the processes. They can do it concurrently.
You need to lock DB access mostly for updates.
Now, under Windows you will use ADO or OLE DB in order to access
database. OLE DB provides so called cursor service. Cursor service
enables you to set desired policy regarding DB access and
recordset locks. See more here:
"Microsoft Cursor Service for OLE DB (ADO Service Component)"