Link to home
Start Free TrialLog in
Avatar of mvoronkin
mvoronkin

asked on

Mutex - record locking mechanism

I have vb6 app that communicates with MSAccess2003 database. The problem I'm having is the record locking. I use parameterized store procedures to insert records to a database. I have 3 users constantly entering data. Every day they ran into the locking problem.
I was advised to use mutex to manage exclusive access to the database while inserting records.
My question is what is the proper name for a mutex object located on the server? I need all my clients to be able to access it.
Will the following code work:
Public Function SaveData(ByRef MyData As MyType) As Boolean  
    Dim hMutex As Long
    Dim lngWaitResult As Double
    Dim blnReturn As Boolean
    MUTEX_NAME = "\\SERVER_NAME\Mutex_Name"
    hMutex = CreateMutex(0, True, MUTEX_NAME)
    If Err.LastDllError = ERROR_ALREADY_EXISTS Then        
        lngWaitResult = WaitForSingleObject(hMutex, INFINITE)
    End If    
    Select Case lngWaitResult
        Case WAIT_OBJECT_0
          '  Write to the database.
            blnReturn = WriteToDB(MyData)
            Dim lngReleasmutex As Long
            lngReleasmutex = ReleaseMutex(hMutex)
            If lngReleasmutex = 0 Then
                 '   Deal with error.
            Else
                CloseHandle hMutex                
            End If          
       Case WAIT_TIMEOUT
            blnReturn = False
       Case WAIT_ABANDONED
            blnReturn = False
        Case WAIT_FAILED
            blnReturn = False
    End Select
    SaveData = blnReturn    
   Exit Function

Another question is what is the proper way to handle the ReleaseMutex error?

Thanks in advance

Milena
Avatar of ravs120499
ravs120499

The Mutex name should be something that is (a) easy to understand who/which app creates it, and (b) difficult for two different apps to use the same name by coincidence. For example, \\SERVER\MYAPPNAME_DBMutex. If MYAPPNAME is not distinctive enough, you need to figure out what combination of fields would be distinctive on that server environment.

Moving on to the code
> hMutex = CreateMutex(0, True, MUTEX_NAME)
>    If Err.LastDllError = ERROR_ALREADY_EXISTS Then        
>        lngWaitResult = WaitForSingleObject(hMutex, INFINITE)
>    End If    

The first time you execute this, you would expect it to return success. The above code will not do a WaitForSingleObject in that case.

Second, I suggest putting the mutex management into separate functions and not embed them in the main body of the code.
Avatar of mvoronkin

ASKER

Hi,
Thanks for a reply.
My problem is that I run this code on the client machine and I can't create a mutex on the server using following name for a mutex \\SERVERNAME\MUTEXNAME. The return value is 0.
 Is it possible to create a mutex on server from code running on the client machine?


Thanks again

ASKER CERTIFIED SOLUTION
Avatar of ravs120499
ravs120499

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