unit Unit2;

interface

uses
  Classes, IBSQL;

type
  TTestthread = class(TThread)
  private
    { Private declarations }
  protected
    sql : TIBSQL;
    procedure Execute; override;

  end;

implementation

uses IBDatabase, Unit1, Windows;
{ Important: Methods and properties of objects in VCL or CLX can only be used
  in a method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure TTestthread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }

{ TTestthread }

procedure TTestthread.Execute;
var
  d : TIBDatabase;
begin
  d := nil;
  sql := TIBSQL.Create(nil);
  { Place thread code here }
  while not Terminated do
  begin
    while not Assigned(d) do
      d := Form1.IBConnectionBroker1.GetConnection;

    // Always update teh BIX component's Database and Transaction properties
    // with the new pooled DB.

    sql.Database := d;
    sql.Transaction := d.DefaultTransaction;
    sql.Transaction.StartTransaction;
    sql.SQL.Text := 'select * from employee';
    sql.ExecQuery;
    while not sql.Eof do
      sql.Next;
    Sleep(1000 + Random(5000));
    sql.Close;
    sql.SQL.Text := 'select * from customer';
    sql.ExecQuery;
    while not sql.Eof do
      sql.Next;
    sql.Close;
    sql.Transaction.Commit;

    // TIBSQL does not unprepare itself when a transaction is ended.
    // It also does not unprepare itself when the transaction changes.
    // This is a bug that will be fixed in the next IBX update (.04), the
    // workaround to to manually free the handle when we give back the
    // pooled connnection because we are not assured of getting the
    // same connection/transaction as before.

    // The TIBCustomDataSet descendants to NOT suffer from this because
    // they are unprepared when their transaction ends.

    sql.FreeHandle;
    Form1.IBConnectionBroker1.ReleaseConnection(d);
    d := nil;
    Sleep(1000 + Random(2000));
  end;
  sql.Free;
end;

end.
 