Locking a table record as opposed to a query

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
mskeels
Posts: 16
Joined: Tue 06 Mar 2012 15:08

Locking a table record as opposed to a query

Post by mskeels » Thu 11 Oct 2012 15:56

Can someone please point me to an example of Delphi code that uses locking in concert with an edit?

I found an example for a query.......but still not sure if that applies directly to a table.

Thanks,
Mark

AndreyZ

Re: Locking a table record as opposed to a query

Post by AndreyZ » Fri 12 Oct 2012 07:37

Hello,

To use locking, you should use the LockMode property of a dataset (TMSQuery or TMSTable). If LockMode is set not to the lmNone default value, SDAC performs locking on records editing. Here is a code example:

Code: Select all

procedure TForm1.Button1Click(Sender: TObject);
begin
  MSTable1.TableName := 'tablename';
  MSTable1.LockMode := lmPessimistic;
  MSTable1.Open;
  MSTable1.Edit; // because LockMode is lmPessimistic, here SDAC locks the current record
  MSTable1.FieldByName('fieldname').AsString := 'test';
  MSTable1.Post; // here the current record is unlocked
end;
For more information about the LockMode property, please refer to the SDAC documentation.

mskeels
Posts: 16
Joined: Tue 06 Mar 2012 15:08

Re: Locking a table record as opposed to a query

Post by mskeels » Mon 15 Oct 2012 13:14

Thanks so much for the reply.

A further question: will the following work with the table edit lock:

mstbl.Connection.ExecSQL('SET LOCK_TIMEOUT 10000', []);


Thanks,
Mark

AndreyZ

Re: Locking a table record as opposed to a query

Post by AndreyZ » Mon 15 Oct 2012 14:31

Yes, it will work. Please note that SDAC sets LOCK_TIMEOUT to 2000 by default. You can control this using the TMSConnection.Options.DefaultLockTimeout property.

mskeels
Posts: 16
Joined: Tue 06 Mar 2012 15:08

Re: Locking a table record as opposed to a query

Post by mskeels » Mon 15 Oct 2012 14:39

I appreciate the helpful and timely responses; thanks.

Mark

AndreyZ

Re: Locking a table record as opposed to a query

Post by AndreyZ » Tue 16 Oct 2012 09:04

If any other questions come up, please contact us.

Post Reply