S
S
Sergey2015-07-09 17:01:48
Delphi
Sergey, 2015-07-09 17:01:48

How to organize work with the FireBird database from several threads?

I'm digging towards TIBConnectionbroker, but I can't figure out how to properly organize the work. Console application.
Here are a few lines from the code.
Application itself:

program main;

{$APPTYPE CONSOLE}

uses
  IBX.IBConnectionBroker,
  MyThread in 'MyThread.pas';

var
  Thread1, Thread2: TMyThread;
  ConnectionBroker: TIBConnectionBroker;

begin
  ConnectionBroker:=TIBConnectionBroker.Create(nil);
  ConnectionBroker.DatabaseName:='C:\DataBase.fdb';
  ConnectionBroker.Params.Clear;
  ConnectionBroker.Params.Add('user_name=SYSDBA');
  ConnectionBroker.Params.Add('password=masterkey');

  Thread1:=TMyThread.Create(True);
  Thread1.fBroker:=ConnectionBroker;
  Thread1.Resume;

  Thread2:=TMyThread.Create(True);
  Thread2.fBroker:=ConnectionBroker;
  Thread2.Resume;
end;

Flow module:
unite MyThread;

interface

uses
  IBDatabase,
  IBCustomDataSet,
  IBX.IBConnectionBroker;

type
  TMyThread = class(TThread)
  private
    fDataBase: TIBDatabase;
    fTransaction: TIBTransaction;
    fDataSet: TIBDataSet;
  protected
    procedure Execute; override;
  published
    fBroker: TIBConnectionBroker;
  end;

implementation

procedure TMyThread.Execute;
begin
  fDatabase:=fBroker.GetConnection; // Тут вылетает Exception class $C0000005 with message 'access violation at 0x0069dc06: read of address 0x00000000'
  
  fTransaction:=TIBTransaction.Create(nil);
  fTransaction.DefaultDatabase:=fDataBase;

  fDataSet:=TIBDataSet.Create(nil);
  fDataSet.Database:=fDataBase;
  fDataSet.Transaction:=fTransaction;
end;

I can't understand it myself. Hope for public intelligence.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-07-09
@sezavasasilov

I found what's wrong.
After creating and setting parameters, TIBConnectionBrokerit was necessary to initialize it.
What ended up happening:

ConnectionBroker:=TIBConnectionBroker.Create(nil);
ConnectionBroker.DatabaseName:='C:\DataBase.fdb';
ConnectionBroker.Params.Clear;
ConnectionBroker.Params.Add('user_name=SYSDBA');
ConnectionBroker.Params.Add('password=masterkey');
ConnectionBroker.Init; //   <- Вот он, мой геморрой

Everything else remains as it was.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question