struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Frederic Laub" <>
Subject RE: Database Connection in Logic Beans - pooling?
Date Mon, 02 Sep 2002 21:02:07 GMT
Thanks to Troy,

But I read a lot of Java literature and the struts archive over the past
weeks but in the all the examples the connection is retrieved and returned
is the same class. I know it is the easiest and the best way to handle the
In most cases it provides a solution.
I would like to implement a more flexible solution:
- Retrieving a connection and saving it in the context (request scope) or
saving an id associated with the connection.
- When a connection is needed in the request, look first in the context if a
connection has already been retrieved.
- Based on a parameter => return the connection in the class.
- Check at the end of the request if a connection exists, if yes return it
to the pool.
=> Where does the request cycle ends?
At the source I'm a database programmer so I prefer to manage the
transactions myself.
I think that another advantage of using a same connection through the whole
request is the fact that in case of heavy loading, when connections are
difficult to obtain, the request can be served as a whole. I prefer X
requests with 100% satisfaction and y not satisfied than x+y requests with
30% satisfaction. If you understand what I mean?

Your help or advice will be appreciated.

I've excluded EJB for my project because most of the data is read only or
linked to one user only.

-----Original Message-----
From: Troy Hart []
Sent: Monday, September 02, 2002 9:17 PM
To: Struts Users Mailing List
Subject: RE: Database Connection in Logic Beans - pooling?

You can solve this problem by providing a business service framework for
your application (There has been a lot of talk about this sort of thing
on this list...I can't think of a particular thread to reference for you
so you may just want to go through the archives). My approach to this
problem has been to create a set of very coarse grained business service
components (ours is an ecommerce system so we have components like the
following: one for cataloging, one for requisitioning, one for order
services, and etc...), where each component provides a public API that
mirrors the "use-cases" one-to-one (for the most part). I think you will
find that the top level "use-cases" defined for your system provide
natural scoping boundaries for your transactions, and resource
management in general.

This approach will leave you with a set of re-usable business components
that are not tied to struts and/or the servlet paradigm. Also, this API
provides an abstraction that will allow you to hide complexity from your
struts actions (or whatever else uses the API). For example, the
component you expose could be a facade to an EJB Session bean

Anyway, my suggestion is to think about your problem a little
differently. Sorry if I didn't provide enough details, my intention was
simply to give a high level overview of how I see the problem and the
solution. Once again, I would urge to lookup other threads on this topic
to read what other people have said.

Hope this helps,


On Mon, 2002-09-02 at 10:45, Frederic Laub wrote:
> Hi,
> How do achieve the following:
> In some cases I want to get a connection at the beginning of a request
> (request scope), pass the same connection to all the java beans that are
> called in the request and return the connection at the end of the request.
> Where do I put the code to return the connection to the pool at the end of
> the request and in case of an error be sure that the connection is
> to the pool?
> A same connection is required when all DML statements throughout a request
> are part of a same transaction.
> The commit (or rollback in case of an error) statement is issued at the
> of the request before the connection is returned to the pool.
> Sorry if the vocabulary I used is database oriented and not 100 %
> java/struts compatible.
> Your help will be appreciated.
> Frederic
> -----Original Message-----
> From: []
> Sent: Monday, September 02, 2002 9:06 AM
> To: Struts Users Mailing List
> Subject: Re: Database Connection in Logic Beans - pooling?
> Steve McLeod writes:
> > I am using:
> > Tomcat 4.0
> > Struts 1.0.2
> >
> > The problem
> > ========
> >
> > I have successfully used the Struts database connection pooling in a
> > web app, but as far as I can tell, a reference to the datasource can
> be
> > obtained from within an Action class (or directly within a JSP page but
> > let's not think about that today).
> >
> > However I would like to have logic beans which handle database access,
> > rather than have this in the Action class. But I can't get a reference
> > the datasource from the logic bean because it doesn't have a
> ServletContext
> > to which I can get a handle.
> >
> > I have toyed with various ideas:
> > - Initialise a logic bean by passing it a reference to the Servlet
> > - Acquire a connection in the Action class and pass that to the bean
> >
> > But really, I would rather the logic bean know inherently how to acquire
> > database connection.
> >
> > My current workaround is to not use the Struts connection pooling, and
> > rather to manually create a connection each time database access needs
> be
> > done, then destroy it. But this is clearly not suitable for our
> > environment.
> There are a few ways to solve your problem. One way would be to bind a
> datasource to some JNDI name that your logic beans are aware of. Then,
> that name, your logic beans can lookup the datasource completely
> of struts.
> Give it a try,
> Troy
> >
> >
> > The context of my problem
> > ==================
> >
> > I want to use some code like this in a JSP:
> >
> > <jsp:useBean id="abean" scope="page"
> > class="" />
> > <table border="1">
> >     <tr>
> >         <th>#</th>
> >         <th>Client</th>
> >         <th>Active</th>
> >     </tr>
> > <logic:iterate id="clientList" name="abean" property="clients"
> > type="">
> >     <tr>
> >         <td><bean:write name="clientList" property="clientID"/></td>
> >         <td><bean:write name="clientList" property="clientName"/></td>
> >         <td><bean:write name="clientList" property="active"/></td>
> >     </tr>
> > </logic:iterate>
> >
> >
> > The idea is that ClientManager is used to handle all general database
> tasks
> > for the Client bean (which maps to a Client entity in the database).
> > ClientManager.getClients() connects to the database, creates an
> of
> > Client objects, one for each row in the database, and returns the
> ArrayList.
> >
> > ClientManager has other methods, such as:
> > -         ClientManager.delete(Client c), which deletes the row in the
> > database entity corresponding to the specified client.
> > -         ClientManager.findByPrimaryKey(int ID) which returns the
> > which matches the specified ID
> > - c), which stores the client in the
> > database, creating or updating as necessary
> >
> > So an Action class can also call any of these directly, and it really
> > shouldn't care about how these work and how they store to/retrieve from
> the
> > database. But I can't think of the elegant way to do this and still be
> able
> > to use the Struts connection pooling.
> >
> > Any thoughts?
> >
> > Thanks
> >
> > Steve McLeod
> >
> >
> >
> --
> To unsubscribe, e-mail:
> <>
> For additional commands, e-mail:
> <>
> --
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message