We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Record Locking in MS SQL Server 2000 using VB app.

US-IT
US-IT asked
on
Medium Priority
226 Views
Last Modified: 2013-12-25
I converted the access database into ms sql server and I am using VB 6.0 app for front end interface. The problem is that I lost the record locking feature. The vb app does not recognize if the record is locked or not. I need to know the best way to have record locking for concurrent processes. Is there any stored procedure available that I can directly call or some other better way? Please let me know. Thanks!
Here is the code I am using for checking if the record is lock or not:

'Test method to determine whether a record is locked or not before editing it
Public Function IsItLocked(rstAny As ADODB.Recordset) As Boolean
    On Error GoTo IsItLocked_Error
    IsItLocked = False
   
    rstAny.Fields(0).Value = rstAny.Fields(0).Value
    Exit Function
   
IsItLocked_Error:
   
    If Err.Number = -2147467259 Then
        IsItLocked = True
        Exit Function
    Else
        MsgBox "Error #" & Err.Number & vbCrLf & Err.Description, vbCritical, "Unknown Error Found!"
    End If
End Function

Public Sub LockRecord(rstAny As ADODB.Recordset)
    ADODBConn.BeginTrans
    gblnInTrans = True
    rstAny.Fields(0).Value = rstAny.Fields(0).Value 'record should lock occur here
   
End Sub
Comment
Watch Question

leclairm.Net Developer  ERP integration

Commented:
What lock type is your recordset using??

Author

Commented:
It is  'adLockPessimistic' and the cursor type is 'adOpenKeyset'.

thanks!
leclairm.Net Developer  ERP integration

Commented:
I don't have books online on the computer I'm at now, but I know there is a locking hints section that explains how to lock records directly in your query.

Here's a head start:
http://www.mssqlcity.com/Articles/Adm/SQL70Locks.htm

Author

Commented:
It's a great site, but I need a quicker solution. Do you have a working code in VB to invoke a lock and check if it is locked?
leclairm.Net Developer  ERP integration

Commented:
You can use the UPDLOCK locking with your select which will ensure nothing changes during your transaction.  I would try using begin trans / commit trans with a boolean variable that is set to true during your transaction that has a lock on the row/table and it's set to false after the commit trans.

I've never done this before though, so I'm not sure of any other methods of doing this.

More info...

http://www.informit.com/articles/article.asp?p=27020&rl=1

Author

Commented:
How would I determine whether the record is locked or not?
leclairm.Net Developer  ERP integration

Commented:
You would have to pass the lock mode in your select statement, so I would imagine your lockrecord function should work.  Not so sure about the IsItLocked function though.

Sorry I couldn't be much more help...

Author

Commented:
I tried and it did not work. thanks anyway. Is anyone else can be able to help in this problem?
I don't think you can read if a SQL table has a lock applied.  If your application owns the database and/or table, you can set up another table to keep track of your locks so that your program doesn't attempt to do something you don't want it to do.  E.g., if user "A" is updating Table "T", don't let user "B" update table "T"

Does that help?

Author

Commented:
Well, I am able read a record while another client is updating it. I applied this trick before, but did not find it efficient.  This adds more complexity and problems. I want to keep it simple. Any other suggestions?

Author

Commented:
Also, I am able to update it while someone else is editing it.
CERTIFIED EXPERT
Top Expert 2012

Commented:
If you are going to resort to editing in a recordset instead of using INSERT/UPDATE/DELETE statements than you are going to have to add a flag in the record to indicate whether the rows is being edited or not.  You will also have to allow for the user that "went to lunch" and left the row "locked"

Author

Commented:
well, how would the client knows if the record is locked. Is there any stored procedure that can provide this information?
CERTIFIED EXPERT
Top Expert 2012
Commented:
The simple answer is you do not.  You can only assume that if the row is "locked" for more than x number of minutes than it is time to "unlock" it.  This can be done manually or a job that runs every x minutes to clear all old locks.

I am afraid to say, that is the best you can do with recordset editing.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2012

Commented:
I believe I answered the question.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.