Record-/table-locking

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
formi
Posts: 39
Joined: Thu 17 Apr 2008 13:01

Record-/table-locking

Post by formi » Tue 01 May 2012 14:26

SQL Server 2008, UniDac-components.
I have to deliver a unique value (as a document-number with a specific format). So my idea is to lock a record (or the whole table), to read then last value, to modify it and to unlock the record / table. I tried it with
try
Connection.StartTransaction
Table.Open
Table.Lock
do by job
Table.Unlock
Table.Close
except
Showmessage('Sorry, table is locked ...');
end;

To test it I made two buttons, two tables and the code above in the OnClick-Event for each table. But there is never an excption fired. How to do it? Thanks for your help, Peter

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Record-/table-locking

Post by AlexP » Wed 02 May 2012 10:47

Hello,

This error message occurs only in case of an attempt to perform simultaneous access to a locked table.
This error message occurs only in case you do not complete an already started transaction, before an attempt to access the table.

formi
Posts: 39
Joined: Thu 17 Apr 2008 13:01

Re: Record-/table-locking

Post by formi » Wed 02 May 2012 11:21

And how can I solve my task? Exclusive acces on a record or on a whole table?
Thanks Peter

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Record-/table-locking

Post by AlexP » Wed 02 May 2012 13:03

hello,

If you don't need to change all records in the table, it is irrational to lock the whole table, it is sufficient to lock only the record that requires changing.

formi
Posts: 39
Joined: Thu 17 Apr 2008 13:01

Re: Record-/table-locking

Post by formi » Wed 02 May 2012 13:23

Thanks, but I don't know how to do it. Please give an example, Peter

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Record-/table-locking

Post by AlexP » Wed 02 May 2012 14:09

hello,

To lock the record you are editing, set the LockMode property to lmPessimistic, in this case calling the Edit method will call the following command:

Code: Select all

SELECT * FROM Table
WITH (UPDLOCK, ROWLOCK, HOLDLOCK)
WHERE
  id = ?
:Old_id(Integer,IN)=N
which will lock the current record

formi
Posts: 39
Joined: Thu 17 Apr 2008 13:01

Re: Record-/table-locking

Post by formi » Wed 02 May 2012 15:02

Thanks for your help, that works for me, Peter

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Re: Record-/table-locking

Post by AlexP » Thu 03 May 2012 06:54

Hello,

Glad to see that the problem was solved. If you have any other questions, feel free to contact us.

Post Reply