You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Christian Pich <pi...@llauwl.com> on 2002/08/09 20:23:47 UTC

How to handle Connection object from ActionServlet

I am wondering how you best architect the flow of the connection object:
It is suggested in Struts to obtain a Connection object in the ActionServlet
and that is the place where you close or free up the connection after 
the request.
But how do you pass that object down to your middletier where you have 
the views.
 From the Action Servlet I am calling my business classes (beans), e.g. 
create user.
In that class then you call the class where you actually create the sql 
(views, inserts etc.)
or the PreparedStatement. Where would you call the connection and do the 
execute?
To pass the connection object into the bean you have to do that in the 
constructor
or set it with a set-method. Which then means you have to do that every 
time you
call a business object separately from the Action class. It feels like 
in the business layer
I do not want to care about the connection. Is there a more elegant  
way to do this.



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: How to handle Connection object from ActionServlet

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Fri, 9 Aug 2002, Christian Pich wrote:

> Date: Fri, 09 Aug 2002 11:23:47 -0700
> From: Christian Pich <pi...@llauwl.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: struts-user@jakarta.apache.org
> Subject: How to handle Connection object from ActionServlet
>
> I am wondering how you best architect the flow of the connection object:
> It is suggested in Struts to obtain a Connection object in the ActionServlet
> and that is the place where you close or free up the connection after
> the request.
> But how do you pass that object down to your middletier where you have
> the views.
>  From the Action Servlet I am calling my business classes (beans), e.g.
> create user.
> In that class then you call the class where you actually create the sql
> (views, inserts etc.)
> or the PreparedStatement. Where would you call the connection and do the
> execute?
> To pass the connection object into the bean you have to do that in the
> constructor
> or set it with a set-method. Which then means you have to do that every
> time you
> call a business object separately from the Action class. It feels like
> in the business layer
> I do not want to care about the connection. Is there a more elegant
> way to do this.
>

I've seen three common patterns for this, and personally prefer the third
one:

* Pass the connection to use as a method parameter to your
  business layer objects.

* Make the connection pool available via some static method
  so that the business layer objects can access them directly.

* Use JNDI resources to make your connection pool available
  globally (for examples, see the Tomcat jndi-resources-howto.html
  page), which works on any J2EE server.  This is sort of a
  variation on static variables, but has the additional benefit
  that your app doesn't have to set up or manage the connection
  pool itself -- that's all handled for you by the container.

Craig


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>