Search code examples
oracle-databasespringdatasourcetomcat6jndi

Error upon integrating JNDI with Spring


This was my first attempt at Spring with JNDI but getting the below mentioned exception when trying to create the ApplicationContext like:

ApplicationContext context = new ClassPathXmlApplicationContext("master-job.xml");

The Spring configuration file is as follows:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="/jdbc/Eqpstatus"/>
    <property name="resourceRef" value="true" />
</bean>

<bean id="masterDao" class="com.dao.MasterDao">
    <property name="dataSource" ref="dataSource"/>
</bean>

On Server i have the required resource entry for the JNDI name.

<Resource auth="Container" driverClassName="oracle.jdbc.OracleDriver"
        maxActive="10" maxIdle="2" maxWait="10000" name="jdbc/Eqpstatus"
        password="xxxx" type="javax.sql.DataSource"
        url="jdbc:oracle:thin:@(DESCRIPTION=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=xxxx) (PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=xxxx) (PORT=1521))(CONNECT_DATA=(SERVICE_NAME=xyz)))"
        username="xxx"/>

The error i see is:

javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

Would highly appreciate any inputs on this as am new to Spring-JNDI integration.


Solution

  • If you are using Tomcat and everything is fine with your Tomcat configuration; this should be enough:

    <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/dataSource" />
    

    Where jdbc/dataSource is the name defined in your Tomcat config.

    EDIT:

    My bad, forgot about the jndi.properties; there are two possibilities:

    • either provide a jndi.properties on your classpath or
    • set the values in a in the <jee:jndi-environment/> element of <jee:jndi-lookup /> (see reference)

    The java.naming.factory.initial property needs to be set for sure, to something like org.apache.naming.java.javaURLContextFactory, possibly some other values as well, like:

    java.naming.factory.url.pkgs=org.apache.naming  
    java.naming.factory.url.pkgs.prefixes=org.apache.naming  
    java.naming.provider.url=org.apache.naming  
    

    Also see reference.