nice_guy_in_stj
10-31-2004, 08:59 AM
Hey guys, I'd really appreciate any help on this one: I have a shared database and one of the tables contains info that when someone is using, nobodoy else should be allowed to access the same info. Therefore, each record in the table has a lock on it, and only the user who has access to the lock can edit the info. I'm trying to deal with concurrency amongst other users all in the same office. I thought the code below would be the best solution. I check if the lock is available, if it is, then I may be checking it at the same time as another user, so I wait a random number of milliseconds, then check it again, if it's still available, then I set the lock so that nobody else can edit that record.
The problem : when I test this by clicking edit at the same time on 2 different computers, both end up getting the same random number of milliseconds to wait, then they both check the lock at the same time AGAIN, so both try to update the record's lock at the same time, thus crashing the whole function. How can I create a random number that will be different when done on 2 different machines, but at the same time ?
checkLock.Open "SELECT lock FROM [A] WHERE item_num=" & num, con, 3, 3
If checkLock.Fields("lock") = 1 Then ' record is available
checkLock.Close
waitForTimer = True
Randomize CLng(date) + Timer 'random number generator
tmrWait.interval = CInt(Rnd * 1000)
tmrWait.Enabled = True
While waitForTimer
DoEvents
Wend
checkLock.Open "SELECT lock FROM [A] WHERE item_num=" & num, con, 3, 3
If checkLock.Fields("lock") = 1 Then 'first one to get lock
checkLock.update "lock", 0
checkLock.Close
Else
alreadyLocked = True
End If
Else
alreadyLocked = True
End If
If alreadyLocked Then
MsgBox "This entry is currently being edited by another user, try again later", vbOKOnly, "Currently Accessed"
Exit Sub
End if
'Timer has gone off.
Private Sub tmrWait_Timer()
tmrWait.Enabled = False
waitForTimer = False
End Sub
The problem : when I test this by clicking edit at the same time on 2 different computers, both end up getting the same random number of milliseconds to wait, then they both check the lock at the same time AGAIN, so both try to update the record's lock at the same time, thus crashing the whole function. How can I create a random number that will be different when done on 2 different machines, but at the same time ?
checkLock.Open "SELECT lock FROM [A] WHERE item_num=" & num, con, 3, 3
If checkLock.Fields("lock") = 1 Then ' record is available
checkLock.Close
waitForTimer = True
Randomize CLng(date) + Timer 'random number generator
tmrWait.interval = CInt(Rnd * 1000)
tmrWait.Enabled = True
While waitForTimer
DoEvents
Wend
checkLock.Open "SELECT lock FROM [A] WHERE item_num=" & num, con, 3, 3
If checkLock.Fields("lock") = 1 Then 'first one to get lock
checkLock.update "lock", 0
checkLock.Close
Else
alreadyLocked = True
End If
Else
alreadyLocked = True
End If
If alreadyLocked Then
MsgBox "This entry is currently being edited by another user, try again later", vbOKOnly, "Currently Accessed"
Exit Sub
End if
'Timer has gone off.
Private Sub tmrWait_Timer()
tmrWait.Enabled = False
waitForTimer = False
End Sub