Retrieving a DataSource


General J2EE: Retrieving a DataSource

  1. Retrieving a DataSource (1 messages)


    I cannot get the DataSource that I configured in my app-server (Sun RI) when invoqued from a component running in another container (such as Tomcat).

    The code executed comes this way:

      public class MyServlet extends HttpServlet {

        Connection cnx=null;


        public connect() {
          try {
            InitialContext ic = new InitialContext();
            DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/dsMyConnection");
            cnx = ds.getConnection();
          } catch (Exception e) { e.printStackTrace(); }

        // ...

    I get this exception message in the stack trace:

      javax.naming.NamingException: java:comp/env namespace is only available from within a J2EE component
        at javax.naming.InitialContext.lookup(

    I don't experience any problem when I run this from the same component deployed on the apps-sever. It seems that I need something to locate the namespace that let's me lookup the DataSource. What's the correct way?

    Really thankfull:

    J. Carlos Muro
  2. Retrieving a DataSource[ Go to top ]

    Thats means your server does not recognize the JNDI name you specify, Tomcat itself does not run any EJB container stuff.