0

I am trying to connect my Microsoft sql server using JNDI method. My code is running as container. The details are given below

Below is my context.xml under META-INF

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/CIBILDB"
          auth="Container"
          type="javax.sql.DataSource"
          validationQuery="SELECT 1"
          validationInterval="30000"
          maxActive="100"
          minIdle="10"
          maxWait="10000"
          initialSize="10"
          jmxEnabled="true"
          username="automationrobot"
          password="Soft2007"
          driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
          url="jdbc:sqlserver://TGSLAP-2154\\SQLEXPRESS:1433"/>
</Context>

and below is my java code

package com.CIBIL.dao;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class InitialiseCIBILDBConnection {

 private static DataSource dataSource;
 private static final String  JNDI_LOOKUP_SERVICE = "java:/comp/env/jdbc/CIBILDB";
 static{
  try {
   Context context = new InitialContext();
   Object lookup = context.lookup(JNDI_LOOKUP_SERVICE);
   if(lookup != null){
    dataSource =(DataSource)lookup;
    
   }else{
    new RuntimeException("JNDI look up issue.");
   }
  } catch (NamingException e) {
   e.printStackTrace();
  }
 }
 public static Connection getConnection(){
  try {
   return dataSource.getConnection();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return null;
 }
 
}

When I use the above code, I am getting error

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

Can some one please help me on how to resolve the issue?

Divakar Ragupathy
  • 191
  • 1
  • 6
  • 20

1 Answers1

0

By looking at the official Tomcat documentation (see the section titled JDBC Data Sources), it seems that you also need to declare the resource in the /WEB-INF/web.xml file like so:

<resource-ref>
  <description>
    Resource reference to a factory for java.sql.Connection
    instances that may be used for talking to a particular
    database that is configured in the <Context>
    configuration for the web application.
  </description>
  <res-ref-name>jdbc/CIBILDB</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>

If that doesn't work, try setting the initial context factory as a system property (taken from this Stack Overflow answer):

System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");

Some other Stack Overflow answers recommend declaring the resource in server.xml as well. The way I understand this, this is optional; it's just a convenient way to declare resources that will be used by multiple web applications on the same server.

Keep in mind that getting the initial context in a static initializer is somewhat risky, especially if you need to set the Context.INITIAL_CONTEXT_FACTORY system property mentioned above. You could try passing the system property as a JVM parameter (or something similar), but it's probably a good idea to move this part of your code to a "normal" (non-static) method. That way, you will have one less thing to worry about when debugging.

Asotos
  • 995
  • 11
  • 14
  • Thanks for the response and sorry to come back late. I have tried all above option except server.xml. Now I am getting error ``javax.naming.NameNotFoundException: Name [java:/comp/env/jdbc/CIBILDB] is not bound in this Context. Unable to find [java:].``. I have changed the JNDI_LOOKUP_SERVICE value to jdbc/CIBILDB but still its failing. It was working earlier but not sure how its failing now. Please help – Divakar Ragupathy Jun 20 '19 at 20:48
  • The issue is not with JNDI. Actually if i build my code and convert into war file then its working fine. Hence I believe the way i ran in debug mode using JNDI connection is incorrect. Can you please help me how to run in debug mode for a web service developed in spring – Divakar Ragupathy Jul 05 '19 at 18:01