• Post Reply Bookmark Topic Watch Topic
  • New Topic

javax.naming.NameNotFoundException: Name ["/>] is not bound in this Context.  RSS feed

 
tangara goh
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am experiencing some problem with the datasource connection and I think one of the problems is due to

javax.naming.NameNotFoundException: Name [hi5"/>] is not bound in this Context.

What I have done so far is adding a context.xml:



Still, the error doesn't go away.

At Stackoverflow, someone suggests adding in at the server.xml inside Tomcat as well but I am not very sure if this is the right way cos we are not suppose to touch the server.xml as specified in the Apache document.

Hope someone can tell me the right thing to do to make things work.

Tks.
 
Stephan van Hulst
Saloon Keeper
Posts: 7928
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, can you show us the code where you retrieve the data source from the context?
 
Tim Holloway
Bartender
Posts: 18777
74
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not a proper context.xml. It's a stanza that should be in your webapp's WEB-INF/web.xml file to reference the context.

The context.xml file should look more like this:
 
tangara goh
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:So, can you show us the code where you retrieve the data source from the context?


Well, I do not know that I still have to write code to retrieve the data source from the context.  However, I still have Connection class like this :



Will the above not do ?

Or should I add in something more ?

 
Tim Holloway
Bartender
Posts: 18777
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will not do.

When you use the Tomcat database connection pool resource, Tomcat itself constructs and owns the pool and all the connections in the pool. Your web application does not (and SHOULD not) know the database URL, user ID or password.

To obtain a Connection from that pool, your web application code should do a JNDI lookup on the JNDI directory path "java:comp/env/jdbc/hi5". The object that the lookup returns will be a pool connection ready to be used immediately.

It is important to note, however, that you should explicitly close that Connection as soon as you are done using it, so that it may be returned to the pool for other users to use. If you do not do so, the pool will run dry and future users will not be able to obtain connections.

In a similar vein, do not attempt to save pool connections between HTTP service requests (for example, by storing them in an HttpSession). First, because that violates the previous recommendation, and more importantly, Connections are not serializable objects, so attempting to save them is an object that MUST be serializable (HttpSession) can result in random unpleasant results.
 
tangara goh
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:It will not do.

When you use the Tomcat database connection pool resource, Tomcat itself constructs and owns the pool and all the connections in the pool. Your web application does not (and SHOULD not) know the database URL, user ID or password.

To obtain a Connection from that pool, your web application code should do a JNDI lookup on the JNDI directory path "java:comp/env/jdbc/hi5". The object that the lookup returns will be a pool connection ready to be used immediately.

It is important to note, however, that you should explicitly close that Connection as soon as you are done using it, so that it may be returned to the pool for other users to use. If you do not do so, the pool will run dry and future users will not be able to obtain connections.

In a similar vein, do not attempt to save pool connections between HTTP service requests (for example, by storing them in an HttpSession). First, because that violates the previous recommendation, and more importantly, Connections are not serializable objects, so attempting to save them is an object that MUST be serializable (HttpSession) can result in random unpleasant results.


Hi Tim,

The information such as database URL, user ID or password are all inside a properties file which is in the classpath.

Will that not do?

This webapp consists of JSP, Servlet, JPA and hibernate.

Also, I'd like to know if it is necessary to add a listener in order for the connection to work ?



 
tangara goh
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add in one more error which I hope someone can tell me if this is the cause. Tks.,

Configuration error
java.io.FileNotFoundException: {CATALINA_HOME}\conf\logging.properties (The system cannot find the path specified)
 
Tim Holloway
Bartender
Posts: 18777
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tangara goh wrote:

The information such as database URL, user ID or password are all inside a properties file which is in the classpath.

Will that not do?

This webapp consists of JSP, Servlet, JPA and hibernate.

Also, I'd like to know if it is necessary to add a listener in order for the connection to work ?





No. It will not do. You should be using a Connection pool. The connection pool is defined for the server, not for the webapp, and, in fact, it's perfectly valid for more than one webapp to use a common connection pool.

The URL, userid and password are not needed in the webapp. In fact, that's both a security risk and a complication when testing webapps. The URL, userid and password are defined as part of the pool Resource definition in Tomcat, which is part of the context.xml, conf/Catalina/localhost/context.xml or server.xml file. These are all Tomcat configuration files, although context.xml is an optional part of the WAR known as the "server-dependent deployment descriptor".

If you define the pool properly, getting access to the pool is as simple as a JNDI lookup of that pool in your application code. Nothing else is actually required.
 
tangara goh
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:
tangara goh wrote:

The information such as database URL, user ID or password are all inside a properties file which is in the classpath.

Will that not do?

This webapp consists of JSP, Servlet, JPA and hibernate.

Also, I'd like to know if it is necessary to add a listener in order for the connection to work ?





No. It will not do. You should be using a Connection pool. The connection pool is defined for the server, not for the webapp, and, in fact, it's perfectly valid for more than one webapp to use a common connection pool.

The URL, userid and password are not needed in the webapp. In fact, that's both a security risk and a complication when testing webapps. The URL, userid and password are defined as part of the pool Resource definition in Tomcat, which is part of the context.xml, conf/Catalina/localhost/context.xml or server.xml file. These are all Tomcat configuration files, although context.xml is an optional part of the WAR known as the "server-dependent deployment descriptor".

If you define the pool properly, getting access to the pool is as simple as a JNDI lookup of that pool in your application code. Nothing else is actually required.


Hello Tim,

I have everything set up as per your advice.

However, now I am getting the error below :


Do I need to do the wizard connection at the JPA end ?  Please find attached.

However, when I do the drop down to highlight the database, there is no database shown.


JPA-configure-needed-here-to-ask.jpg
[Thumbnail for JPA-configure-needed-here-to-ask.jpg]
 
Tim Holloway
Bartender
Posts: 18777
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't use IDE/GUI options to define my applications or servers. They keep you from knowing important things about how the system is put together, the "wizards" often don't support advanced features and they break when a new version comes out. And you've never really enjoyed life until a critical production system stops working, you can fix it with a 1-line change, but before you can make the change work, you have to re-install an old OS, re-install its service packs, re-install an old IDE and compiler and re-install their service packs before you can even begin to compile the change. All while the boss keeps asking "is it done yet?" 

Apparently, you are using JPA, but your questions up to now have been related to raw database connections. That's a complication even without a GUI hiding the important functions.

I think you need to go back and do some basic study and probably read some of the Sun/Oracle tutorials. Specifically, on how database connection pools work in a web application environment and on how basic (non-JPA) database apps can obtain and work with pool connections. Then you need to learn how pools are defined for Tomcat using the documentation and examples at http://tomcat.apache.org.

Once you understand those concepts then you are ready to learn how to integrate pool connections into a JPA EntityManager. Unfortunately, I use the Spring Framework to do all this, so we speak very different languages at that point. I can give examples on how to use Spring to invoke JNDI to build EntityManager factory resources and to inject them into DAOs, but as I said earlier, I don't have any useful knowledge about having an IDE do all the work for you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!