Subject RE: IBO and threads
Author

Thanks for your input! I'll check it out.


/Dany 



---In ibobjects@yahoogroups.com, <supportlist@...> wrote:

ConnectionPooling was intended for use in such a situation as you describe but it is by no means any kind of a substitute for you keeping track of your own module instances for use in a muti-threaded service application. For that I have written my own type of system that can be seen in the source\web\server folder of IBO 5.
 
Jason


From: IBObjects@yahoogroups.com [mailto:IBObjects@yahoogroups.com] On Behalf Of danymarmur@...
Sent: Friday, September 13, 2013 7:06 AM
To: IBObjects@yahoogroups.com
Subject: [IBO] RE: IBO and threads

I usually do it like that or have a complete DataModule instantiated instead. The DataModule would have all creation and destructions stuff in it and some public procedure to actually do work.


I use one DataModule per "task" and see to it that stuff like "webuser" and "id's" are parameters so that i can pool instances of the same DataModule class. Then, depending on what's being done, the quieries/procedures might just have to be refreshed/re-executed (plus transactions commited/rolled back of course).


A timer then sees to it to access (unlock) datamodule instances from the pool after a certain time of inactivity and disconnect them from the server. That way the DB will keep all transaction counters nice and tight.


My question now is about the connection pooling in IBO. Was it intended to be used like above? I would have expected the pooling to be "session-pooling" instead. Any pointers as how and if i can use the IBO pooling in some way instead of my own would be appreciated!


Thanks!


/Dany



--- In ibobjects@yahoogroups.com, <supportlist@...> wrote:

I confirm that this looks like a good way to go.
You need some exception handling added in there, but I suppose you realize that.
 
Jason
 


From: IBObjects@yahoogroups.com [mailto:IBObjects@yahoogroups.com] On Behalf Of soerensoerensen@...
Sent: Thursday, September 12, 2013 3:26 AM
To: IBObjects@yahoogroups.com
Subject: RE: Re: [IBO] IBO and threads

 Thank you Rob I was Down the same road but was sure if it was the proper way to do it.



--- In IBObjects@yahoogroups.com, <ibobjects@yahoogroups.com> wrote:

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 me with 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
Version: 2013.0.3392 / Virus Database: 3222/6656 - Release Date: 09/11/13