6 Replies Latest reply on Jun 8, 2004 6:12 PM by Ovidiu Feodorov

    [Newbie]: DataSource not binding to java:comp/env

    dHiNa Newbie

      Hi:

      I'm posting this message after googling for about 2 days and was not able to find a solution.

      I'm trying to access a Postgres Database through JNDI in JBoss 3.2.3.
      My problem is, the Data Source I'm using is getting bound to "java:" instead of "java:comp/env" ('am using the default Naming Service that comes with the JBoss package)

      SO, when I try to access from the client, I'm forced to lookup like "java:/jdbc/defaultDS" as against "java:comp/env/jdbc/defaultDS"
      where "jdbc/defaultDS" is my JNDI Name specified in ps-ds.xml in the deploy directory.

      I'm kind of struck in this and any help will be greatly appreciated.


      Thanks,
      Pani

        • 1. Re: [Newbie]: DataSource not binding to java:comp/env
          Ovidiu Feodorov Master

          Your component should look up the resouce factory in its Enterprise Naming Context ("java:comp/env") using a resource reference. For that:

          1. Declare a resouce reference in ejb-jar.xml:

          <session>
           <ejb-name>MySession</ejb-name>
           ...
           <resource-ref>
           <res-ref-name>jdbc/PostgresDS</res-ref-name>
           <res-type>javax.sql.DataSource</res-type>
           <res-auth>Container</res-auth>
           <res-sharing-scope>Sharable</res-sharing-scope>
           </resource-ref>
          </session>
          


          2. Declare the mapping to the actual JNDI name in jboss.xml:
          <session>
           <ejb-name>MySession</ejb-name>
           ...
           <resource-ref>
           <res-ref-name>jdbc/PostgresDS</res-ref-name>
           <jndi-name>jdbc/PostgresDS</jndi-name>
           </resource-ref>
          </session>
          


          3. Look it up:
          Context initCtx = new InitialContext();
          Context compEnv = (Context)initCtx.lookup("java:comp/env");
          DataSource ds = (DataSource)initCtx.lookup("jdbc/PostgresDS");
          




          • 2. Re: [Newbie]: DataSource not binding to java:comp/env
            Lennart Petersson Novice

            Hm... looks to me that you should have a closer look on the last lookup - shouldn't compEnv be used instead ;)


            Context initCtx = new InitialContext();
            Context compEnv = (Context)initCtx.lookup("java:comp/env");
            DataSource ds = (DataSource)initCtx.lookup("jdbc/PostgresDS");


            /L

            • 3. Re: [Newbie]: DataSource not binding to java:comp/env
              dHiNa Newbie

              Thanks for the reply.

              I understand that I can define a resource reference and look-up for the resource reference in the ENC and access an object that's tied to that Context.

              Also, my understanding is that ENC are declared in the client side, meaning from the place where you want to access the server side objects tied in a NamingService via JNDI.

              Now, my question is; In the NamingService, I want to BIND the DataSource Object to the name "jdbc/PostgresDS" in the context "java:comp" rather than "java:"

              So, when I access it then I can define a resource-ref in the jboss-xml, so that the client would get a reference from "env".
              OR
              I can access it directly by looking up for "java:comp/jdbc/PostgresDS"

              After doing all the reading about JBoss NS Architecture, I concluded it is not possible in 3.2.3. Please correct me if 'am wrong.

              Thanks,
              Pani

              • 4. Re: [Newbie]: DataSource not binding to java:comp/env
                Ovidiu Feodorov Master

                Yes, obviously step 3 should be:

                Context initCtx = new InitialContext();
                Context compEnv = (Context)initCtx.lookup("java:comp/env");
                DataSource ds = compEnv.lookup("jdbc/MyAccessToPostgresDS");
                

                It wouldn't make sense to look up compEnv and not use it. I type faster than I think :).

                As for the last reply on the topic: ENC is private to each application component. An application component environment is a local environment that is accessible only by the component when the container's thread of control is interacting with it. Bean2 cannot access "java:comp" of Bean1 and viceversa. You cannot externally bind something into a component's "java:comp". You cannot access a component's ENC from client-side, you can only access it from inside the component, while the component is deployed in the application server.

                JBossNS has three levels of naming scope:
                - "java:comp" - can be accessed only by the component.
                - "java:" - can be accessed only from the application server VM.
                - and everything else.






                • 5. Re: [Newbie]: DataSource not binding to java:comp/env
                  dHiNa Newbie

                   

                  As for the last reply on the topic: ENC is private to each application component. An application component environment is a local environment that is accessible only by the component when the container's thread of control is interacting with it. Bean2 cannot access "java:comp" of Bean1 and viceversa. You cannot externally bind something into a component's "java:comp". You cannot access a component's ENC from client-side, you can only access it from inside the component, while the component is deployed in the application server.


                  Thanks for the response.

                  I'm not using any EJB component as of now. Just got my Web Component and trying to access the Data Source. So, does this mean it is not possible to bind the datasource to java:comp to be able to access from my web component?


                  • 6. Re: [Newbie]: DataSource not binding to java:comp/env
                    Ovidiu Feodorov Master

                    It is similar for web applications. Use resource-ref.