locking still needs transaction

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply

record locking is very usefull or outdated

Poll ended at Wed 02 Jul 2008 14:19

 
Total votes: 0

jkuiper_them
Posts: 28
Joined: Thu 20 Dec 2007 14:48

locking still needs transaction

Post by jkuiper_them » Mon 02 Jun 2008 14:19

In MyDAC 5.2x I used this code to lock a record.

Code: Select all

procedure TForm1.MyQuery1AfterPost(DataSet: TDataSet);
var MyQuery : TMyQuery;
begin
  MyQuery := (DataSet as TMyQuery);
  if MyQuery.Connection.InTransaction then
  begin
    MyQuery.Connection.Commit;
  end;
end;

procedure TForm1.MyQuery1BeforeEdit(DataSet: TDataSet);
var MyQuery : TMyQuery;
begin
  MyQuery := (DataSet as TMyQuery);
  if not MyQuery.Connection.InTransaction then
    MyQuery.Connection.StartTransaction;
  MyQuery.Lock(lrImmediately);
end;
This fortunally works also in version 5.5x

Bu now there is a new property lockmode witch has these options ( lmOptimistic, lmPessimistic and lmNone). If you use the first two, you have to put an event BeforeEdit to call the locking.

Code: Select all

procedure TForm1.MyQuery1BeforeEdit(DataSet: TDataSet);
begin
  MyQuery1.Lock;
end;
But you miss something. MyQuery1.Lock calls MyQuery1.Connection.StartTransaction. So if the first user changed the record, there is no way to save the same record witch is connected to another user. The only to save the changes is closing the form witch the first user connected to the table.

To solve the problem, you still have to need this code:

Code: Select all

procedure TForm1.MyQuery1AfterPost(DataSet: TDataSet);
var MyQuery : TMyQuery;
begin
  MyQuery := (DataSet as TMyQuery);
  if MyQuery.Connection.InTransaction then
  begin
    MyQuery.Connection.Commit;
  end;
end;
Why can't I find it in the manuals or in the demo?

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Tue 03 Jun 2008 09:07

If you set the TMyQuery.LockMode property to lmPessimistic or to lmOptimistic, you should not call the TMyQuery.Lock method in the BeforeEdit event and the Commit method in the AfterPost event.

If LockMode is set to lmPessimistic, TMyQuery performs locking when user starts editing a record. When user posts the changes, TMyQuery performs Commit. In this case another user can not change the record until the first user posts or cancels editing.

If LockMode is set to lmOptimistic, TMyQuery performs locking when user posts the edited record. So, if another user has changed the record, TMyQuery raises an exception and you won't be able to post changes.

jkuiper_them
Posts: 28
Joined: Thu 20 Dec 2007 14:48

Post by jkuiper_them » Tue 03 Jun 2008 09:31

This I found out myself :!:
But the demo is not giving this example and should be updated to the latest version. The manual gives no example at all how to work with these components.

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Tue 03 Jun 2008 09:45

We will add a demo to demonstrate using the TMyQuery.LockMode property in the next MyDAC build.

jkuiper_them
Posts: 28
Joined: Thu 20 Dec 2007 14:48

Post by jkuiper_them » Tue 03 Jun 2008 11:32

Oke, thanks

Post Reply