You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Federico Spinazzi <f....@masterhouse.it> on 2003/06/09 17:10:03 UTC

JNDI datasource non available in ServletContextListener.contextInitialized

Hi you all,

I'm trying to build application wide variable from a 
ServletContextListener under Tomcat 4.1.24.
Some of the variables are built with data read from a database.
I've been able to successfully define the JNDI datasources and to list 
them in the contextInitialized method
but I needed to try many possible combinations (see later) to 
succesfully perform the lookup in the contextInitialized call: it seems 
like if Tomcat doesn't make the JNDI completely available during the 
initialization sequence of the web application: infact the following 
behaviour is applicable to an init method of a servlet loaded at start-up.
I would like to share this experience and to ask for help, if I'm doing 
something wrong.

There is only a way I've managed to get the result (to be able to 
estabilish a connection using a DataSource retrieved from a JDNI lookup):
- declare the use of the resource in web.xml
- define the resource in server.xml (this is a little 'limitating' for me)
- deploy the applications in unpacked format (i.e. not from a war file: 
this is quite limiting, however)

I want to list othe options I've tried (sarting from an 'empty' webapps 
directory) without success:
- stop tomcat, copy theApplication.war and theApplication.xml in 
CATALINA_HOME/webapps: theApplication.xml contains the context 
configuration, start tomcat
- deploy theApplication.war with a META-INF/theApplication.xml  in it:
The stack traces look like this:
javax.naming.NamingException: Cannot create resource instance
        at 
org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:189)
        at 
javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:301)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:834)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:181)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:822)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:181)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:822)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:181)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:822)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:194)
        at 
org.apache.naming.SelectorContext.lookup(SelectorContext.java:183)
        at javax.naming.InitialContext.lookup(InitialContext.java:347)
        at 
it.masterhouse.termopoli.web.startup.Init.contextInitialized(Init.java:37)
        at 
org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3269)
        at 
org.apache.catalina.core.StandardContext.start(StandardContext.java:3598)
        at 
org.apache.catalina.core.StandardHostDeployer.start(StandardHostDeployer.java:629)
Any feedback would be appreciate
Thank you very much
Federico Spinazzi


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


Re: [tomcat] RE: JNDI datasource non available in ServletContextListener.contextInitialized

Posted by Federico Spinazzi <f....@masterhouse.it>.
James Lewis wrote:

>>always perform such a lookup successfully, but when I try from a
>>ContextListener contextInitialized method it fails: the same code WILL
>>NOT FAIL contextDestroyed !!!
>>    
>>
>
>Hmmm, odd. This is *exactly* what I do pretty much. I have a
>LifeCycleListener specified in the webapps/<app>/WEB-INF/web.xml like this:
>
>  <listener>
>  	<listener-class><my.foo.bar.package>.LifeCycleListener</listener-class>
>  </listener>
>
>and then in the contextInitialized method of the LifeCycleListener I do a
>bunch of calls to jndiContext.lookup().
>
>Hmm, hmmm, hmmm. You are correctly specifying the listener in web.xml?
>
This is the web.xml entry:

    <listener>
      
<listener-class>it.masterhouse.termopoli.web.startup.Init</listener-class>
    </listener>

    <resource-ref>
      <description>
        JNDI resource reference to a factory for java.sql.Connection
        instances that may be used for talking to Babele
        database (configured in the server.xml file).
      </description>
      <res-ref-name>
        jdbc/Babele
      </res-ref-name>
      <res-type>
        javax.sql.Datasource
      </res-type>
      <res-auth>
        Container
      </res-auth>
    </resource-ref>

and this is the context:

<Context path="/termopoli" docBase="termopoli" debug="0" 
reloadable="true" crossContext="true">

    <Logger className="org.apache.catalina.logger.FileLogger"
            prefix="localhost_termopoli_log."
            suffix=".txt"
            timestamp="true"/>

    <!-- Pool JNDI per DB2/Babele -->
    <Resource name="jdbc/Babele" auth="Container" 
type="javax.sql.DataSource"/>

    <ResourceParams name="jdbc/Babele">
    <parameter>
      <name>username</name>
      <value>foo</value>
    </parameter>
    <parameter>
      <name>password</name>
      <value>foo</value>
    </parameter>
    <parameter>
      <name>driverClassName</name>
      <value>com.ibm.db2.jcc.DB2Driver</value>
    </parameter>
    <parameter>
      <name>url</name>
      <value>jdbc:db2://foo:9999/BABELE</value>
    </parameter>
    <parameter>
      <name>maxActive</name>
      <value>8</value>
    </parameter>
    <parameter>
      <name>maxIdle</name>
      <value>4</value>
    </parameter>
   
    <parameter>
      <name>removeAbandoned</name>
      <value>true</value>
    </parameter>
    <parameter>
      <name>removeAbandonedTimeout</name>
      <value>60</value>
    </parameter>
    <parameter>
      <name>logAbandoned</name>
      <value>true</value>
    </parameter>   
    </ResourceParams>
   

</Context>


I can list the JNDI datasource like this:
INFO : [06/06/2003 11:56:50]: 
it.masterhouse.termopoli.web.startup.Init.printBindings(Init.java:86): 
Babele: 
ResourceRef[className=javax.sql.Datasource,factoryClassLocation=null,factoryClassName=org.apache.naming.factory
.ResourceFactory,{type=description,content=JNDI resource reference to a 
factory for java.sql.Connection
        instances that may be used for talking to Babele
        database (configured in the server.xml 
file).},{type=scope,content=Shareable},{type=auth,content=Conta
iner}]
INFO : [06/06/2003 11:56:50]: 
it.masterhouse.termopoli.web.startup.Init.printBindings(Init.java:88): 
context j
ava:comp/env/jdbc enumerated

