tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gwyn Fisher" <g...@gwynfisher.com>
Subject Re: Problem using JDBC resource during HttpServlet.init()
Date Wed, 25 Jun 2003 19:19:18 GMT
Sweet, thanks! This is exactly what I needed.
/gf

ps. Also thanks to Yoav who suggested the same thing about 2 minutes ago.
:-)

----- Original Message -----
From: "Michael Duffy" <duffymo@yahoo.com>
To: "Tomcat Users List" <tomcat-user@jakarta.apache.org>
Sent: Wednesday, June 25, 2003 3:16 PM
Subject: Re: Problem using JDBC resource during HttpServlet.init()


>
> What about this snippet?
>
> ClassLoader loader  =
> this.getClass().getClassLoader();
> InputStream stream  =
> loader.getResourceAsStream(resourceName);
>
> If the files you want are in the CLASSPATH, won't the
> class loader find them using this idiom?
>
> I've used this with success.  I have to read an XML
> config file on initialization, which I put in
> WEB-INF/classes in my WAR file.  Tomcat finds it every
> time.
>
> --- Gwyn Fisher <gwyn@gwynfisher.com> wrote:
> > This is correct. However, because the WAR file does
> > not get unpacked by
> > Tomcat in this scenario, we have other problems due
> > to needing files from
> > the directory structure that gets created during an
> > unpack operation.
> >
> > Perhaps another / better question I should ask is,
> > if there are files in
> > your WAR that you need to open during the lifetime
> > of your servlet, and
> > assuming that your WAR does not get unpacked due to
> > the presence of an
> > <app>.xml context definition, how do you open those
> > files? Obviously the
> > java.io.* packages don't work, but are there other
> > mechanisms specific to
> > servlets that would work?
> >
> > /gf
> >
> > ----- Original Message -----
> > From: "Michael Duffy" <duffymo@yahoo.com>
> > To: "Tomcat Users List"
> > <tomcat-user@jakarta.apache.org>
> > Sent: Wednesday, June 25, 2003 2:52 PM
> > Subject: Re: Problem using JDBC resource during
> > HttpServlet.init()
> >
> >
> > >
> > > I've got a JDBC data source that I get via JNDI
> > lookup
> > > using Tomcat.  My servlet has <load-on-startup>
> > set to
> > > 2, and I haven't seen this problem with the "not
> > > bound" problem.
> > >
> > > I define a context.xml that has a <Resource> tag
> > and
> > > its <ResourceParams> children.  During
> > development, I
> > > deploy the app-name.war file using the Ant
> > Catalina
> > > <deploy> task, which lets me specify a
> > app-name.xml
> > > context file.
> > >
> > > When I install the app, I put the app-name.war and
> > > app-name.xml context file in TOMCAT_HOME/webapps.
> > > Tomcat picks up both.
> > >
> > > --- Gwyn Fisher <gwyn@gwynfisher.com> wrote:
> > > > 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 =
> > > >
> > >
> >
> (DataSource)context.lookup("java:comp/env/jdbc/MySQLDB");
> > > >     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
> > > > web.xml:
> > > >
> > > >     <resource-ref>
> > > >
> > <res-ref-name>jdbc/MySQLDB</res-ref-name>
> > > >
> > <res-type>javax.sql.DataSource</res-type>
> > > >         <res-auth>Container</res-auth>
> > > >     </resource-ref>
> > > >
> > > > 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:
> > > > tomcat-user-unsubscribe@jakarta.apache.org
> > > > For additional commands, e-mail:
> > > > tomcat-user-help@jakarta.apache.org
> > > >
> > >
> > >
> > > __________________________________
> > > Do you Yahoo!?
> > > SBC Yahoo! DSL - Now only $29.95 per month!
> > > http://sbc.yahoo.com
> > >
> > >
> >
> ---------------------------------------------------------------------
> > > To unsubscribe, e-mail:
> > tomcat-user-unsubscribe@jakarta.apache.org
> > > For additional commands, e-mail:
> > tomcat-user-help@jakarta.apache.org
> > >
> > >
> > >
> >
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> > tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> > tomcat-user-help@jakarta.apache.org
> >
>
>
> __________________________________
> Do you Yahoo!?
> SBC Yahoo! DSL - Now only $29.95 per month!
> http://sbc.yahoo.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message