9 Replies Latest reply on Sep 6, 2007 10:25 AM by waynebaylor

    Why is my jdbc/database  not in the local ENC?

    deanouk

      Hi all.

      Just moving to EJB 3 and having a hard time working out why the usual database resource isn't bound to my local ENC within the EJB layer, i.e. java:comp/env/maindatabase and only bound globally i.e. java:/maindatabase

      How do I go about rectifying this?

        • 1. Re: Why is my jdbc/database  not in the local ENC?
          waynebaylor

          Use the ejb-jar.xml to specify what is in your ENC and jboss.xml to map it to the JNDI name.

          For example, to have access to the default datasource, java:/DefaultDS, you would use the following:

          <ejb-jar ...>
           <enterprise-beans>
           <session>
           <ejb-name>MySessionBean</ejb-name>
           <local>my.local.MySessionLocal</local>
           <ejb-class>my.bean.MySessionBean</ejb-class>
          
           <resource-ref>
           <res-ref-name>jdbc/MyDataSource</res-ref-name>
           <res-type>javax.sql.DataSource</res-type>
           <res-auth>Container</res-auth>
           </resource-ref>
           </session>
           </enterprise-beans>
          </ejb-jar>
          

          and
          <jboss ...>
           <enterprise-beans>
           <session>
           <ejb-name>MySessionBean</ejb-name>
           <resource-ref>
           <res-ref-name>jdbc/MyDataSource</res-ref-name>
           <resource-name>DefaultDS</resource-name>
           </resource-ref>
           </session>
           </enterprise-beans>
          </jboss>
          


          and perform the lookup within MySessionBean as:
          DataSource ds = (DataSource)new InitialContext().lookup("java:comp/env/jdbc/MyDataSource");


          • 2. Re: Why is my jdbc/database  not in the local ENC?
            deanouk

            This is certainly how I'd do it in 2.1, but is it not simpler in 3?

            What is the benefit to putting the references into the local ENC anyway - all the EJB 3 examples always seem to use the global look up.

            Cheers.

            • 3. Re: Why is my jdbc/database  not in the local ENC?
              waynebaylor

              the ejb3 way is to use

              @Resource(mappedName="DefaultDS")
              private DataSource ds;


              • 4. Re: Why is my jdbc/database  not in the local ENC?
                deanouk

                Indeed, but is the ref then within the local ENC?
                And what if u want to use the ref in a pojo - doesnt Jboss 4.2.0 not allow you to use the resource ref in anything other than an EJB?

                • 5. Re: Why is my jdbc/database  not in the local ENC?
                  waynebaylor

                  the mappedName refers to java:/DefaultDS, which is not in the bean's ENC.

                  nothing is put in the ENC automatically, if you MUST have it in the ENC then you will need to add it with deployment descriptors or with @EJB at the class level.

                  • 6. Re: Why is my jdbc/database  not in the local ENC?
                    deanouk

                    Thanks for your replies thus far.

                    What I've never understood is the benefit to having it in the local ENC? Why is this generally done?

                    • 7. Re: Why is my jdbc/database  not in the local ENC?
                      waynebaylor

                      by using the ENC you're not coupling you code to the app. server (there are no JBoss specific JNDI names). you will have to include an app. server specific dep. desc. (jboss.xml), but once you're code is compiled you're good to go as far as portability. if you wanted to change to Glassfish, for example, you would just replace the jboss.xml with an equivalent Glassfish dep. desc..

                      • 8. Re: Why is my jdbc/database  not in the local ENC?
                        deanouk

                        I'm still not satisifed I know why there is a global directory and a local/ENC, and why you should use the latter?

                        • 9. Re: Why is my jdbc/database  not in the local ENC?
                          waynebaylor

                          global JNDI names are app. server specific--the default global name for a bean deployed to Glassfish will not be the same as if it were deployed to JBoss.

                          if you don't care about hard-coding JBoss specific names into your classes, then there's no need to worry about it.