2 Replies Latest reply on Mar 24, 2011 6:12 AM by xmedeko

    Dependancy injection with datasources within a webservice

    jensmander

      Hi,

       

      I'am trying to use the @Resouce-Annotation for a datasource in my WebService (wsdl). I guess the datasource is properly configured because the following snippet works without any problems.

       

       

      public String myFunction(String in) throws NamingException {
      
              InitialContext ctx;
      
              ctx = new InitialContext();
              DataSource ds = (DataSource) ctx.lookup("java:/MyDataSource");
      
              // do something with the datasource
              
              return "something";
          }

       

       

      but if I'am trying something like that

       

      @Resource(name="MyDataSource")
      private DataSource ds;
      
      public String myFunction(String in) throws NamingException {
      
              // do something with the datasource ds
              
              return "something";
          }
      
      

       

      I can't even deploy my webservice to the JBoss AS. The message says  that "MyDataSource" couldn't be found ;(

      I hope somebody has a piece of advice for me. Thanks a lot.

       

      Jens

        • 1. Re: Dependancy injection with datasources within a webservice
          jensmander

          No ideas? Still struggling at this point

           

          The postgres-ds.xml looks like that:

           

          <datasources>
            <local-tx-datasource>
              <jndi-name>PostgreDS</jndi-name>
              <connection-url>jdbc:postgresql://x.x.x.x/mydb</connection-url>
              <driver-class>org.postgresql.Driver</driver-class>
              <user-name>myuser</user-name>
              <password>mypass</password>
                  <!-- sql to call when connection is created.  Can be anything, select 1 is valid for PostgreSQL
                  <new-connection-sql>select 1</new-connection-sql>
                  -->
          
                  <!-- sql to call on an existing pooled connection when it is obtained from pool.  Can be anything, select 1 is valid for PostgreSQL
                  <check-valid-connection-sql>select 1</check-valid-connection-sql>
                  -->
          
                <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
                <metadata>
                   <type-mapping>PostgreSQL 8.4</type-mapping>
                </metadata>
            </local-tx-datasource>
          
          </datasources>
          

           

          Do I have to set up any other configuration files?

           

          The output during the deployment looks like that:

           

          08:26:59,359 WARN  [JBossWSResourceResolver] Cannot resolve resource: java:/PostgreDS
          08:26:59,359 INFO  [ResourceInjector] failed to resolve resource java:/PostgreDS

           

          And the Jboss JNDIView shows:

           

          java: Namespace

            +- PostgreDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)

              ....

          • 2. Re: Dependancy injection with datasources within a webservice
            xmedeko

            Hi,

             

            @Resource(mappedName="java:/AdpartnerDS", name="java:/comp/env/AdpartnerDS")

             

            is working for me on JBoss 5.1.0GA. Note: mappedName is the global JNDI name. The name is the local and you can write anything you like, just it must start by "java:/comp/env/". (I think this is a bug.)