On Tue, 10 Jun 2003, Hibbs, David wrote:

> Date: Tue, 10 Jun 2003 16:15:24 -0500
> From: "Hibbs, David" <[EMAIL PROTECTED]>
> Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]>
> To: "Struts-Users (E-mail)" <[EMAIL PROTECTED]>
> Subject: RE: Best practive for presenting a search result to a user
>
>       I disagree wholeheartedly with this practice for several reasons.
>
>       First and foremost, the problem with passing the result set directly
> to the JSP is that you must make sure to close your connections when done.
> Your JSP might be able to close your result set, but your statement and
> connection--as a best case scenario--have to wait for the GC.  Worst case
> they stay out there and go stale, but they can still block active threads
> from accessing the DB when you exceed your max connection count.
>
>       Second, what happens if you encounter a SQLException while
> processing the ResultSet?  Error handling is best done at the controller
> level (when notified by the model, of course).
>
>       Third, and I've already said this, but processing results belongs in
> the model.  Your view tier should not be your model.  This is the whole
> premise of MVC--don't put your control or business logic in your view!
>
>       Fourth, a ResultSet in itself is not Serializable, so you don't want
> to try and pass it around, store it in the session, or any other silly thing
> like that.
>
>       So the Best Practice IMHO is to have a model component perform your
> query, load the results into a generic list of DTO's.  It then becomes
> trivial to display the content of the search results, page back and forth,
> and so on.  It also maintains the integrity and separation of your MVC
> design.
>

If you *really* like using result sets in your JSP pages (perhaps because
you have some custom tags that know how to deal with them), you can
actually have your cake and eat it too.  Check out the
org.apache.commons.beanutils.RowSetDynaClass class, which is included in
the version of commons-beanutils.jar included in Struts 1.1 RC2.  Javadocs
are available at:

  http://jakarta.apache.org/commons/beanutils/api/

Note that using this class costs you a copy operation to reproduce the
result set's contents as a collection of DynaBeans.  On the other hand,
you can close the underlying ResultSet, and return the Connnection to the
pool, before your Action forwards control to the JSP page.

And, because all the Struts tags know how to deal with DynaBeans,
iterating over this collection is just like iterating over a collection of
JavaBeans.

> David Hibbs
> Staff Programmer / Analyst
> Distributed Applications Development and Support
> American National Insurance Company
>

Craig

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to