Kamis, 17 April 2008

How To Check MySQL Connection is in use by another

// ******************************************************************
// MS-SQL : CONNECTION IS IN USE BY ANOTHER STATEMENT
// Category : ADO
// Author : DelphiFAQ.com
// Author Email : tips@delphifaq.com
// Author Web : http://www.delphifaq.com
// Tips Website : Delphi FAQ
// Tips Website URL: http://www.delphifaq.com
// ******************************************************************

{
When porting a larger database application (130k LOC) that worked fine with
Oracle and InterBase to MS-SQL (6.5), I frequently got the error message
‘connection is in use by another statement’.

At first, creating a new TDatabase for each TTable/ TQuery seemed to be
necessary.

Then I found what was ‘wrong’ (not really wrong.. :-)

To speed up some of my queries, I had set the property Unidirectional to true.
Delphi creates for such queries only one cursor (versus two for bidirectional
queries or TTables). After removing the assignments of Unidirectional := true
the error message disappeared and everything worked fine.

The following code resulted in the exception ‘connection is in use by another
statement’:
}

// dataBaseNameS : string is the name of the alias (MS-SQL 6.5)
begin
Query1 := TQuery.Create (Application);
With Query1 do
begin
DatabaseName := dataBaseNameS;
SQL.Text := ‘SELECT * FROM ABLESTOP’;
// the exception disappears if the following is removed
Unidirectional := True;
Open;
end;
ShowMessage (’ok’)

Table1 := TTable.Create (Self);
With Table1 do
begin
DatabaseName := dataBaseNameS;
TableName := ‘COMPONENT_PLAN’;
UpdateMode := upWhereKeyOnly;
Open
end;

Table1.Insert;
Table1.FieldByName (’PARTNO’).AsString := IntToStr (GetTickCount);
Table1.FieldByName (’ID’).AsString := ‘WWxx’;
Table1.FieldByName (’VERSION’).AsInteger := 1;
// the exception will occurr in the next statement:
// “Connection is in use by another statement”
Table1.Post;
end;

Tidak ada komentar: