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/02 19:38:13 UTC

Avalon-Component and pooling

Hello,

in my avalon component I'am retrieving a datasource object and from that 
I'am getting a connection object. My component has several methods and 
some of them are communicating together. Like the following:

public Object methodA() {
 
   // Here connection will be used...
   return new Object();
}

public void methodB() {

   // Here connection will be used...
   this.methodA();
}

....

So how to share connections between the methods?

The first (bad) idea is to create a new Connection each time a new 
Method is started:

public void method() {

   try {
      Connection con = this.datasource.getConnection();
      ...
   } catch(SQLException ex) {
      // Do something
   } finally {
      con.close();
   }
}

Another idea is to implement the interfaces Initializable and Recyclable 
and creating in method initialize() the connection and returning it back 
to the pool in recycle(). What do you think about that?

But there is one problem: What happens if an exception occures which was 
not catched? Like a ParameterException or a NullPointer? How to return 
the connection clear back to the pool?

Is there a best practice way?

Thank you!

Regards
Stephan




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


Re: Avalon-Component and pooling

Posted by Joerg Heinicke <jo...@gmx.de>.
Asking Avalon specific questions on developers list (or even Avalon 
users) will give more response I guess ...

But to your question: Isn't the dispose() function what you are 
searching for, i.e. releasing resources on the end of usage time?

What about comparing it with existing components as modular DatabaseAction:
http://cvs.apache.org/viewcvs.cgi/cocoon-2.1/src/blocks/databases/java/org/apache/cocoon/acting/modular/DatabaseAction.java.

Joerg

On 02.02.2004 19:38, Stephan Coboos wrote:

> Hello,
> 
> in my avalon component I'am retrieving a datasource object and from that 
> I'am getting a connection object. My component has several methods and 
> some of them are communicating together. Like the following:
> 
> public Object methodA() {
> 
>   // Here connection will be used...
>   return new Object();
> }
> 
> public void methodB() {
> 
>   // Here connection will be used...
>   this.methodA();
> }
> 
> ....
> 
> So how to share connections between the methods?
> 
> The first (bad) idea is to create a new Connection each time a new 
> Method is started:
> 
> public void method() {
> 
>   try {
>      Connection con = this.datasource.getConnection();
>      ...
>   } catch(SQLException ex) {
>      // Do something
>   } finally {
>      con.close();
>   }
> }
> 
> Another idea is to implement the interfaces Initializable and Recyclable 
> and creating in method initialize() the connection and returning it back 
> to the pool in recycle(). What do you think about that?
> 
> But there is one problem: What happens if an exception occures which was 
> not catched? Like a ParameterException or a NullPointer? How to return 
> the connection clear back to the pool?
> 
> Is there a best practice way?
> 
> Thank you!
> 
> Regards
> Stephan

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


Re: Avalon-Component and pooling

Posted by Olivier Billard <ob...@rennes.jouve.fr>.
In addition, AFAIK the initialize method is only called once during the like of the 
component, so once recycled, your component won't get a fresh connection...

You can use initialize() in combination with dispose().
Get and release a connection shouldn't cost a lot, since excalibur don't really close 
connections when released, as Sylvain Wallez answered to me some time ago [1]. The 
connection you get is only a wrapper around the real connection.

[1] : http://marc.theaimsgroup.com/?t=106560733600001&r=1&w=4

--
Olivier


On 02/02/2004 19:38, Stephan Coboos wrote:

> Hello,
> 
> in my avalon component I'am retrieving a datasource object and from that 
> I'am getting a connection object. My component has several methods and 
> some of them are communicating together. Like the following:
> 
> public Object methodA() {
> 
>   // Here connection will be used...
>   return new Object();
> }
> 
> public void methodB() {
> 
>   // Here connection will be used...
>   this.methodA();
> }
> 
> ....
> 
> So how to share connections between the methods?
> 
> The first (bad) idea is to create a new Connection each time a new 
> Method is started:
> 
> public void method() {
> 
>   try {
>      Connection con = this.datasource.getConnection();
>      ...
>   } catch(SQLException ex) {
>      // Do something
>   } finally {
>      con.close();
>   }
> }
> 
> Another idea is to implement the interfaces Initializable and Recyclable 
> and creating in method initialize() the connection and returning it back 
> to the pool in recycle(). What do you think about that?
> 
> But there is one problem: What happens if an exception occures which was 
> not catched? Like a ParameterException or a NullPointer? How to return 
> the connection clear back to the pool?
> 
> Is there a best practice way?
> 
> Thank you!
> 
> Regards
> Stephan


-- 
Olivier BILLARD


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


Re: Avalon-Component and pooling

Posted by Jorg Heymans <jh...@domek.be>.
> 
> Another idea is to implement the interfaces Initializable and Recyclable 
> and creating in method initialize() the connection and returning it back 
> to the pool in recycle(). What do you think about that?
> 
> But there is one problem: What happens if an exception occures which was 
> not catched? Like a ParameterException or a NullPointer? How to return 
> the connection clear back to the pool?
It is my assumption that even if you get uncaught exceptions during the 
active lifetime of a component, cocoon still recycle()'s the component.

You can try this out by throwing a runtime exception in your setup() 
method and put a logging statement in recycle(). (ofcourse this is 
usually the point where someone says "yep done that and recycle is not 
called")

> 
> Is there a best practice way?
> 
> Thank you!
> 
> Regards
> Stephan


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