John C
John C

Reputation: 1835

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.

Upvotes: 3

Views: 4759

Answers (2)

abalogh
abalogh

Reputation: 8281

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.

Upvotes: 2

Tristan
Tristan

Reputation: 9141

First, I think you should use the dedicated tag instead of declaring a "JndiObjectFactoryBean" bean :

<!-- JNDI DataSource for J2EE environments -->         
<jee:jndi-lookup id="dataSource" jndi-name="java:jdbc/rppsDS-PUB-PROTO" default-ref="localDataSource" />

<!-- local dataSource for JUnit integration tests -->
<bean id="localDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="maxActive" value="100"/>
    <property name="maxWait" value="1000"/>
    <property name="poolPreparedStatements" value="true"/>
    <property name="defaultAutoCommit" value="true"/>
</bean>

Then, you need a jndi.properties file (which could be directly in application server dir such as JBoss) with a content similar to :

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099

Upvotes: 3

Related Questions