• Post Reply Bookmark Topic Watch Topic
  • New Topic

Binding objects in JNDI  RSS feed

 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I bind an object in my code, in Tomcat or my app's web.xml do I still have to make an entry?
In my servlet that loads on startup I say:
Context ctx = new InitialContext();
ctx.rebind("jdbc/pool",dbPool);

In my code to access the jndi object I say:
dbp = ( DBPool ) ctx.lookup ("jdbc/pool");
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you wouldn't have to do it all over again. The resources declared in your server.xml are just so you can have Tomcat instantiate the objects and bind them for you (so you don't have to). It just helps you decouple your code from the objects you lookup and use during execution.
Declaring them in your web.xml file is useful to decouple your lookuped objects from ones already bound. But in your case, since you're the one binding them, there'd really be no use to use a resource-ref anyways. So, you can do exactly what you're doing and you'll be fine.
 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wel this is very strange. I have gotten this to work before, but now I get the error: javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
I can tell from my print stack and print statements that it is happens on this statement:
ctx.rebind("jdbc/pool",dbPool);
I do nto understand.
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
at org.apache.naming.NamingContext.bind(NamingContext.java:882)
at org.apache.naming.NamingContext.rebind(NamingContext.java:249)
at org.apache.naming.NamingContext.rebind(NamingContext.java:264)
at org.apache.naming.SelectorContext.rebind(SelectorContext.java:253)
at javax.naming.InitialContext.rebind(InitialContext.java:363)
at com.fedex.bit.act.servlets.Bootstrap.init(Unknown Source)
at javax.servlet.GenericServlet.init(GenericServlet.java:256)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.
java:935)
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Will that work? I, ahem ... haven't ever done this before.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!