You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Stephan Coboos <cr...@gmx.net> on 2004/02/09 19:38:02 UTC

How to initialize a component coming from the pool?

Hello,

I do not understand how to initialize a component which comes from the 
pool. Please have a look at the following example:

public class AnotherComponent implements ... {

   private MyComponent myComponent:
   private ServiceManager serviceManager;

   public void recycle() {

      this.serviceManager.release(this.myComponent);
   }

   public void serice(ServiceManager manager) throws ServiceException {

      this.serviceManager = manager;
   }

   public void initialize() throws Exception {

      this.myComponet = (MyComponent)this.manager.lookup(MyComponent.ROLE);
   }
}

The "AnotherComponent" is pooled. What I want to do is to get an new 
"MyComponent" from the serviceManager like in initialize() but 
initialize() is only called once at instanciation time. So how can I get 
a new component "MyComponent" from the serviceManager? Is there another 
method avaiable which will be called between the pool and the lookup of 
the serviceManager so I can get "MyComponent" there?

Thank you very much!

Regards
Stephan


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


Re: How to initialize a component coming from the pool?

Posted by Stephan Coboos <cr...@gmx.net>.
Jorg Heymans wrote:

>
> Have you tried raising this on the developer list?

Yes, and on the avalon mailing list.

>
> Sorry i can't be of more help :)

Ok. Thank you very much.

Regards
Stephan

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


Re: How to initialize a component coming from the pool?

Posted by Jorg Heymans <jh...@domek.be>.
>>
> 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:
> 
i had a use case for this as well a while ago (but with something less 
dangerous than database connections) so i ended up doing lookup and 
release each time all over the class.

Have you tried raising this on the developer list?

Sorry i can't be of more help :)
Jorg


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


Re: AW: How to initialize a component coming from the pool?

Posted by Stephan Coboos <cr...@gmx.net>.
Olivier Billard wrote:

> Hi Stefan,
>
> I had the same pb, and I solved it the way Marco wrote, that is to say :
>
> private field :
>  - private Connection connection;
>
> initialize (called once in the lifetime) :
>  - get the datasource
>
> dispose (called once in the lifetime);
>  - release the datasource
>
> recycle (called each time your component returns to the pool) :
>  - close the connection and set it to null;
>
> private getConnection() {
>     if (connection==null)
>         connection = datasource.getConnection();
>     return connection;
> }
>
> and each time you need a connection, use getConnection() to get it.
>
>
> For me, it works good.


OK, I will try it.

Thank you.

Stephan

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


Re: AW: How to initialize a component coming from the pool?

Posted by Olivier Billard <ob...@rennes.jouve.fr>.
Hi Stefan,

I had the same pb, and I solved it the way Marco wrote, that is to say :

private field :
  - private Connection connection;

initialize (called once in the lifetime) :
  - get the datasource

dispose (called once in the lifetime);
  - release the datasource

recycle (called each time your component returns to the pool) :
  - close the connection and set it to null;

private getConnection() {
     if (connection==null)
         connection = datasource.getConnection();
     return connection;
}

and each time you need a connection, use getConnection() to get it.


For me, it works good.
HTH
--
Olivier Billard


On 10/02/2004 13:47, Stephan Coboos wrote:

> Marco Rolappe wrote:
> 
>> and why isn't that possible? AFAIK implementing Poolable doesn't prevent
>> implementing Disposable. if you implement Disposable, the component's
>> dispose() method should get called when the pool and therefore the
>> pooled/recycled component itself gets disposed.
>>  
>>
> Hello Marco,
> 
> yes, you're right. But I mean that Disposable for me has no affect
> because I'am already using Recyclable and Dispose will only be
> called if the component will be destoyed.
> 
> Best Regards
> Stephan


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


Re: AW: How to initialize a component coming from the pool?

Posted by Stephan Coboos <cr...@gmx.net>.
Marco Rolappe wrote:

>and why isn't that possible? AFAIK implementing Poolable doesn't prevent
>implementing Disposable. if you implement Disposable, the component's
>dispose() method should get called when the pool and therefore the
>pooled/recycled component itself gets disposed.
>  
>
Hello Marco,

yes, you're right. But I mean that Disposable for me has no affect
because I'am already using Recyclable and Dispose will only be
called if the component will be destoyed.

Best Regards
Stephan


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


AW: How to initialize a component coming from the pool?

Posted by Marco Rolappe <m_...@web.de>.
hi stephan,

> -----Ursprungliche Nachricht-----
> Von: users-return-62645-m_rolappe=web.de@cocoon.apache.org
> [mailto:users-return-62645-m_rolappe=web.de@cocoon.apache.org]Im Auftrag
> von Stephan Coboos
> Gesendet: Dienstag, 10. Februar 2004 12:22
> An: users@cocoon.apache.org
> Betreff: Re: AW: How to initialize a component coming from the pool?
>
>
> Marco Rolappe wrote:
>
> >hi stephan,
> >
> >I don't know exactly your use case, but...
> >
> >why don't you just keep the datasource, I assume it won't change
> anyway. so
> >just close the connection in recycle()
> >
> Yes, this is what I'am doing.
>
> >and release the data source in
> >dispose() (i.e. implement Disposable).
> >
> It's not possible because the component itself should be poolable.

