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
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
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Moving on to the code
> hMutex = CreateMutex(0, True, MUTEX_NAME)
> If Err.LastDllError = ERROR_ALREADY_EXISTS Then
> lngWaitResult = WaitForSingleObject(hMutex
> 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.