You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Thomas Mahler <th...@web.de> on 2003/03/26 21:27:13 UTC

[FAQ] Re: How to obtain a Broker instance

Hi Sylvain,

In the tutorial1 I followed a very simply scheme.
The application is considered to be single threaded. Thus there is only 
one broker instance created for the whole application.

This single instance is passed to the use case objects that are 
reponsible for implementing the business logic.
This is done in the registration of use cases phase. SHoudn't be too 
difficult to find in the code.

Of course you will follow different approaches in multi threaded code.
Say you have a method myThreadSafeCode() than be executed by multiple 
threads (e.g. within a Servlet or SessionBean component).
You would handle the broker as follows:

public void myThreadSafeCode()
{
     // obtain broker instance from pool
     PersistenceBroker broker =
                     PersistenceBrokerFactory.defaultPersistenceBroker();

     // do some persistance work with the broker here

     // close broker and return it to the pool
     broker.close();

     // do not work with a broker instance after closing!

}

It obviously depends on your application if it is appropriate to store a 
broker instance in a variable and pass it to other components.

I hope the single- and multithread example given above makes things 
clearer for you.

cheers,
Thomas


Sylvain.Thevoz@swisscom.com wrote:
> (repost)
> Hello,
> 
> In the PB tutorial (tutorial1) the author explains how to obtain a Broker instance:
> 
> You put the code below in the constructor of your class.
> 
> ----code----
> public MyClass()
> {
>     PersistenceBroker broker = null;
>     try
>     {
>         broker = PersistenceBrokerFactory.
>                     defaultPersistenceBroker();
>     }
>     catch (Throwable t)
>     {
>         t.printStackTrace();
>     }
> }
> ----code----
> 
> I don't understand how the PB instance is passed to the constructor of other classes in this example.
> 
> My situation is: a class (the controller) creates an instance of a JavaBean. I want to create the PB instance in this JavaBean. How to store the PB instance?
> 
> 
> Someone could give me advices about that?
> 
> Thanks
> Sylvain
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
>