3

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.

2 Answers 2

3

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 
Sign up to request clarification or add additional context in comments.

6 Comments

Am using Tomcat 6 as web server and i have added the entry on server.xml above which includes all these configuration that you have in localDataSource bean.
Well, if you use Tomcat, you don't need these jnp parameters, you just need to define a "Resource" in "GlobalNamingResources". Maybe you're trying to run a JUnit test (which can not use jndi datasource). By the way, Are you sure this datasource is a valid jdbc datasource ? Don't u have errors when u start Tomcat ?
I am not trying to run the application and not Junit tests. The datasource is valid and there are no errors upon starting Tomcat.
You are not trying to run the application, and you are not running JUnit tests, so can you explain what you are trying which gives you errors ?
Sorry, that was a typo. I meant i am trying to run the application and not Junit tests.
|
2

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.

6 Comments

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
After changing the jndiName property to "/jdbc/Eqpstatus" and adding resourceRef property to true, the error i see now is - javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
the jndiName property in which config file? can you edit your question with your current config files? Spring and Tomcat as well
jndiName in the Spring configuration file. Have edited my question.
if you are using Tomcat you need to use java:comp/env/jdbc/Eqpstatus. Do you have jndi.properties? Have you also tried the dedicated jee schema?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.