I think, therefore I blog

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

By , 15 April 2008

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

It seems that Tomcat 5.5 and 6.0 DataSourceRealms don't like referring to JNDI resources declared in the same context. The following configuration gives me a javax.naming.NameNotFoundException:

<Context path="/">
  
  <!-- connection pool for database -->
  <Resource name="jdbc/data" auth="Container" type="javax.sql.DataSource"
            driverClassName="org.postgresql.Driver"
            url="jdbc:postgresql://localhost/test"
            username="postgres" password=""
            maxActive="5" maxIdle="2" maxWait="-1"/>

  <!-- authentication realm -->
  <Realm className="org.apache.catalina.realm.DataSourceRealm"
         dataSourceName="jdbc/data" digest="MD5"
         userTable="web_users" userRoleTable="web_user_roles"
         userNameCol="id" userCredCol="password" roleNameCol="role"/>
</Context>

However, if I move the JNDI resource up it works as expected:

<GlobalNamingResources>

  <!-- connection pool for database -->
  <Resource name="jdbc/data" auth="Container" type="javax.sql.DataSource"
            driverClassName="org.postgresql.Driver"
            url="jdbc:postgresql://localhost/test"
            username="postgres" password=""
            maxActive="5" maxIdle="2" maxWait="-1"/>
</GlobalNamingResources>
...
<Context path="/">
        
  <!-- authentication realm -->
  <Realm className="org.apache.catalina.realm.DataSourceRealm"
         dataSourceName="jdbc/data" digest="MD5"
         userTable="web_users" userRoleTable="web_user_roles"
         userNameCol="id" userCredCol="password" roleNameCol="role"/>
</Context>

Odd...

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

About Roger Keays

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

Roger Keays is an artist, an engineer, and a student of life. Since he left Australia in 2009, he has been living as a digital nomad in over 40 different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, and finding good food. Click here to subscribe to his weekly blog, or stalk him on Facebook and Twitter.

Leave a Comment

Please visit https://RogerKeays.com/blog/javax.naming.namenotfoundexception-name-jdbc-is-not-bound-in-this-context to add your comments.

Comment posted by: Shekhar, 5 years ago

why it showing this type of Message

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

SEVERE: Servlet.service() for servlet action threw exception
java.lang.NoClassDefFoundError: javax/ejb/EJBHome
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1817)
    at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:872)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1325)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at com.jlcindia.struts.delegate.SessionBeanDelegate.validateUser(SessionBeanDelegate.java:25)
    at com.jlcindia.struts.action.LoginAction.execute(LoginAction.java:29)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)

Comment posted by: , 8 years ago

@see tomcat-users:

Roger Keays wrote:
> Is that the intended behaviour?
Yes. If you want a realm to use a context defined datasource rather than a global one you need to set: localDataSource="true" on the realm

It is documented in the howto but not the realm docs. I'll fix that now. http://tomcat.apache.org/tomcat-6.0-doc/realm-howto.html#DataSourceRealm

Mark

grrr...

Join 3,559 People Who Think Outside The Box

I write every Sunday about travel, psychology, technology, and all sorts of interesting stuff. It's completely free, and you can subscribe for as long as you like. Do it now, so you don't miss a single post.

Chat For A While

Your Vote Matters

Which animal will take over when humans go extinct?
Lions
Dogs
Pigs
Octopuses
Cockroaches
Rats
Dolphins
Sharks
Jellyfish
Cows

Read a Good Book

“A spellbinding true story of love, passion and adventure. One can’t help but be swept away by 100% Love Guaranteed.” —Dr Tammie Matson, author of Elephant Dance.