7 Replies Latest reply on Jul 13, 2015 6:24 AM by Un Known

    Resource Injection fails

    Un Known Newbie

      What I am trying to do is this:

       

      public class Sample {

          @Resource(name="jdbc/fs") DataSource fs;

      }

       

      I have configured a datasource with the name "jdbc/fs" and a lookup "java:jboss/datasources/FS", which connects to an Oracle database. To my surprise the default H2 database is being injected.

       

      Changing the code to

       

      public class Sample {

          @Resource(name="jdbc/fs", lookup="java:jboss/datasources/FS") DataSource fs;

      }

       

      Everything works fine; however, I'd love to ship the app for both Glassfish and Wildfly. I'd prefer to bind the name "jdbc/fs" in an app-server specific deployment descriptor; I can't find any resources as to how that should work for Wildfly.

      Any Ideas?

       

      Thanks a lot!

        • 1. Re: Resource Injection fails
          Wolfgang Mayer Master

          Did you try "@Resource(mappedName="?

          For datasources and JMS destinations and connection factories, JBoss uses the @Resource.mappedName() attribute. This attribute corresponds to the global JNDI name of the referenced resource. (JBoss EJB 3.0)

          • 2. Re: Resource Injection fails
            Un Known Newbie

            Well, yes, mappedName didn't make things better, plus it's not known for being portable

            • 3. Re: Resource Injection fails
              Wolfgang Mayer Master

              This is strange indeed. Can you post the WildFly version as well as the start option (standalone/domain) you are using?

              From where did you get the information that the wrong driver is injected?

              Did you verify that the datasource is linked to the correct driver? You should find entries in the log like:

              2015-07-05 12:07:43,524 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 36) WFLYJCA0004: Deploying JDBC-compliant driver class oracle.jdbc.OracleDriver (version 11.2)

              2015-07-05 12:07:43,544 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-8) WFLYJCA0018: Started Driver service with driver-name = oracle

               

              You should also find a hibernate confirmation like

              INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 65) HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect

              • 4. Re: Resource Injection fails
                Un Known Newbie

                WildFly 8.2.0.Final (and 9.0.0.CR1)

                 

                Just to make sure: using the proper java:jboss/datasources/FS lookup I can successfully connect to my Oracle database; and: not having configured the jdbc/fs-name in the datasources section, I cannot deploy my app. And that is weird: Wildfly seems to recognize the resource name and matches this against its resource pool entries, but the runtime resolution resorts to the default database.

                 

                Unfortunately, I can't find an example for a jboss-web.xml fragment that maps jdbc/fs to java:jboss/datasources/FS, I think that would be the best solution...

                 

                Thanks btw :-)

                • 5. Re: Resource Injection fails
                  Eduardo Martins Apprentice

                  Un Known wrote:

                   

                  What I am trying to do is this:

                   

                  public class Sample {

                      @Resource(name="jdbc/fs") DataSource fs;

                  }

                   

                  I have configured a datasource with the name "jdbc/fs" and a lookup "java:jboss/datasources/FS", which connects to an Oracle database. To my surprise the default H2 database is being injected.

                   

                  Changing the code to

                   

                  public class Sample {

                      @Resource(name="jdbc/fs", lookup="java:jboss/datasources/FS") DataSource fs;

                  }

                   

                  Everything works fine; however, I'd love to ship the app for both Glassfish and Wildfly. I'd prefer to bind the name "jdbc/fs" in an app-server specific deployment descriptor; I can't find any resources as to how that should work for Wildfly.

                  Any Ideas?

                   

                  Thanks a lot!

                   

                  @Resource's name element usually has nothing to do with lookup, it's the target name in your scoped java:comp context (jdbc/fs translates to java:comp/jdbc/fs) where the resource will be bound. The source of resource injection is defined by @Resource's lookup element, and whenever is missing you get the default datasource injected. The only exception is when the name entry is already bound, for instance by a XML descriptor. In such case @Resource's name or lookup means the same.

                   

                  Wrt app-server specific deployment descriptor, you can use jboss-app.xml in a EAR, or component specific such as jboss-web.xml and jboss-ejb3.xml, just add a <resource-ref/> there with name "jdbc/fs" and lookup "java:jboss/datasources/FS".

                   

                  Example of jboss-web.xml:

                   

                  <?xml version="1.0" encoding="UTF-8"?>
                  <jboss-web>
                    <resource-ref>
                      <res-ref-name>jdbc/fs</res-ref-name>
                      <jndi-name>java:jboss/datasources/FS</jndi-name>
                    </resource-ref>
                  </jboss-web>
                  
                  

                   

                  Now your resource injection code should work, cause jdbs/fs (or java:comp/jdbs/fs) was bound by that descriptor.

                  • 6. Re: Resource Injection fails
                    Un Known Newbie

                    Thanks, I was looking exactly for this.

                    Can you direct me to the XSD or DTD for jboss-web.xml files?

                    • 7. Re: Resource Injection fails
                      Un Known Newbie

                      Seems not to work :-(

                      I have added another datasource and added another resource-ref entry accordingly - and it defaults to H2 again, for both datasources!

                      Changing the @Resource annotation such that the lookup attribute is specified, everything works as expected.