cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stephan Coboos <cromo...@gmx.net>
Subject Re: How to initialize a component coming from the pool?
Date Tue, 10 Feb 2004 10:07:04 GMT
Jorg Heymans wrote:

> Looking a bit better at the docs that was not the best advice from my 
> part sorry.
>
> I am assuming that you want to use myComponent throughout your whole 
> component, hence the need to only look it up once when it comes from 
> the pool. But what is so special about myComponent that you want a 
> fresh one every time?
>
>
Think about a SQL connection which should shared between two or more 
methods in the component. After usage of the component the connection 
must returned to the pool. Next time I will get the component from the 
pool I need another connection object from the pool.

I'm appending a part of a message which I had posted in the avalon 
mailing list about this problem:

public class AnotherComponent   implements Serviceable, 
AnotherInterface, Recyclable, Initializable
{
   private ServiceManager m_ServiceManager;
   private ComponentDatasource ds;
   private Connection con;

   public void processSomething()
   {
      Connection con = this.con;

       // Do something here with con...
       // but do not close!
   }


   public void processSomething2()
   {         Connection con = this.con;

       // Do something here with con...
       // but do not close!
   }

   public void initialize() {

    this.ds = ... // Getting the datasource from the serviceManager
       this.con = this.ds.getConnection();            }

   public void recycle() {

     // Here close the connection and return them to the pool
     this.con.close();

     // Release the datasource
     this.serviceManager.release(ds);
   }

   public void service( ServiceManager man )
   {
       m_ServiceManager = man;
   }
}

Please have a look into the code above. In the method initialize() I'am 
creating the datasource and the connection. The connection will be used 
by the two methods processSomething() and processSomething2(). I dont 
want to create a new connection or close it in each method. After the 
component will not be longer in use, I'am returning it into the pool and 
therefore the method recycle() will be called which closes the 
connection and releases the datsource. But what happens if I get the 
component the next time from the pool? Where is the initialization 
process to get the datasource and connection called? In intialize()? I 
think not because initialize() will be called only once at instanciation 
time and after that never more. So where to put the initialization code 
if I have a pooled component?

Thank you.

Regards
Stephan

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message