Tomcat: javax.naming.NoInitialContextException

耗尽温柔 提交于 2019-12-05 19:44:19

Hi progNewbie,

I am working on Windows OS and Eclipse IDE as specially on same type of project for which are you looking.

you need to update your code at following places(3-Changes)...

1. Do Following Java Changes,

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
......
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/Database");
conn = ds.getConnection();

2. Configure Java Web-Application's web.xml likewise,

......
  <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/Database</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
......... 

3. Into Tomcat's server.xml file do following changes :

..................
<!-- Assume my application runs on localhost... -->
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">

<Valve className=..../>

<Context docBase="...." path="..." reloadable="true" source=".....">

<!-- Assume i am using Oracle Database so that driverClass(below) Attribute's Value will be oracle.jdbc.driver.OracleDriver -->
<Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver" 
    maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/Database" password="db-password" 
    type="javax.sql.DataSource" url="db-connection-url" username="db-user-name" 
/>

</Context>

</Host>
............

EDITED :

Let me know if still stuck up then.

Your configuration in web.xml and context.xml is OK. There is no need to reproduce it by code filling a HashTable and so.

I have tested similar experiments successfully. The problem might be as simple as there is an slash too much in your JNDI url: Try this:

DataSource ds = (DataSource) envContext.lookup( "java:comp/env/jdbc/Database" ); 
SkyWalker

UPDATE:

What is initialContextFactory and what it does?

Initial context factory

The constant that holds the name of the environment property for specifying the initial context factory to use. The value of the property should be the fully qualified class name of the factory class that will create an initial context. This property may be specified in the environment parameter passed to the initial context constructor, an applet parameter, a system property, or an application resource file.

If it is not specified in any of these sources, NoInitialContextException is thrown when an initial context is required to complete an operation.

The value of this constant is "java.naming.factory.initial".

For more details: you can follow this tutorial.


Server Admin Working Area:

When server admin creates a data source, he associates it with a Java Database Connectivity (JDBC) provider that is configured for access to a specific vendor database. The application server requires both objects for your applications to make calls to that particular database and receive data from it. The data source provides connection management capabilities that physically make possible these exchanges between your applications and the database.

The server has to be restarted for the newly-created datasource to be available for a Java Naming and Directory Interface (JNDI) from the application.

In server configuration, there is a option named jndi name. Server admin gives input Java Naming and Directory Interface (JNDI) name in the JNDI name field. The application server uses the JNDI name to bind resource references for an application to this data source.

Caution:

  1. Do not assign duplicate JNDI names across different resource types, such as data sources versus J2C connection factories or JMS connection factories.
  2. Do not assign duplicate JNDI names for multiple resources of the same type in the same scope.

Resource Link:

Configuring a data source using the administrative console


Developer working area:

There three steps to configure and run JNDI Datasource Connection pool for any Java Web application:

  • Configure data source in Server and create JNDI name.
  • Configure web.xml
  • Configure your bean with JNDI Datasource
  • Include JDBC driver library on Server lib

Database wise configuration and code is given here.

Resource Link:

  1. JNDI Resources HOW-TO in Apche Tomcat 7
  2. How to configure MySQL DataSource in Tomcat 6


As Mr. @BalusC said that,

Exception strongly suggests that JNDI is disabled on Tomcat instance.

So for enabling JNDI, you can get many tutorials.

A more detailed explanation of what is going on in enabling JNDI, complete with an example, can be found here.

For more you can go through this tutorial also.

Resource Link:

  1. Tomcat DataSource JNDI Example
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!