5 Replies Latest reply on Feb 4, 2014 8:15 AM by Sergio Samayoa

    How to inject data source in AS independent way?

    Jacek Palka Newbie

      Hi,

      Our application uses a number of data sources. In all examples we have found data source injection in an EJB is done with the @Resource annotation and the 'mappedName' attribute, e.g.:

      @Resource(mappedName="java:jdbc/fndbas_en-US")
      private DataSource dsEn;
      


      But the problem is that the 'mappedName' attribute is application server specific. For example the data source from the example above in GlassFish shouldn't be prefixed with "java"; it should only be the JNDI name alone.

      Is there any way to inject data sources in a way that isn't application server specific?

      Thanks!


        • 1. Re: How to inject data source in AS independent way?
          jaikiran pai Master

          You could use the resource-ref entry:

          ejb-jar.xml:

          <enterprise-beans>
          
           <session>
           <ejb-name>ABC</ejb-name>
          ...
           <resource-ref>
           <res-ref-name>myDataSource</res-ref-name>
           <res-type>javax.sql.DataSource</res-type>
           </resource-ref>
          ...
           </session>
          ...
          
          ...
          

          jboss.xml (for other servers, they would have their own deployment descriptor):

          <enterprise-beans>
          
           <session>
           <ejb-name>ABC</ejb-name>
          ...
           <resource-ref>
           <res-ref-name>myDataSource</res-ref-name>
           <jndi-name>java:DefaultDS</jndi-name>
           </resource-ref>
          ...
           </session>
          ...
          


          EJB code:

          @Resource (name="myDataSource")
          private DataSource myDS;


          • 2. Re: How to inject data source in AS independent way?
            Jacek Palka Newbie

            Thanks for answer, but I'm not sure if it is the right way to go for us.

            As I understand we should create deployment descriptors for all the beans that use our data sources. The problem is that we have thousands of beans using the same data sources and the @Resource injection is done in a super class all our beans inherit from.

            One of the goals with upgrading to EJB3 was to eliminate needs for individual, bean specific deployment descriptors.

            Is it possible to create these mappings globally, once for the entire application?

            • 3. Re: How to inject data source in AS independent way?
              Sergio Samayoa Newbie

              A really old post but hurts me now.

               

              I have a JEE 6 application which happily runs on Glassfish 3.1.x and injection on EJB just ave "jdbc/some-name".

               

              Do I really have to wrote the hated xml configuration or this was addressed in newer JB versions?

               

              Regards.

              • 4. Re: How to inject data source in AS independent way?
                Abhijith Nagarajan Newbie

                What Jaikiran has mentioned is the only way out. MappedName is not portable according to Commons Annotations specification (JSR 250) Section 2.3 javax.annotation.Resource.

                 

                Below is the excerpt

                 

                "The mappedName element is a product specific name that this resource should be mapped to. The name of this resource, as defined by the name element or defaulted, is a name that is local to the application using the resource. Many application servers provide a way to map these local names to names of resources known to the application server. The mapped name could be of any form. Application servers are not required to support any particular form or type of mapped name, nor the ability to use mapped names. The mapped name is product-dependent and often installation-dependent. No use of mapped name is portable."

                • 5. Re: How to inject data source in AS independent way?
                  Sergio Samayoa Newbie

                  Thanks for the lesson but doesn't resolve my problem.

                   

                  Also AFAIK name is not the same as mappedName.

                   

                  Bottom line: Even if JEE promises deploy anywhere without resource portable names we have to use, again, configuration files.

                   

                  Regards.