Home > Back-end >  Delphi SQL Server deadlocks
Delphi SQL Server deadlocks

Time:10-07

Following a simple example, can cause a deadlock, you can try, what is the reason?

Db: TDatabase;
Qry, qryTemp: TQuery;

Procedure the ExecuteSQL (strSQL: string);
The begin
QryTemp. SQL. Text:=strSQL;
QryTemp. ExecSQL;
end;

Procedure ButtonClick (Sender: TObject);
The begin
Db. StartTransaction;
The ExecuteSQL (' Insert Into Table1 (Name) Values (' a ') ');
Qry. SQL. Text:='Select * From Table1 (NoLock) Where Name=' 'a' ' '.
Qry. Active:=True;
The ExecuteSQL (' Insert Into Table2 (Name) Values (' ' ' 'b)');
Qry. Next;//if you remove it, will not deadlock
The ExecuteSQL (' Update Table2 Set Name='c' 'Where Name=' 'b' ' ');//add qry. Next, here will be a deadlock
The db. The Rollback;
end;

1, don't entanglements this code to do so boring things, just to test a deadlock;
2, Table1 and Table2 table structure is:
Numeric ID primary key ID
The Name varchar (50)
3, why add qry. Next will deadlock, don't add all right? I use Delphi6 + SQL Server2000 and Delphi XE + SQL Server2008 are tested, there is also the problem, should not be caused by bugs of Delphi,

CodePudding user response:

Test and use ADO nothing, seems to be the problem caused by the BDE,

CodePudding user response:

This code is more than one instance you performed at the same time? Not a single transaction may lead to a deadlock, if there are multiple execution at the same time, so see you BDE locking levels or related parameters, such as transaction isolation level, see if use pessimistic locks,

CodePudding user response:

Suggest you use ADO to

CodePudding user response:

Agree with upstairs ~ ADO
  • Related