5 Replies Latest reply on Oct 17, 2013 10:24 AM by Vikas B

    weblogic 12c resource-ref not working

    Steve Favez Newbie

      I'm trying to test the following scenario using weblogic 12c in remote :

       

      An ejb 3.1 jar project, with a simple stateless session bean injecting a datasource using @Resource annotation.

      I've a Datasource on my weblogic 12c with a jndi name "jdbc/myDS".

      I also have a weblogic-ejb-jar.xml file declaring an ressource ref for my ejb  ( name is "mydatasource" ), to create a ressource ref on my global datasource, and the same for a "test" web app (weblogic.xml)

      When starting my test with Arquillian, if I use the global jndi name (jdbc/myDS) to inject my datasource, it's working fine. But If I use the resource-ref name ("mydatasource"), datasource can't be found.

       

      Here's the code of my arquillian class :

       

      @RunWith(Arquillian.class)

      public class TestUserPasswordService {

       

          @Resource(name = "mydatasource")

          DataSource ressourceRefDs;

       

          @Resource(name = "jdbc/myDS")

          DataSource directJdniNameDS;

       

       

          @Deployment

          public static WebArchive createDeployment() {

                JavaArchive ejb = ShrinkWrap.create(JavaArchive.class, "testUserPasswordServices.jar")

                          .addClass(UserPasswordService.class).addClass(BusinessException.class)

                          .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

                final WebArchive war = ShrinkWrap.create(WebArchive.class, "mytest.war").addAsLibrary(ejb)

                          .addAsWebInfResource("weblogic.xml").addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");

                return war;

          }

       

      and my weblogic.xml file :


      <?xml version="1.0" encoding="UTF-8"?>

      <wls:weblogic-web-app xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.3/weblogic-web-app.xsd">

          <wls:resource-description>

              <wls:res-ref-name>mydatasource</wls:res-ref-name>

              <wls:jndi-name>jdbc/myDS</wls:jndi-name>

          </wls:resource-description>

      </wls:weblogic-web-app>


       

       

          @Test

          /**

           * test if datasource has been properly injected.

           */

          public void testDataSourceInjection() {

                Assert.assertNotNull(directJdniNameDS);

                Assert.assertNotNull(ressourceRefDs);

          }

      }

       

      What's wrong with my test ? any Idea ?

        • 1. Re: weblogic 12c resource-ref not working
          Vikas B Newbie

          I am getting the same problem.. Does anyone know if this is fixed in 12.1.2. ?

          • 2. Re: weblogic 12c resource-ref not working
            Robert Panzer Newbie

            Hi,

             

            maybe you cut that out, but your snippet does not show that you are actually adding the test class to the war.

            What kind of container are you using? Embedded, Managed or Remote?

             

            Additionally I would try

            @Resource(mappedName="mydatasource") (If the test is really executed in the context of the war!)

            and

            @Resource(lookup="jdbc/myDS")

             

            AFAIK the name attribute refers to the name that has to be used to address the injection point when configuring the resource-ref using a deployment descriptor.

             

            Kind regards,

            Robert

            • 3. Re: weblogic 12c resource-ref not working
              Vikas B Newbie

              I had created sample program and I created datasource using weblogic web console but I am having problem injecting datasource using @Resource..

               

              here is my sample program :

               

              TestClass.java

              package com.xyz.risk.execution.test;

               

              import javax.inject.Inject;

               

              import junit.framework.Assert;

               

              import org.jboss.arquillian.junit.Arquillian;

              import org.junit.Test;

              import org.junit.runner.RunWith;

               

               

              @RunWith(Arquillian.class)

              public class TestClass extends BasicExecutionTest {

               

                  @Inject

                  private MyTest mytest;

               

                  @Test

                  public void test() throws Exception {

               

                      mytest.testMe();

                      Assert.assertNotNull(mytest);

                      Assert.assertNotNull(mytest.getDs());

                  }

               

              }

              -----------------------------------------------

              BasicExecutionTest.java

               

              package com.xyz.risk.execution.test;

               

              import org.jboss.arquillian.container.test.api.Deployment;

              import org.jboss.shrinkwrap.api.ShrinkWrap;

              import org.jboss.shrinkwrap.api.spec.WebArchive;

               

              public class BasicExecutionTest {

               

                  @Deployment

                  public static WebArchive createTestArchive() {

                      return ShrinkWrap

                              .create(WebArchive.class, "riskexecution.war")

                              .addPackages(false, "com.xyz.risk.execution.test")

                             

                                  .setWebXML("webapp/WEB-INF/web.xml")

                              .addAsWebInfResource("webapp/WEB-INF/beans.xml", "beans.xml");

                             

               

                  }

               

              }

               

              ---------------------------------

              package com.xyz.risk.execution.test;

               

              import javax.annotation.Resource;

              import javax.ejb.Stateless;

              import javax.sql.DataSource;

               

              @Stateless

              public class MyTest {

               

                 

                  

                  @Resource(name="java:/xyz/datasources/xyzRiskDS")   

                  private DataSource ds;

               

                 

                 

                  public void testMe() {

                      System.out.println("Testing ... ");

                  }

                 

                  public DataSource getDs() {

                      return ds;

                  }

               

                 

                  public void setDs(DataSource ds) {

                      this.ds = ds;

                  }

                 

                 

              }

              -------------------------------------------------------

              XYZRiskDS-...jdbc.xml

               

              <?xml version='1.0' encoding='UTF-8'?>

              <jdbc-data-source xmlns="http://xmlns.oracle.com/weblogic/jdbc-data-source" xmlns:sec="http://xmlns.oracle.com/weblogic/security" xmlns:wls="http://xmlns.oracle.com/weblogic/security/wls" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/jdbc-data-source http://xmlns.oracle.com/weblogic/jdbc-data-source/1.2/jdbc-data-source.xsd">

                <name>xyzRiskDS</name>

                <jdbc-driver-params>

                  <url>jdbc:oracle:thin:@localhost:1521:xyz</url>

                  <driver-name>oracle.jdbc.OracleDriver</driver-name>

                  <properties>

                    <property>

                      <name>user</name>

                      <value>MiddleTier</value>

                    </property>

                  </properties>

                  <password-encrypted>{AES}isuecgyVwDdokUNfe5/QDhn4+1mtUv3+9i70+QL1TzM=</password-encrypted>

                </jdbc-driver-params>

                <jdbc-connection-pool-params>

                  <test-table-name>SQL SELECT 1 FROM DUAL</test-table-name>

                </jdbc-connection-pool-params>

                <jdbc-data-source-params>

                  <jndi-name>java:/xyz/datasources/xyzRiskDS</jndi-name>

                  <global-transactions-protocol>OnePhaseCommit</global-transactions-protocol>

                </jdbc-data-source-params>

              </jdbc-data-source>

              • 4. Re: weblogic 12c resource-ref not working
                Robert Panzer Newbie

                Hi,

                 

                I don't have WebLogic, but the name property of the annotation does not look right to me.

                @Resource.name is usually not the global JNDI name.

                 

                Did you already try @Resource(lookup="java:/xyz/datasources/xyzRiskDS")? That should refer to a

                 

                Kind regards,

                Robert

                • 5. Re: weblogic 12c resource-ref not working
                  Vikas B Newbie

                  no, lookup did not work either. Do we have any other alternative ?