[phpBB Debug] PHP Warning: in file [ROOT]/viewtopic.php on line 1092: Undefined array key 0
[phpBB Debug] PHP Warning: in file [ROOT]/viewtopic.php on line 1092: Trying to access array offset on null
[phpBB Debug] PHP Warning: in file [ROOT]/viewtopic.php on line 1099: Undefined array key 0
[phpBB Debug] PHP Warning: in file [ROOT]/viewtopic.php on line 1099: Trying to access array offset on null
[phpBB Debug] PHP Warning: in file [ROOT]/viewtopic.php on line 1099: Undefined array key 0
[phpBB Debug] PHP Warning: in file [ROOT]/viewtopic.php on line 1099: Trying to access array offset on null
Devart Forums • locking still needs transaction
Page 1 of 1

locking still needs transaction

Posted: Mon 02 Jun 2008 14:19
by jkuiper_them
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?

Posted: Tue 03 Jun 2008 09:07
by Dimon
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.

Posted: Tue 03 Jun 2008 09:31
by jkuiper_them
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.

Posted: Tue 03 Jun 2008 09:45
by Dimon
We will add a demo to demonstrate using the TMyQuery.LockMode property in the next MyDAC build.

Posted: Tue 03 Jun 2008 11:32
by jkuiper_them
Oke, thanks