4 Replies Latest reply on Nov 14, 2006 4:13 PM by jerrygauth

    JNDI lookup fails with JBoss4.0.4GA Patch1

    msmail315

      Hi,
      I'm trying to do a JNDI lookup on the DataSource (MySQL) under java: namespace in my JSP and the lookup fails. I've configured all the files pertinent to creating a new data source. I see the new datasource bound in the JNDIView (but not in the global namespace).
      Here's the code:

      <%@ page language="java" %>
      <%@ page import="javax.sql.*,javax.naming.*,java.sql.*" %>
      <%
      InitialContext ctxt = new InitialContext();
      DataSource ds = (DataSource) ctxt.lookup("java:/jdbc/MySqlDS");
      Connection con = ds.getConnection();
      if(con != null) out.println("Connection not null");
      else out.println("Connection is null");
      %>

      I wanted to set the <use-java-context>false</use-java-context> in the *.ds file but per MySQL its not a good practice for a production environment. How can I circumvent this issue? I want to able to access the data source in a servlet as well as in a POJO.

      Thanx for your help.
      Mash

        • 1. Re: JNDI lookup fails with JBoss4.0.4GA Patch1
          peterj

          What is the name of you data source in the *-ds.xml file? Is it "MySqlDS" or "jdbc/MySqlDS"? If the former, change the lookup to ctxt.lookup("java:/MySqlDS");

          • 2. Re: JNDI lookup fails with JBoss4.0.4GA Patch1
            msmail315

            The Data Source is set to MySqlDS. Also I changed the lookup to java:/MySqlDS. When I invoke it on a JSP, the data source lookup is returning null. Reading thru' different internet articles, can the data source only be accessed from an EJB or can it be accessed from a POJO, JSP and/or Servlet? If so, how can it be done?

            • 3. Re: JNDI lookup fails with JBoss4.0.4GA Patch1
              peterj

              I quite often access my data sources from servlets or pojos (usually the later unless I am doing something quick and dirty). Since I can access them from a servlet, I should be able to access them from a JSP (which is really a servlet in disguise).

              What you have now (java:/MySqlDS) should work, based on what you have written. What I would try next is just opening "java:", it should be a context, and then listing its contents.

              • 4. Re: JNDI lookup fails with JBoss4.0.4GA Patch1

                Have you tried the lookup without the "/" character? If I define a data source with a JNDI name of "MySource", I can look it up in a servlet using java:MySource.