Subject | Re: [IBO] IBO and threads |
---|---|
Author | Robert Martin |
Post date | 2013-09-11T22:06:21Z |
Not sure why this has not come through so here is a resend...
Hi
We do lots of threads with IBO and always use the code method. Here is a
basic example of how we do it....
procedure TMyThread.Execute;
begin
//Create session for this thread
ClientSesion := TIB_Session.Create(Nil);
ClientSesion.UseCursor := False;
//Create Database for all Query components
IBODataBase := TIBODatabase.Create(ClientSesion);
IBODataBase.IB_Session := ClientSesion;
IBODataBase.Path := Owner.DataBasefullPath;
IBODataBase.SchemaCacheDir :=
ExtractFilePath(IBODataBase.Path);
IBODataBase.Server := SystemServerName +
FormattedSystemFB_Port;
IBODataBase.Password := WS_DB_PASSWORD;
IBODataBase.Username := WS_DB_USERNAME;
IBODataBase.Protocol := cpTCP_IP;
IBODataBase.Connected := True;
fWriteTransaction :=
TIB_Transaction.CreateForSession(IBODataBase, IBODataBase.IB_Session);
fWriteTransaction.Isolation := tiCommitted;
fReadOnlyTransaction :=
TIB_Transaction.CreateForSession(IBODataBase, IBODataBase.IB_Session);
fReadOnlyTransaction.Isolation := tiCommitted;
fReadOnlyTransaction.ReadOnly := True;
fGeneralQuery :=
TIBOQuery.Create(IBODataBase.IB_Session);
fGeneralQuery.IB_Session := IBODataBase.IB_Session;
fGeneralQuery.IB_Connection := IBODataBase;
fGeneralQuery..IB_Transaction := fReadOnlyTransaction ;
fWorkControlQuery :=
TIBOQuery.Create(IBODataBase.IB_Session);
fWorkControlQuery.IB_Session := IBODataBase.IB_Session;
fWorkControlQuery.IB_Connection := IBODataBase;
fWorkControlQuery.IB_Transaction:=
fReadOnlyTransaction; //RM 12/04/2006
while (.....)
.....
end;
//Close and free all DB components
i.e.
fGeneralQuery.Active := False;
fGeneralQuery.Free;
fReadOnlyTransaction.Free;
IBODataBase.Connected := False;
IBODataBase.Free;
ClientSesion.Free;
end;
The above is cleaned code from a thread in our system. Note the Free
stuff was just typed in now so could be wrong.
When creating transactions you can use CreateForSession or I believe the
following is equivalent..
fReadOnlyTransaction :=
TIB_Transaction.Create(IBODataBase.IB_Session);
fReadOnlyTransaction.IB_Session := IBODataBase.IB_Session;
It is important that all DB components are created and freed as part of
the TThread.execute procedure.
Hope that helps
Cheers
Rob
Hi
We do lots of threads with IBO and always use the code method. Here is a
basic example of how we do it....
procedure TMyThread.Execute;
begin
//Create session for this thread
ClientSesion := TIB_Session.Create(Nil);
ClientSesion.UseCursor := False;
//Create Database for all Query components
IBODataBase := TIBODatabase.Create(ClientSesion);
IBODataBase.IB_Session := ClientSesion;
IBODataBase.Path := Owner.DataBasefullPath;
IBODataBase.SchemaCacheDir :=
ExtractFilePath(IBODataBase.Path);
IBODataBase.Server := SystemServerName +
FormattedSystemFB_Port;
IBODataBase.Password := WS_DB_PASSWORD;
IBODataBase.Username := WS_DB_USERNAME;
IBODataBase.Protocol := cpTCP_IP;
IBODataBase.Connected := True;
fWriteTransaction :=
TIB_Transaction.CreateForSession(IBODataBase, IBODataBase.IB_Session);
fWriteTransaction.Isolation := tiCommitted;
fReadOnlyTransaction :=
TIB_Transaction.CreateForSession(IBODataBase, IBODataBase.IB_Session);
fReadOnlyTransaction.Isolation := tiCommitted;
fReadOnlyTransaction.ReadOnly := True;
fGeneralQuery :=
TIBOQuery.Create(IBODataBase.IB_Session);
fGeneralQuery.IB_Session := IBODataBase.IB_Session;
fGeneralQuery.IB_Connection := IBODataBase;
fGeneralQuery..IB_Transaction := fReadOnlyTransaction ;
fWorkControlQuery :=
TIBOQuery.Create(IBODataBase.IB_Session);
fWorkControlQuery.IB_Session := IBODataBase.IB_Session;
fWorkControlQuery.IB_Connection := IBODataBase;
fWorkControlQuery.IB_Transaction:=
fReadOnlyTransaction; //RM 12/04/2006
while (.....)
.....
end;
//Close and free all DB components
i.e.
fGeneralQuery.Active := False;
fGeneralQuery.Free;
fReadOnlyTransaction.Free;
IBODataBase.Connected := False;
IBODataBase.Free;
ClientSesion.Free;
end;
The above is cleaned code from a thread in our system. Note the Free
stuff was just typed in now so could be wrong.
When creating transactions you can use CreateForSession or I believe the
following is equivalent..
fReadOnlyTransaction :=
TIB_Transaction.Create(IBODataBase.IB_Session);
fReadOnlyTransaction.IB_Session := IBODataBase.IB_Session;
It is important that all DB components are created and freed as part of
the TThread.execute procedure.
Hope that helps
Cheers
Rob
On 12/09/2013 4:55 a.m., soerensoerensen@... wrote:
>
>
> I would prefer the code solution, but how do I create a second
> instance of TIB_session, TIB_Cursor, TIB_connection? Could some one
> provide mewith an example please!
>
>
>
> --- In IBObjects@yahoogroups.com, <ibobjects@yahoogroups.com> wrote:
>
> I recommend making a data module to hold everything that goes together.
> Put a TIB_Session and however many TIB_Connection, TIB_Query, etc.
> components you need on that module.
> Then, whe you have a need for it to be used, create a TThread and an
> instance of that DataModule and marry the two together.
> You can also just do it in code without a data module. I normally
> start by creating a TIB_Session instance and then thereafter I use
> that instance as the Owner when I create the subsequent connections,
> queries, etc. Then, I have the thread do whatever is needed within
> that TIB_Session context.
> The rule of thumb is as long as you only have a thread active within a
> single TIB_Session's full context you are thread safe.
> Jason
>
> ------------------------------------------------------------------------
> *From:* IBObjects@yahoogroups.com [mailto:IBObjects@yahoogroups.com]
> *On Behalf Of *soerensoerensen@...
> *Sent:* Tuesday, September 10, 2013 11:47 AM
> *To:* IBObjects@yahoogroups.com
> *Subject:* [IBO] IBO and threads
>
> I'm developing a threaded server and use IBO and Delphi XE4. But how
> do I create a IBconnection, IBcursor and IBsession for each thread? I
> tried putting the creation under threadvar. I can compile but gets a
> runtime error (read access error)
>
>
>
>
>
> No virus found in this message.
> Checked by AVG - www.avg.com <http://www.avg.com>
> Version: 2013.0.3392 / Virus Database: 3222/6656 - Release Date: 09/11/13
>