tsgiannis

Optimistic Record Locking

Sep 3rd, 2025
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | Source Code | 0 0
  1. Sub UpdateWithValueBasedOptimisticLocking()
  2. Dim conn As ADODB.Connection
  3. Dim cmd As ADODB.Command
  4. Dim affectedRows As Long
  5. Dim retryCount As Integer
  6.  
  7. ' Original values (would come from your form/data)
  8. Dim originalSalary As Currency: originalSalary = 50000
  9. Dim originalLastName As String: originalLastName = "Smith"
  10. Dim employeeID As Long: employeeID = 123
  11.  
  12. retryCount = 0
  13.  
  14. Do While retryCount < 3
  15. On Error GoTo ErrorHandler
  16.  
  17. Set conn = New ADODB.Connection
  18. conn.ConnectionString = "Provider=SQLOLEDB;Data Source=YourServer;Initial Catalog=YourDatabase;User ID=YourUser;Password=YourPassword;"
  19. conn.Open
  20. conn.BeginTrans
  21.  
  22. ' SQL with value-based optimistic locking
  23. Dim strSQL As String
  24. strSQL = "UPDATE Employees SET " & _
  25. "Salary = u/NewSalary, " & _
  26. "LastName = u/NewLastName, " & _
  27. "ModifiedDate = GETDATE() " & _
  28. "WHERE EmployeeID = u/EmployeeID " & _
  29. "AND Salary = u/OriginalSalary " & _ ' Optimistic lock check
  30. "AND LastName = u/OriginalLastName"
  31.  
  32. Set cmd = New ADODB.Command
  33. With cmd
  34. .ActiveConnection = conn
  35. .CommandType = adCmdText
  36. .CommandText = strSQL
  37.  
  38. ' Parameters
  39. .Parameters.Append .CreateParameter("@NewSalary", adCurrency, adParamInput, , 55000)
  40. .Parameters.Append .CreateParameter("@NewLastName", adVarChar, adParamInput, 50, "UpdatedName")
  41. .Parameters.Append .CreateParameter("@EmployeeID", adInteger, adParamInput, , employeeID)
  42. .Parameters.Append .CreateParameter("@OriginalSalary", adCurrency, adParamInput, , originalSalary)
  43. .Parameters.Append .CreateParameter("@OriginalLastName", adVarChar, adParamInput, 50, originalLastName)
  44. End With
  45.  
  46. cmd.Execute affectedRows
  47.  
  48. If affectedRows > 0 Then
  49. conn.CommitTrans
  50. MsgBox "Update successful!"
  51. Exit Do
  52. Else
  53. conn.RollbackTrans
  54. retryCount = retryCount + 1
  55.  
  56. If retryCount < 3 Then
  57. ' Refresh original values for next attempt
  58. If GetCurrentValues(employeeID, originalSalary, originalLastName) Then
  59. MsgBox "Record was modified. Retrying with current values..."
  60. CleanupObjects cmd, conn
  61. Application.Wait Now + TimeValue("00:00:01")
  62. Else
  63. MsgBox "Record not found or unable to read current values!"
  64. Exit Do
  65. End If
  66. Else
  67. MsgBox "Update failed after 3 attempts due to conflicts."
  68. End If
  69. End If
  70.  
  71. GoTo Cleanup
  72.  
  73. ErrorHandler:
  74. If Not conn Is Nothing And conn.State = adStateOpen Then conn.RollbackTrans
  75. MsgBox "Error: " & Err.Description
  76. Exit Do
  77.  
  78. Cleanup:
  79. CleanupObjects cmd, conn
  80. Loop
  81. End Sub
  82.  
  83. Function GetCurrentValues(employeeID As Long, ByRef salary As Currency, ByRef lastName As String) As Boolean
  84. Dim conn As ADODB.Connection
  85. Dim rs As ADODB.Recordset
  86.  
  87. On Error GoTo ErrorHandler
  88.  
  89. Set conn = New ADODB.Connection
  90. conn.ConnectionString = "Provider=SQLOLEDB;Data Source=YourServer;Initial Catalog=YourDatabase;User ID=YourUser;Password=YourPassword;"
  91. conn.Open
  92.  
  93. Set rs = conn.Execute("SELECT Salary, LastName FROM Employees WHERE EmployeeID = " & employeeID)
  94.  
  95. If Not rs.EOF Then
  96. salary = rs.Fields("Salary").Value
  97. lastName = rs.Fields("LastName").Value
  98. GetCurrentValues = True
  99. Else
  100. GetCurrentValues = False
  101. End If
  102.  
  103. rs.Close
  104. conn.Close
  105. Exit Function
  106.  
  107. ErrorHandler:
  108. GetCurrentValues = False
  109. If Not rs Is Nothing Then If rs.State = adStateOpen Then rs.Close
  110. If Not conn Is Nothing Then If conn.State = adStateOpen Then conn.Close
  111. End Function
Tags: vba ado
Advertisement
Add Comment
Please, Sign In to add comment