and why isn't that possible? AFAIK implementing Poolable doesn't prevent
implementing Disposable. if you implement Disposable, the component's
dispose() method should get called when the pool and therefore the
pooled/recycled component itself gets disposed.


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


Re: AW: How to initialize a component coming from the pool?

Posted by Stephan Coboos <cr...@gmx.net>.
Marco Rolappe wrote:

>hi stephan,
>
>I don't know exactly your use case, but...
>
>why don't you just keep the datasource, I assume it won't change anyway. so
>just close the connection in recycle() 
>
Yes, this is what I'am doing.

>and release the data source in
>dispose() (i.e. implement Disposable). 
>
It's not possible because the component itself should be poolable.

>in your processSomethingXY()s get the
>connection by using a private method, which stores/retrives the current
>connection; when the current connection is null, get it from the data
>source, otherwise return it.
>  
>

Yes, thats what I want to do now.

Thank you.

Regards
Stephan

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


AW: How to initialize a component coming from the pool?

Posted by Marco Rolappe <m_...@web.de>.
hi stephan,

I don't know exactly your use case, but...

why don't you just keep the datasource, I assume it won't change anyway. so
just close the connection in recycle() and release the data source in
dispose() (i.e. implement Disposable). in your processSomethingXY()s get the
connection by using a private method, which stores/retrives the current
connection; when the current connection is null, get it from the data
source, otherwise return it.

> -----Ursprüngliche Nachricht-----
> Von: users-return-62635-m_rolappe=web.de@cocoon.apache.org
> [mailto:users-return-62635-m_rolappe=web.de@cocoon.apache.org]Im Auftrag
> von Stephan Coboos
> Gesendet: Dienstag, 10. Februar 2004 11:07
> An: users@cocoon.apache.org
> Betreff: Re: How to initialize a component coming from the pool?
>
>
> 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
>


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


Re: How to initialize a component coming from the pool?

Posted by Stephan Coboos <cr...@gmx.net>.
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


Re: How to initialize a component coming from the pool?

Posted by Jorg Heymans <jh...@domek.be>.
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?



Stephan Coboos wrote:

> Jorg Heymans wrote:
> 
>> Looking at
>> http://avalon.apache.org/framework/principals/lifecycle.html
>>
>> Does implementing Startable and Stoppable work?
>>
>> Jorg
>>
> Hello Jorg,
> 
> I'd tried it but the method start() will be called only once like 
> initialize() and then never more. How to stop it?
> 
> Thank you.
> 
> Regards
> Stephan


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


Re: How to initialize a component coming from the pool?

Posted by Stephan Coboos <cr...@gmx.net>.
Jorg Heymans wrote:

> Looking at
> http://avalon.apache.org/framework/principals/lifecycle.html
>
> Does implementing Startable and Stoppable work?
>
> Jorg
>
Hello Jorg,

I'd tried it but the method start() will be called only once like 
initialize() and then never more. How to stop it?

Thank you.

Regards
Stephan

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


Re: How to initialize a component coming from the pool?

Posted by Stephan Coboos <cr...@gmx.net>.
Jorg Heymans wrote:

> Looking at
> http://avalon.apache.org/framework/principals/lifecycle.html
>
> Does implementing Startable and Stoppable work?
>
> Jorg
>
Ahh, thats it!!  The doc says

"These lifecycle methods are only called once in the entire life of a 
component:

- contextualize
- service
- configure
- parameterize
- initialize
- dispose"

And therefore in my opinion startable should work. I get to trying it 
and will inform you abaout that.

Thank you.

Regards
Stephan

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


Re: How to initialize a component coming from the pool?

Posted by Jorg Heymans <jh...@domek.be>.
Looking at
http://avalon.apache.org/framework/principals/lifecycle.html

Does implementing Startable and Stoppable work?

Jorg

Stephan Coboos wrote:
> Hello,
> 
> I do not understand how to initialize a component which comes from the 
> pool. Please have a look at the following example:
> 
> public class AnotherComponent implements ... {
> 
>   private MyComponent myComponent:
>   private ServiceManager serviceManager;
> 
>   public void recycle() {
> 
>      this.serviceManager.release(this.myComponent);
>   }
> 
>   public void serice(ServiceManager manager) throws ServiceException {
> 
>      this.serviceManager = manager;
>   }
> 
>   public void initialize() throws Exception {
> 
>      this.myComponet = (MyComponent)this.manager.lookup(MyComponent.ROLE);
>   }
> }
> 
> The "AnotherComponent" is pooled. What I want to do is to get an new 
> "MyComponent" from the serviceManager like in initialize() but 
> initialize() is only called once at instanciation time. So how can I get 
> a new component "MyComponent" from the serviceManager? Is there another 
> method avaiable which will be called between the pool and the lookup of 
> the serviceManager so I can get "MyComponent" there?
> 
> Thank you very much!
> 
> Regards
> Stephan


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