Row-level Locking
-
Guest
Row-level Locking
Hi,
I am trying to use row-level locking. We tried with TCustomMyDataSet.Lock, but won't work for us. If you have any locking related example then please send me. We are using InnoDB as a table type for the same.
regards,
Sanjivan
I am trying to use row-level locking. We tried with TCustomMyDataSet.Lock, but won't work for us. If you have any locking related example then please send me. We are using InnoDB as a table type for the same.
regards,
Sanjivan
Go to the first record of MyTable1. Execute the following code in Application1.
MyConnection1.StartTransaction;
MyTable1.Lock(lrImmediately);
After that, if you execute the same code in Application2 then MySQL will be
waiting for committing a transaction in Application1.
If you try to edit this record in Application2 then MySQL will be waiting for
committing a transaction in Application1.
lrImmediately parameter value corresponds to SELECT ... FOR UPDATE statement
lrDelayed parameter value corresponds to SELECT ... LOCK IN SHARE MODE
statement
Please see about FOR UPDATE and LOCK IN SHARE MODE in MySQL documentation.
MyConnection1.StartTransaction;
MyTable1.Lock(lrImmediately);
After that, if you execute the same code in Application2 then MySQL will be
waiting for committing a transaction in Application1.
If you try to edit this record in Application2 then MySQL will be waiting for
committing a transaction in Application1.
lrImmediately parameter value corresponds to SELECT ... FOR UPDATE statement
lrDelayed parameter value corresponds to SELECT ... LOCK IN SHARE MODE
statement
Please see about FOR UPDATE and LOCK IN SHARE MODE in MySQL documentation.
-
Guest
Stiil, i am not able to do this? Here is the code.
procedure TForm1.Button1Click(Sender: TObject);
var
custom : TCustomMyDataSet;
connection : TCustomDAConnection;
value : string;
begin
connection := TCustomDAConnection.Create(nil);
custom := TCustomMyDataSet.Create(nil);
if MyConnection1.Connected then
Begin
ShowMessage('connected');
Try
MyConnection1.StartTransaction;
MyQuery1.FetchAll := false;
custom.Lock(lrImmediately);
custom.SQL.Add('insert into xx (xx1, xx2) Values(''12'', ''testStore''');
custom.Execute;
// value := custom.FieldValues['StoreName'];
ShowMessage( 'I got value');
MyConnection1.Commit;
except
MyConnection1.Rollback;
end;
end
else
ShowMessage('NO RESULT');
end;
end.
procedure TForm1.Button1Click(Sender: TObject);
var
custom : TCustomMyDataSet;
connection : TCustomDAConnection;
value : string;
begin
connection := TCustomDAConnection.Create(nil);
custom := TCustomMyDataSet.Create(nil);
if MyConnection1.Connected then
Begin
ShowMessage('connected');
Try
MyConnection1.StartTransaction;
MyQuery1.FetchAll := false;
custom.Lock(lrImmediately);
custom.SQL.Add('insert into xx (xx1, xx2) Values(''12'', ''testStore''');
custom.Execute;
// value := custom.FieldValues['StoreName'];
ShowMessage( 'I got value');
MyConnection1.Commit;
except
MyConnection1.Rollback;
end;
end
else
ShowMessage('NO RESULT');
end;
end.
-
Guest
No, we don't have samples for MyDAC with locking.
The most primitive schema of using row-level locking:
The most primitive schema of using row-level locking:
- Create InnoDB table at the server
- Create at your project TMyTable(TMyQuery), and associate it with the table
- Create event handlers:
Code: Select all
procedure TForm3.MyTable1BeforeEdit(DataSet: TDataSet);
begin
MyTable1.Connection.StartTransaction;
MyTable1.Lock(lrImmediately);
end;
procedure TForm3.MyTable1AfterPost(DataSet: TDataSet);
begin
MyTable1.Connection.Commit;
end;
procedure TForm3.MyTable1AfterCancel(DataSet: TDataSet);
begin
MyTable1.Connection.Rollback;
end;-
Guest
Row level lockign
Fine, I am having success on locing. But i have small problem. I want to set autocommit = false;
How it can be done. As i am using myquery , i want to keep autocommit =false, on that .
Please guide me on the same.
Thanks in advance
How it can be done. As i am using myquery , i want to keep autocommit =false, on that .
Please guide me on the same.
Thanks in advance