You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Mario Imperato <me...@mclink.it> on 2000/04/15 12:23:34 UTC

Shared resources basic design

Hi folks, as a newbie on this technology I got a 'typical' design question.
It is about what is the preferable pattern during init and shutdown. 
Let's suppose you have a pool of shared resources (ie DB connections) that you want 
to set-up at startup time (the scenario is fairly common I think). The first idea could be to 
create a initializer/manager servlet that gets loaded as the very first, creates the 
shared objects  and stores them in the ServletContext for the other servlets to use.
The question now comes to what happens when the system shutdown.
You'd like to have a clean-up hook to gracefully release the resources. 
A first thinking could use the destroy method of the same servlet used before but from 
the specs I noticed that the servlet container can shutdown the servlets any time 'The 
servlet container is not required to keep a servlet loaded for any period of time'. This 
type of behaviour of course creates a problem in here. 
I wasn't able to find in the specs any idea on the type of suggested design for those 
cases (ie is there a listener like the httpsession one?). 

I'd really appreciate if somebody could shed some light on this.

Mario A. Imperato
e-mail: imperato@mclink.it
cell: 0335-5398828



Re: Shared resources basic design

Posted by Hans Bergsten <ha...@gefionsoftware.com>.
Mario Imperato wrote:
> 
> Hi folks, as a newbie on this technology I got a 'typical' design question.
> It is about what is the preferable pattern during init and shutdown.
> Let's suppose you have a pool of shared resources (ie DB connections) that you want
> to set-up at startup time (the scenario is fairly common I think). The first idea could be to
> create a initializer/manager servlet that gets loaded as the very first, creates the
> shared objects  and stores them in the ServletContext for the other servlets to use.
> The question now comes to what happens when the system shutdown.
> You'd like to have a clean-up hook to gracefully release the resources.
> A first thinking could use the destroy method of the same servlet used before but from
> the specs I noticed that the servlet container can shutdown the servlets any time 'The
> servlet container is not required to keep a servlet loaded for any period of time'. This
> type of behaviour of course creates a problem in here.
> I wasn't able to find in the specs any idea on the type of suggested design for those
> cases (ie is there a listener like the httpsession one?).
> 
> I'd really appreciate if somebody could shed some light on this.

The approach you describe is the best way of dealing with this in a Servlet 2.2
container. Even though, as you point out, a container can shutdown a servlet at
any time, no container do so (AFAIK) today. A better solution to this issue is
to define an application life-cycle, listed as one area to look at for Servlet
2.3
(in JSR 053).

Hans
-- 
Hans Bergsten		hans@gefionsoftware.com
Gefion Software		http://www.gefionsoftware.com