Subject RE: [IBO] Thread Error - Bug
Author Jason Wharton
As a side note:

This code is not correct.

if not IB_Transaction.Started then
IB_Transaction.StartTransaction;

The Started property has to do with the physical transaction handle being
present. The StartTransaction method has purely to do with an explicit
(programmer's context only) aspect of transactions.

You should either write it like this:

if not IB_Transaction.InTransaction then
IB_Transaction.StartTransaction;

Or, you should simply delete that code and allow a physical transaction to
be started when appropriate automatically.

Your description of your problem needs to be more clear. I don't know
exactly what you mean by "hangs".

Jason Wharton

-----Original Message-----
From: Vincenzo Scarpellino [mailto:vincenzoscarpellino@...]
Sent: Monday, August 30, 2004 3:31 AM
To: IBObjects@yahoogroups.com
Subject: [IBO] Thread Error - Bug



FB 1.5 - IBO 4.3.Aa - WIN XP Prof.


Hi,

Before my holiday I wrote any messages about to execute a query in
background with TThread Object, but I didn't understand if my problems
depends by a bug or an wrong error using IBObjects. I am converting my BDE
application and I am trying to get the same behavior with run a query in
background. It work fine when I let elapse a little time before to run a new
query: I have a button to run my query (see borland demos in
delphi7\demos\db\BkQuery I am using that sample of course changing reference
objects) when I click without let elapse this time first query hangs and
second work fine. Next there is my code (I tried to use IB_Cursor or
IB_Query with IB_Grid with IB_Datasource but nothing changed). Why doesn't
it work fine ? Is it a bug ?


type
TForm2 = class(TForm)
DBGrid1: TDBGrid;
Navigator1: TDBNavigator;
DSThread: TDataSource;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

{ TEurSecurityThread }
type
TEurSecurityThread = class(TThread)
private
FSession:TIB_Session;
FConnection: TIB_Connection;
FTransaction: TIB_Transaction;
FQuery: TIBOQuery;
procedure ConnectQuery;
protected
procedure Execute; override;
public
constructor Create(QForm:TForm2);
end;

procedure BackgroundQuery;

implementation

{$R *.dfm}

{ BackgroundQuery }
procedure BackgroundQuery;
begin
Form2 := TForm2.Create(Application);

with Form2 do
begin
Show;
end;

TEurSecurityThread.Create(Form2);
end;

constructor TEurSecurityThread.Create(QForm:TForm2);
begin
Form2 := QForm;
FreeOnTerminate := True;
inherited Create(False);
end;

procedure TEurSecurityThread.ConnectQuery;
begin
with Form2 do
begin
DSThread.DataSet:= FQuery;
DBGrid1.DataSource:= DSThread;
Navigator1.DataSource:= DSThread;

WindowState:= wsNormal;
BringToFront;
end;
end;

procedure TEurSecurityThread.Execute;
begin
with Form2 do
try
try
if (FSession = nil) then
begin
FSession := TIB_Session.Create(nil);

FConnection:= TIB_Connection.Create(FSession);

FConnection.Protocol:= cpTCP_IP;
FConnection.Server:= '192.168.255.2';
FConnection.Path:= 'C:\EURARCHI\DATABASE';
FConnection.SQLDialect:= 3;
FConnection.Username:= 'SYSDBA';
FConnection.Password:= 'masterkey';

FTransaction:= TIB_Transaction.Create(FSession);
FTransaction.IB_Connection:= FConnection;

FQuery:= TIBOQuery.Create(FSession);
FQuery.IB_Transaction:= FTransaction;
FQuery.IB_Connection:= FConnection;


with FQuery do
begin
if not IB_Transaction.Started then
IB_Transaction.StartTransaction;

FQuery.SQL.Clear;
FQuery.SQL.Add('SELECT * FROM CLF WHERE EURAZNOME= ''GRP''');

if not Prepared then Prepare;
Open;
end;
end;

Synchronize(ConnectQuery);
except
WindowState:= wsNormal;
end;
finally
WindowState:= wsNormal;
end;
end;

end.