JDBC JNDI lookup started failing on my application after making it Spring aware.
- JDBC datasource setup on tomcat is correct as the non-spring application is able to connect with same setup.
- Attempted to initiatize JNDIFactoryBean in Application's spring config.
<beans>
<bean id="myDb" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/myDb"/>
<property name="lookupOnStartup" value="true"/>
<property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>
</beans>
- Also attempted adding resource-ref in web.xml of the application.
<resource-ref>
<res-ref-name>jdbc/myDb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>
- Since publishing this thread, also tried 'jee:jndi-lookup' in spring config.xml.
<jee:jndi-lookup expected-type="javax.sql.DataSource" id="myDb" jndi-name="java:comp/env/jdbc/myDb"/>
JDBC JNDI Datasource Setup
CATALINA_HOME/conf/server.xml
<GlobalNamingResources>
......
<Resource auth="Container" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" maxIdle="5" maxTotal="25" name="jdbc/myDb" password="ABDC" type="javax.sql.DataSource" url="jdbc:sqlserver://myServer:1233;databaseName=myDBdbdev" username="myUser"/>
</GlobalNamingResources>
CATALINA_HOME/conf/context.xml
<context>
<ResourceLink global="jdbc/myDb" name="jdbc/myDb" type="javax.sql.DataSource"/>
...
</context>
Error StackTrace
javax.naming.NameNotFoundException: Name [jdbc/myDb] is not bound in this Context. Unable to find [jdbc]. at org.apache.naming.NamingContext.lookup(NamingContext.java:833) at org.apache.naming.NamingContext.lookup(NamingContext.java:174) at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163) at javax.naming.InitialContext.lookup(Unknown Source) at com.abc.myapp.db.DataSourceFactory.createPool(DataSourceFactory.java:126) at com.abc.myapp.db.DataSourceFactory.init(DataSourceFactory.jav
Code for fetching datasource
InitialContext ctx = new InitialContext();
ds = (DataSource)ctx.lookup("jdbc/myDb");
jdbc/myDb
as the jndi name and try settingresourceRef
totrue
. In your namespace sample you have switched theid
andjndi-name
property. – M. Deinumjee:jndi-lookup
is basically a nice wrapper around theJndiTemplate
. – M. Deinumnull
but rather prevent your application from starting. Also why are you doing a manual lookup instead of using the Spring defined one? – M. Deinum