but then:

FATAL: [06/06/2003 11:56:50]: 
it.masterhouse.termopoli.web.startup.Init.contextInitialized(Init.java:55): 
Nami
ngException during lookup
javax.naming.NamingException: Cannot create resource instance
        at 
org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:189)
        at 
javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:301)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:834)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:181)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:822)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:181)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:822)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:181)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:822)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:194)
        at 
org.apache.naming.SelectorContext.lookup(SelectorContext.java:183)
        at javax.naming.InitialContext.lookup(InitialContext.java:347)
        at 
it.masterhouse.termopoli.web.startup.Init.contextInitialized(Init.java:37)

I think the problem is that I cannot get the ResourceParameters: look at 
a succesfull call:

INFO : [06/06/2003 14:38:31]: 
it.masterhouse.termopoli.web.startup.Init.printBindings(Init.java:86): 
Babele: R
esourceRef[className=javax.sql.DataSource,factoryClassLocation=null,factoryClassName=org.apache.naming.factory
.ResourceFactory,{type=scope,content=Shareable},{type=auth,content=Container},{type=url,content=jdbc:db2://foo:9999/BABELE},
{type=maxIdle,content=4},{type=maxActive,content=8},{type=driverClassName,content=c
om.ibm.db2.jcc.DB2Driver},{type=removeAbandoned,content=true},{type=username,content=foo},{type=logAbando
ned,content=true},{type=removeAbandonedTimeout,content=60},{type=password,content=foo}]
INFO : [06/06/2003 14:38:31]: 
it.masterhouse.termopoli.web.startup.Init.printBindings(Init.java:88): 
context j
ava:comp/env/jdbc enumerated
INFO : [06/06/2003 14:38:31]: 
it.masterhouse.termopoli.web.startup.Init.contextInitialized(Init.java:40): 
attr
ibute datasource set for context 
org.apache.catalina.core.ApplicationContext@1911540

This is in accordance with the fact that when I tried to deploy the 
application from webapps putting termopoli.xml and termopoli.war in 
CATALINA_HOME\webapps, server.xml was modified to include the <Resource> 
tag but the <ResourceParams> where left out:

        <Context className="org.apache.catalina.core.StandardContext" 
cachingAllowed="true" 
charsetMapperClass="org.apache.catalina.util.CharsetMapper" 
cookies="true" crossContext="false" debug="0" displayName="Termopoli" 
docBase="C:\java\jakarta-tomcat-4.1.24\webapps\termopoli" 
mapperClass="org.apache.catalina.core.StandardContextMapper" 
path="/termopoli" privileged="false" reloadable="false" 
swallowOutput="false" useNaming="true" 
wrapperClass="org.apache.catalina.core.StandardWrapper">
          <Resource auth="Container" description="JNDI resource 
reference to a factory for java.sql.Connection
        instances that may be used for talking to Babele
        database (configured in the server.xml file)." 
name="jdbc/Babele" scope="Shareable" type="javax.sql.Datasource"/>
        </Context>

I'm surely missing some point ...

Thank you very much,
Federico




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


RE: [tomcat] RE: JNDI datasource non available in ServletContextListener.contextInitialized

Posted by James Lewis <ja...@goodtechnology.com>.
> always perform such a lookup successfully, but when I try from a
> ContextListener contextInitialized method it fails: the same code WILL
> NOT FAIL contextDestroyed !!!

Hmmm, odd. This is *exactly* what I do pretty much. I have a
LifeCycleListener specified in the webapps/<app>/WEB-INF/web.xml like this:

  <listener>
  	<listener-class><my.foo.bar.package>.LifeCycleListener</listener-class>
  </listener>

and then in the contextInitialized method of the LifeCycleListener I do a
bunch of calls to jndiContext.lookup().

Hmm, hmmm, hmmm. You are correctly specifying the listener in web.xml?


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


Re: [tomcat] RE: JNDI datasource non available in ServletContextListener.contextInitialized

Posted by Federico Spinazzi <f....@masterhouse.it>.
James Lewis wrote:

>- define the resource in server.xml (this is a little 'limitating' for me)
>  
>
>>- deploy the applications in unpacked format (i.e. not from a war file:
>>this is quite limiting, however)
>>    
>>
>
>Yep, not good to stick them in server.xml.
>
>Your <application>.xml file containing your resources should be in the
>webapps/ directory along with your packed or unpacked application
>
>You can then access your resources using:
><snip> jndiContext.lookup("java:comp/env/<resource name>");
>
>Is this what you are doing? Are you using the correct Context
>(java:comp/env)?
>
Yes, and this is my trouble: when the application is running I can 
always perform such a lookup successfully, but when I try from a 
ContextListener contextInitialized method it fails: the same code WILL 
NOT FAIL contextDestroyed !!!
I experience the same failure from the init method of a servlet
Thanks.
Federico


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


RE: JNDI datasource non available in ServletContextListener.contextInitialized

Posted by James Lewis <ja...@goodtechnology.com>.
- define the resource in server.xml (this is a little 'limitating' for me)
> - deploy the applications in unpacked format (i.e. not from a war file:
> this is quite limiting, however)

Yep, not good to stick them in server.xml.

Your <application>.xml file containing your resources should be in the
webapps/ directory along with your packed or unpacked application

You can then access your resources using:
<snip> jndiContext.lookup("java:comp/env/<resource name>");

Is this what you are doing? Are you using the correct Context
(java:comp/env)?

Cheers,

james


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