locking still needs transaction
Posted: Mon 02 Jun 2008 14:19
In MyDAC 5.2x I used this code to lock a record.
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.
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:
Why can't I find it in the manuals or in the demo?
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;
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;
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;