George Ludwig wrote:
> I have a servlet that connects to a jdbc datasource. Normally, I would
> instantiate the connection in the servlet init() method. However, recent
> discussion on this list has informed me that the servlet container can destroy
> and recreate the servlet at will.
>
The spec says that it can do this. Most current servlet containers leave a
servlet active for the life of the server once it is initiated, unless they also
implement auto-reloading on Java class changes.
>
> What does this mean in regard to establishing a jdbc connection? If the servlet
> container invokes the init() method when it re-instantiates the servlet, I
> guess it's still fine to establish my connection in the init() method. However,
> if it does not, then I need to rethink my strategy.
>
If the servlet was destroyed and then reloaded (as the spec allows), the init()
method of the new instance would be called before request processing began again.
>
> Is there any common wisdom on this? Where can I learn more about the latest
> thinking on the servlet life-cycle?
>
Adding official support for "application lifecycle" events is to the servlet API
is on the priorities list for many folks involved with servlets, and is mentioned
in the recently announced JSR as a goal for the next spec version. This will
allow you to configure things like JDBC connection pools that are opened at
application startup, and closed at application shutdown, independent of the
lifecycle of individual servlets.
In the mean time, I create my JDBC connection pools in the init() method of a
servlet that has been configured to load at startup time, but is not mapped to any
particular URI. The destroy() method of this same servlet has the code to
gracefully shut down the connection, and I rely on the fact that the servlet
engine I use doesn't actually do what the spec allows in terms of destroying
instances while it is running.
>
> TIA,
>
> George
>
Craig McClanahan
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html