Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: JNDI Lookup Problems on Tomcat 5.5.9

  1. #1
    Join Date
    Aug 2006
    Posts
    15

    Default JNDI Lookup Problems on Tomcat 5.5.9

    Hello,

    I am using Spring 1.2 with Hibernate 3.0.5. My production environment is WebLogic 8.1 SP 5, but for development I'd like to use Tomcat 5.5.9 because it's so much faster to load than WebLogic.

    I'm having trouble with the JNDI lookup when deploying the application to Tomcat. I've scoured the web and it looks like I'm not the only one to have this problem, but I haven't been able to find a fix that works. I'll include a lot of info here in the hopes that somebody can give me a hand. The deployment works in WebLogic, so I know I'm close.

    ============================================
    Here's how I've defined the transaction beans in applicationContext.xml:
    ============================================


    <bean id="springJtaTransactionManager"
    class="org.springframework.jdbc.datasource.DataSou rceTransactionManager" />

    <!-- transaction manager -->
    <bean id="jtaTransactionInterceptor"
    class="org.springframework.transaction.interceptor .TransactionInterceptor">
    <property name="transactionManager">
    <ref local="springJtaTransactionManager" />
    </property>

    <property name="transactionAttributes">
    <props>
    <prop key="search*">PROPAGATION_REQUIRED,readOnly,-com.biperf.core.exception.ServiceErrorExceptionWit hRollback,timeout_3000</prop>
    <prop key="is*">PROPAGATION_REQUIRED,readOnly,-com.biperf.core.exception.ServiceErrorExceptionWit hRollback,timeout_3000</prop>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly,-com.biperf.core.exception.ServiceErrorExceptionWit hRollback,timeout_3000</prop>
    <prop key="report*">PROPAGATION_REQUIRED,-com.biperf.core.exception.ServiceErrorExceptionWit hRollback,timeout_10000</prop>
    <prop key="insertPromotionEligibilityCriteriaAudienceRec ords*">PROPAGATION_REQUIRED,-com.biperf.core.exception.ServiceErrorExceptionWit hRollback,timeout_10000</prop>
    <prop key="*">PROPAGATION_REQUIRED,-com.biperf.core.exception.ServiceErrorExceptionWit hRollback,timeout_3000</prop>
    </props>
    </property>
    </bean>


    ============================================
    Here's how I have defined the datasource lookup. Notice that I'm using the JndiFactoryObjectBean:
    ============================================
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName"><value>jdbc/datasource-travelsystem</value></property>
    </bean>



    You can see that the JNDI name is jdbc/datasource-travelsystem. Remember that this works in WebLogic.

    ============================================
    This is the code I have in the WAR file's META-INF/context.xml file that should set up the datasource in Tomcat. I've verified that it exists in Tomcat:
    ============================================
    <Context path="/tosched">
    <Resource name="jdbc/datasource-travelsystem" auth="Container"
    type="javax.sql.DataSource" username="bitravel" password="l3v9rt1b"
    driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521:travel"
    maxActive="8" maxIdle="4"/>

    I know that the datasource is available because if I use this code in a test servlet:

    Context initContext = new InitialContext();
    Context envContext = (Context)initContext.lookup("java:/comp/env");
    DataSource ds = (DataSource)envContext.lookup("jdbc/datasource-travelsystem");
    Connection conn = ds.getConnection();


    It doesn't fail.


    When I deploy the app to Tomcat 5.5.9, it fails with this error:

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'dataSource' defined in class path resource [datasourceContext-server.xml]: Initialization of bean failed; nested exception is javax.naming.NameNotFoundException: Name jdbc is not bound in this Context


    With a stack trace of (abbreviated):

    javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
    at org.apache.naming.NamingContext.lookup(NamingConte xt.java:769)
    at org.apache.naming.NamingContext.lookup(NamingConte xt.java:152)
    at org.apache.naming.SelectorContext.lookup(SelectorC ontext.java:136)
    at javax.naming.InitialContext.lookup(InitialContext. java:351)
    at org.springframework.jndi.JndiTemplate$1.doInContex t(JndiTemplate.java:123)
    at org.springframework.jndi.JndiTemplate.execute(Jndi Template.java:85)
    at org.springframework.jndi.JndiTemplate.lookup(JndiT emplate.java:121)
    at org.springframework.jndi.JndiLocatorSupport.lookup (JndiLocatorSupport.java:71)
    at org.springframework.jndi.JndiObjectLocator.lookup( JndiObjectLocator.java:85)
    at org.springframework.jndi.JndiObjectFactoryBean.aft erPropertiesSet(JndiObjectFactoryBean.java:124)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeInitMethods(Abstr actAutowireCapableBeanFactory.java:937)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:334)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:222)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:146)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:271)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:310)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:80)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:65)
    at com.biperf.core.utils.ApplicationContextFactory.ge tApplicationContext(ApplicationContextFactory.java :113)



    Now, some suggestions on the web have said to include java:comp/env/ somewhere, which I've tried in various places but to no avail. The error message may change, but it still doesn't work.

    I've got to believe that somebody has Spring working this way on Tomcat. Can anybody give me some guidance? Since it works on WebLogic I know it's close, I just can't figure out what I need to tweak for Tomcat.

    Thank you very much!!!!

    Ryan Asleson
    Co-Author, "Foundations of Ajax" and "Pro Ajax and Java Frameworks"
    Lead Developer, Taconite (http://taconite.sf.net/)

  2. #2
    Join Date
    May 2006
    Location
    Crawley, UK
    Posts
    105

    Default

    Ryan,

    The format of the jndiName value is different for Tomcat - instead of 'jdbc/datasource-travelsystem', you need to use 'java:comp/env/jdbc/datasource-travelsystem'

    As you development environment (Tomcat) differs from your production environment (Weblogic), you'll have to externalise that part of the configuration.

    See this thread for more information...

    http://forum.springframework.org/showthread.php?t=27956

    Bob
    If you didn't learn anything today, you weren't paying attention!

  3. #3
    Join Date
    Aug 2006
    Posts
    15

    Default

    Thanks, Bob, for the reply.

    I change my setup to be this:

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryB ean">
    <property name="jndiName"><value>java:comp/env/jdbc/datasource-travelsystem</value></property>
    </bean>


    But now this is the error it's throwing:

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'springJtaTransactionManager' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: dataSource is required
    java.lang.IllegalArgumentException: dataSource is required
    at org.springframework.jdbc.datasource.DataSourceTran sactionManager.afterPropertiesSet(DataSourceTransa ctionManager.java:141)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeInitMethods(Abstr actAutowireCapableBeanFactory.java:937)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:334)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:222)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:146)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:277)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:310)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:80)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:65)
    at com.biperf.core.utils.ApplicationContextFactory.ge tApplicationContext(ApplicationContextFactory.java :113)


    So the error message has changed but it's still complaining about something. Any idea why or how I should debug this??

    Thank you very much!!!!

    -Ryan

  4. #4
    Join Date
    May 2006
    Location
    Crawley, UK
    Posts
    105

    Default

    Ryan,

    The only difference, in structure, that I can see between your config and mine, is that you've not specified the datasource wiring explicitly on your 'springJtaTransactionManager' bean - maybe the autowiring isn't working for some reason. I've not used autowiring myself, as I like to state things explicitly.

    Give this a try...
    Code:
    <bean id="springJtaTransactionManager"
    class="org.springframework.jdbc.datasource.DataSou rceTransactionManager" >
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>
    If you didn't learn anything today, you weren't paying attention!

  5. #5
    Join Date
    May 2006
    Location
    Crawley, UK
    Posts
    105

    Default

    Just looked at the docs and discovered that autowiring is off by default - did you enable it specifically?

    Set this for the entire context file by changing you <beans> element like this...

    Code:
    <bean default-autowire="byName">
    or for an individual bean...

    Code:
    <bean id="springJtaTransactionManager" autowire="byName"
    class="org.springframework.jdbc.datasource.DataSou rceTransactionManager" />
    Fingers crossed!
    If you didn't learn anything today, you weren't paying attention!

  6. #6
    Join Date
    Aug 2006
    Posts
    15

    Default

    Bob, that seems to have helped. I'm running into other problems now but I think the data source is being picked up in Tomcat. Thank you so much. I'll be sure to ask if I need more help.

    -Ryan

  7. #7
    Join Date
    Aug 2006
    Posts
    15

    Post

    OK, everything is working great now. Bob, thanks for all of your help, I would have been in trouble without your help.

    For the record, let me fill you in on what the whole story was, in case it ever comes up again. As I stated earlier my production envrironment is Weblogic 8.1 SP 5. Because of this the transaction manager used the Weblogic-specific transaction manager in the production environment:

    Code:
    <bean id="springJtaTransactionManager" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager" >
    </bean>

    Note that there is no dataSource property wiring here. The Weblogic transaction manager must automatically pick up the data source somehow.

    When working in the Tomcat 5.5.9 environment, I used the basic transaction manager, which requires that the data source be wired explicitly:

    Code:
        <bean id="springJtaTransactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
            <property name="dataSource">
                <ref bean="dataSource"/>
            </property>
        </bean>
    I use Ant to dynamically set this all up correctly during the build process.

    For the record, Tomcat requires that the datasource have "java:comp/env/" appended to its name:

    Code:
        <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName"><value>java:comp/env/jdbc/datasource-travelsystem</value></property> 
        </bean>
    While Weblogic doesn't need it:


    Code:
        <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName"><value>jdbc/datasource-travelsystem</value></property> 
        </bean>

    And the data source for Tomcat is set up in the WAR file's META-INF/context.xml file:

    Code:
        <Resource name="jdbc/datasource-travelsystem" auth="Container"
        type="javax.sql.DataSource" username="zz1" password="vcre3"
        driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@localhost:1521:travel"
        maxActive="8" maxIdle="4"/>
    Thanks again for the help, and maybe this will help somebody else in the future.

    -Ryan

  8. #8
    Join Date
    May 2006
    Location
    Crawley, UK
    Posts
    105

    Thumbs up

    Ryan,

    Can't comment on Weblogic 'automatically picking up the datasource' - despite one of my previous projects using Weblogic as its production environment, that was deemed 'off-limits' to us mere developers (As was being able to make any kind of sensible design suggestions to the 'architect') but that is, as they say, 'Another story'.

    Anyway - as far as the Tomcat configuration is concerned, I usually put the datasource definition in the 'conf/server.xml' file and merely refer to this from the 'META-INF/context.xml' file, thereby allowing the same WAR file to be deployed to different tomcat servers, each of which is set up to hit a different database...

    '/conf/server.xml'
    Code:
        <GlobalNamingResources>
            .
            .
            .
            <Resource
                name="jdbc/fms"
                type="javax.sql.DataSource"
                password="password"
                driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
                maxIdle="2"
                maxWait="5000"
                username="username"
                url="jdbc:sqlserver://server.domain:1433"
                maxActive="10"/>
            .
            .
            .
        </GlobalNamingResources>

    'META-INF/context.xml'

    Code:
    <Context antiJARLocking="true" path="/fmswebapp">
            .
            .
            .
        <ResourceLink name="jdbc/fms" type="javax.sql.DataSource" global="jdbc/fms"/>
            .
            .
            .
    </Context>
    (For the record - the 'antiJarLocking' attribute solves problems with redeployment where tomcat keeps jar files open, forcing a restart before deployment can take place.

    This situation may not apply in your case, as you use Tomcat/Weblogic, but I've found it a much better way to work as the configuration that is related purely to the deployment environment is completely removed from the 'binary' produced by the build process. All that the binary needs to know is the name of the datasource, from a JNDI point of view, not how or where it's configured.

    Enjoy!
    If you didn't learn anything today, you weren't paying attention!

  9. #9
    Join Date
    Jun 2007
    Posts
    1

    Default javax.naming.NameNotFoundException

    Hi there,

    I'm getting the following exception while running my application:-

    javax.servlet.ServletException: Exception forwarding for name welcome: javax.servlet.ServletException: javax.naming.NameNotFoundException: Name searchds is not bound in this Context
    org.apache.jasper.runtime.PageContextImpl.doHandle PageException(PageContextImpl.java:825)
    ....

    It comes while I'm trying to connect the datasource "serachds" in http://localhost:8080/xxx/ where xxx.war is right there in the Tomcathome\webapps folder.

    In the Tomcat Server Administratio tool page, I do have the following configuration for the Edit Data Source as:-

    JNDI Name: jdbc/searchds
    Data Source URL: jdbc:mysql://localhost:3306/search
    JDBC Driver Class: com.mysql.jdbc.Driver

    And in the resource link:-
    Name: ds/searchds
    Global: jdbc/searchds
    Type: javax.sql.DataSource

    Any help would be great.

    Thanks in advance...

    -Tsunami

  10. #10
    Join Date
    Apr 2008
    Posts
    5

    Thumbs up

    according to
    The format of the jndiName value is different for Tomcat - instead of 'jdbc/datasource-travelsystem', you need to use 'java:comp/env/jdbc/datasource-travelsystem'
    That means if I have a development server using Tomcat and the testing server is using JBoss.
    Does that means I have to modify ApplicationContext.xml everytime for each webapp before porting to testing server?
    Is there an unify setting for both of them?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •