11 Replies Latest reply on Sep 3, 2007 5:17 AM by Steve Vangasse

    Extremely basic JNDI question

    Dale Bronk Newbie

      I'm almost ashamed to ask such a basic set of questions, but I'm simply having problems. I'm pretty new to JBoss, been using just Tomcat. What I'm doing is creating a datasource and placing a custom pojo bean into jndi. And, I want these to be only accessible by certain web contexts. In Tomcat, this really could not be any easier. I simply do the following in the server.xml...

      <Context path="/myApp" ... >
      <!-- Datasource -->

      .... all the normal datasource params ...

      <!-- My Bean -->



      Now to use the datasource normal Spring stuff. To get my RPSClient object I need only:

      Context initCtx = new InitialContext();
      Context envCtx = (Context) initCtx.lookup("java:comp/env");
      RPSClient rpsClient = (RPSClient) envCtx.lookup("rps/RPSClient");

      Done!

      Okay, how would I do the same for JBoss? I have successfully got a datasource going in jboss jndi, but it is at application server level, not context level. I've had zero success in placing a simple custom java object (a config object really) in jndi.

      My main issue with the datasource is that after I war up my application it is named MyApp.war. Great, works fine. But after working on the war, we want to deploy it to QA so we simply copy/paste the war file to MyAppQA.war, then MyAppUAT.war, etc. So now we may have several different versions running in the same JBoss instance. Since each are looking for jdbc/TestDB they all get the same database. This is bad. With tomcat since we defined the datasource in the context, we pointed each (same name, but context sensitive) to a different db. done!

      Thanks, and sorry for what I hope to be a very basic and easy thing to do.

        • 1. Re: Extremely basic JNDI question
          Dale Bronk Newbie

          Sorry, I guess some of the xml got removed... here it is again but with some spaces so it does not get removed.

          This is the tomcat, easy, setup...


          < Context path="/myApp" ... >
          < !-- Datasource -->
          < Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource" / >
          .... all the normal datasource params ...

          < !-- My Bean -->
          < Resource name="rps/RPSClient"
          auth="Container"
          type="com.myco.rps.RPSClient"
          factory="org.apache.naming.factory.BeanFactory"
          location="localhost" / >
          < / Context>

          • 2. Re: Extremely basic JNDI question
            Peter Johnson Master

            I guess some questions come in threes, but to access a data source using the ENC, see http://www.jboss.com/index.html?module=bb&op=viewtopic&t=115556

            Not sure about the custom pojo, maybe someone else knows.

            • 3. Re: Extremely basic JNDI question
              Dale Bronk Newbie

              Thanks for the very fast reply. I looked at that thread and not sure that is going to give me what I want. I'm assuming that in the thread you pointed me to there would be a someds-ds.xml in the deploy directory of JBoss. That xml will point to the database. The problem now comes in that since you have a war file with the web.xml and jboss-web.xml in it, you now can not simply copy/paste that same war file from someApp.war to someAppQA.war. Well, I'm sure the app will come up, but it will point to the same someds-ds.xml file, thus pointing to the same database. I want my someds-ds.xml to really be something like someContext-someds-ds.xml. ie: someApp-someds-ds.xml, someAppQA-someds-ds.xml. The contents would be the same, except for the actual database pointing to, uid/pw, etc. But the name would be the same name. This way, I have zero reconfiguring of my application when I promote through the different environments. The code always looks for "java:comp/env/ds/SomeDS", but each will pull from the appropriate someApp-someds-ds.xml.

              This is easy as pie in Tomcat.

              Thanks again.

              • 4. Re: Extremely basic JNDI question
                Steve Vangasse Newbie

                I'm having a very similar problem; multiple web contexts sharing a war file, each using distinct data.

                I solved the problem slightly differently in Tomcat stand alone. Each context is given a unique key via JNDI that it uses to select the relevant rows of a shared database. But I think we're looking for the same thing: a way to set up each web context with it's own InitialContext.

                I've been doing some digging and as far as I can see there doesn't seem to be a way to achieve this in JBoss (I would gladly be proved wrong).

                I'm looking at MBeans at the moment. Would it be possible to configure the JNDI for each web context with MBeans?

                Thanks,

                Steve

                • 5. Re: Extremely basic JNDI question
                  Steve Vangasse Newbie

                  Before I continue looking for a way to initialise a name space for each web context maybe I should be asking whether there is one in the fist place.

                  So does anyone know whether each web context is given it's own JNDI name space?

                  Thanks,

                  Steve

                  • 6. Re: Extremely basic JNDI question
                    wayne baylor Apprentice

                    each web context should have its own ENC. so, for a given WAR file you can define "java:comp/env/..." lookups in its web.xml/jboss-web.xml.

                    • 7. Re: Extremely basic JNDI question
                      Dale Bronk Newbie

                      This unfortunately does not satisfy my needs. I'll go about it in a different way. But, this is extremly easy to do in Tomcat so I thought it would be easy to do in JBoss. But, we have found other issues moving from Tomcat to JBoss with JNDI also. Placing data sources in Tomcat we find them by looking up "java:comp/env/....". Move the same app to JBoss and add a xxxxx-datasource.xml to JBoss, and it will not find it when we lookup "java:comp/env/....". We need to remove the "java:comp/env/" portion of our lookup... not very portable portability.

                      • 8. Re: Extremely basic JNDI question
                        jaikiran pai Master

                         

                        "dbronk" wrote:
                        Placing data sources in Tomcat we find them by looking up "java:comp/env/....". Move the same app to JBoss and add a xxxxx-datasource.xml to JBoss, and it will not find it when we lookup "java:comp/env/....". We need to remove the "java:comp/env/" portion of our lookup... not very portable portability.


                        That's not correct. You can have the datasources available in the java:comp/env/ namespace. All you have to do is, add a resource-ref entry in your web.xml and jboss-web.xml files. Let us know if you want more details on this.

                        • 9. Re: Extremely basic JNDI question
                          Steve Vangasse Newbie

                          waynebaylor wrote:

                          each web context should have its own ENC. so, for a given WAR file you can define "java:comp/env/..." lookups in its web.xml/jboss-web.xml.


                          That's true. But if each war file is being shared by multiple web contexts then they all share the same ENC. Is there a way to set up each web context with it's own unique ENC?

                          Ideally this would not be inside the war file (like jboss-web.xml is) so that it can be edited easily.

                          Thanks,

                          Steve

                          • 10. Re: Extremely basic JNDI question
                            wayne baylor Apprentice

                            not that i know of :(

                            • 11. Re: Extremely basic JNDI question
                              Steve Vangasse Newbie

                              Thanks Wayne.

                              Do you know whether a web contexts ENC can be set up dynamically with MBeans under JBoss. I know that Tomcat has pretty good MBeans support and in stand alone mode this can be done, but as far as I know JBoss handles Tomcat's JNDI when it's embedded.

                              Any thoughts?

                              Steve