tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gwyn Fisher" <>
Subject Problem using JDBC resource during HttpServlet.init()
Date Wed, 25 Jun 2003 18:29:29 GMT
I am working on a webapp that consists of an HTTP Servlet and a bunch of
files that are served by the servlet in response to query parameter-based
requests, i.e. they're not static files served by the Default servlet. We
are packaging the servlet and its related files into a WAR to make upload
and deployment at our hosting provider a rational process.

In order for the servlet to be able to serve its files (amongst the other
things it does), the WAR needs to be unpacked by the autoDeploy setting,
which we believe means that we can't define a context for it, either via an
<app>.xml definition, or a <Context> element in the server.xml, as in the
presence of a defined context, Tomcat explicitly does not unpack WAR files.

The servlet overrides init() in order to be able to single thread through
database initialization code without having to synchronize anywhere else.
Within the init() method we are attempting to connect to a JDBC resource
that is defined within the <DefaultContext> (we're using MySQL, but I don't
believe that's important for the purposes of this question).

The connection is accomplished using the normal sequence of operations:

    Context context = new InitialContext();
    DataSource ds =
    Connection connection = ds.getConnection();

If I do not specify that the servlet should be loaded on Tomcat startup,
everything works fine.

If, however, we add the <load-on-startup> element to the servlet's
definition in web.xml, the lookup() operation throws an exception saying
"jdbc is not bound in this context".

After searching the news groups, we added a <resource-ref> element to


Upon restarting Tomcat, the servlet now passes the lookup() operation, but
receives an exception when attempting to open a connection using
getConnection(): Cannot load JDBC driver class 'null'.

In both cases, however, when a user makes a request of the servlet, the
init() method is again invoked and this time everything works as planned.
Both the lookup() and the getConnection() calls succeed.

So my questions are as follows:

- am I correct in my understanding that if you define a Context for a WAR,
the archive is never unpacked by Tomcat? And if so, I wonder if anybody
could explain why? The reason for asking this particular question is that in
the presence of an unpacked WAR and a defined Context (either <app>.xml or a
<Context> element within server.xml), the load-on-startup tag produces the
desired result, i.e. everything in the JDBC initialization works as planned.

- why, when using autoDeploy, if the JDBC initialization works the 2nd time
through init() -- following a user request of the servlet -- does it not
work the 1st time through -- during Tomcat startup?

- or am I just being terminally stupid and missing something really obvious?

Many thanks for any help received,
Gwyn Fisher.

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

View raw message