1 Reply Latest reply on Jun 2, 2004 9:57 AM by nischalsharma

    javax.naming.NoInitialContextException error while creating

    nischalsharma

      Hi

      friends

      I have created class file for the sequence key that use the datasource

      through a function given in file named PSDBConn.java
      :ie

      public Connection getConn() throws Throwable
      {
      Context initContext = new InitialContext();
      Context envContext = (Context)initContext.lookup("java:/comp/env");
      DataSource ds = (DataSource)envContext.lookup("jdbc/PSESPDS");

      return ds.getConnection();
      }




      error message given below while executing the SequenceGen.java:
      ---------------------------------
      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

      at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:640)

      at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)

      at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280)

      at javax.naming.InitialContext.lookup(InitialContext.java:347)

      at com.hill_assoc.esp.PSDBConn.getConn(PSDBConn.java:25)

      at com.hill_assoc.esp.SequenceGen.getConnection(SequenceGen.java:60)

      at com.hill_assoc.esp.SequenceGen.nextVal(SequenceGen.java:30)




      ----------------------------------------------------------------
      SequenceGen.java


      package com.hill_assoc.esp;


      import java.sql.Connection;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import javax.naming.Context;
      import javax.servlet.ServletContext;
      import javax.servlet.http.HttpServlet;
      import javax.sql.DataSource;




      public class SequenceGen
      {
      private static Context jndiCtx ;
      private static DataSource ds ;


      public static synchronized long nextVal(String sSeqName) throws Exception{

      long nextVal = -1 ;
      Connection conn = null ;
      PreparedStatement pst = null ;
      ResultSet rs = null ;

      try{
      conn = getConnection();
      pst = conn.prepareStatement("Select " + sSeqName + ".nextval from DUAL");
      rs = pst.executeQuery();
      if(rs.next()) nextVal = rs.getLong(1);
      }
      catch(Throwable ex) {
      ex.printStackTrace() ;
      }

      finally {
      try{
      if(rs != null) rs.close();
      if(pst != null) pst.close() ;
      freeConnection(conn);
      }catch(SQLException ex) {
      ex.printStackTrace() ;
      }
      }
      return nextVal ;
      }


      private static Connection getConnection() throws Exception
      {
      Connection conn = null ;


      try{

      PSDBConn con = new PSDBConn();
      conn = con.getConn();
      System.out.println("Sequence.getConnection() called ");
      }
      catch(Throwable ex) { ex.printStackTrace() ; }
      return conn ;
      }


      private static void freeConnection(Connection conn)
      {
      try{
      if(conn != null) conn.close();
      }
      catch(SQLException ex) {
      ex.printStackTrace() ;
      }
      }

      public static void main(String[] args) throws Exception {
      try{
      System.out.println("nextval 1 : " + SequenceGen.nextVal("SEQ_LANG"));

      }catch(Throwable e){
      e.printStackTrace();
      }
      }
      }
      ---------------------------------------------------

      PSDBConn.java


      package com.hill_assoc.esp;

      import java.sql.CallableStatement;
      import java.sql.Connection;
      import java.sql.PreparedStatement;
      import java.sql.SQLException;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.servlet.ServletContext;
      import javax.sql.DataSource;

      public class PSDBConn {

      public PSDBConn(ServletContext sc) throws Throwable
      {
      }

      public PSDBConn() throws Throwable
      {
      }

      public Connection getConn() throws Throwable
      {
      Context initContext = new InitialContext();
      Context envContext = (Context)initContext.lookup("java:/comp/env");
      DataSource ds = (DataSource)envContext.lookup("jdbc/PSESPDS");

      return ds.getConnection();
      }

      public void close(Connection con) throws Throwable
      {
      try {
      con.close();
      }
      catch (SQLException e)
      {
      throw new PostError("Database Disconnection error!", e).push();
      }
      }

      //close the preparedStatement
      public void closeStatement(PreparedStatement stmt) throws Throwable
      {
      try{
      if(stmt!=null)
      stmt.close();
      }catch(Exception e){
      throw new PostError("closing statement !",e).push();
      }
      }

      public void closeStatement(CallableStatement stmt) throws Throwable
      {
      try {
      if(stmt!=null)
      stmt.close();
      } catch(Exception e){
      throw new PostError("closing statement !",e).push();
      }
      }


      }


      jboss-web.xml
      ----------------



      <?xml version="1.0" encoding="UTF-8"?>
      <jboss-web>
      <resource-ref>
      <res-ref-name>jdbc/ESPDS</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <jndi-name>java:/ESPDS</jndi-name>
      </resource-ref>
      <resource-ref>
      <res-ref-name>jdbc/PSESPDS</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <jndi-name>java:/PSESPDS</jndi-name>
      </resource-ref>
      </jboss-web>


      if any one know that even after specifying datasource in jboss-web.xml
      why i am getting jndi error.

      OR

      any other better why to generate Oracle sequence for Pk


      with regards
      nischal



        • 1. Re: javax.naming.NoInitialContextException error while creat
          nischalsharma


          but in the jmx-console the datasource look like this:


          java: Namespace
          +- XAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)
          +- DefaultDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)
          +- SecurityProxyFactory (class: org.jboss.security.SubjectSecurityProxyFactory)
          +- DefaultJMSProvider (class: org.jboss.jms.jndi.JBossMQProvider)
          +- PSESPDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)
          +- ESPDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)
          +- comp (class: javax.naming.Context)
          +-

          may be i have specified wrong paramter in lookup().