When and where to close the connection in a struts-web-application?

Arjen van der Meijden <[email protected]>
Newsgroups gmane.comp.java.ozone.user
Message-ID <[email protected]>
Hi list,

I'm building a web application with Struts and it uses ozone as its 
backend database. Well thats easily done and what I have works quite nice.

There is one problem however I don't know where or when to close the 
connection to the ozone database. I can easily open one as soon as I 
need some data, but that data will be put into the "request scope" and 
then the Struts-ActionServlet forwards the control to some jsp-file (or 
another Action).

So what I do now is something like:
public ... execute(...)
{
   ExternalDatabase db = ExternalDatabase.openDatabase(...);
   Object someObject = db.objectForName("someName");

   request.setAttribute("someObject", someObject);
   return actionMapping.findForward("success");
}

And the control gets forwarded to some jsp, that may do something like:
<jsp tags>
<bean:write name="someObject" property="someProperty" />
<some more jsp tags>

And when the entire operation is done, the database-connection should be 
close()-ed. But at that time, I have no direct control over the 
operation anymore... and so the number of open connections increases at 
every pageview.

I see a few solutions to this, but don't know which is best or even more 
exist:
- Make a static singleton reference to an ExternalDatabase object; there 
won't be much connectionpooling anymore, but you can be certain there 
will be only one databaseconnection. It may perform quite badly because 
there won't be multiple connections available at any time?

- Make some kind of value objects, create value objects via the proxies 
and close the connection as soon as the value objects are created and 
stored in the request-scope. This really doesn't have my preference, 
even though it may be saver and "better" in the application design (it 
is also much more work).

Thanks in advance and best regards,

Arjen


PS, John Dixon, I forgot to thank you for your advise/answer on my 
previous e-mail.




-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.