3 Replies Latest reply on May 8, 2007 4:27 PM by bill.burke

    Resource injection with Embedded EJB3 Container

    estiedi

      I am trying to unit test a SSB using the embedded container.

      The problem I have is that I need a DataSource injected in the session bean:

       @Resource(mappedName = "java:/GP3DS")
       public DataSource dataSource;
      


      When I perform the unit test (from an Ant script), the dataSource variable is null (and as soon as accessed of course throwing a NullPointerException).
      However when I look it up the "traditional" way, using InitialContext.lookup(), it works just fin.
      Also, in a regular JBoss AS (I'm using 4.0.5 GA) the injection using the annotation works perfectly well.

      This is the code & configuration:

      the session bean
      @Stateless
      public class CrossRatesBean implements gp3.xrate.CrossRatesLocal {
       @Resource(mappedName = "java:/GP3DS")
       private DataSource dataSource;
      
      // do some SQL stuff
      
      }
      


      I configured the datasource like this in
      embedded-jboss-beans.xml:
      <bean name="DefaultDSBootstrap" class="org.jboss.resource.adapter.jdbc.local.LocalTxDataSource">
       <property name="driverClass">oracle.jdbc.OracleDriver</property>
       <property name="connectionURL">jdbc:oracle:thin:@gp3testserver:1521:gp3</property>
       <property name="userName">foo</property>
       <property name="password">bar</property>
       <property name="jndiName">java:/GP3DS</property>
       <property name="minSize">0</property>
       <property name="maxSize">10</property>
       <property name="blockingTimeout">1000</property>
       <property name="idleTimeout">100000</property>
       <property name="transactionManager"><inject bean="TransactionManager"/></property>
       <property name="cachedConnectionManager"><inject bean="CachedConnectionManager"/></property>
       <property name="initialContextProperties"><inject bean="InitialContextProperties"/></property>
       </bean>
      
       <bean name="GP3DS" class="java.lang.Object">
       <constructor factoryMethod="getDatasource">
       <factory bean="DefaultDSBootstrap"/>
       </constructor>
       </bean>
      


      This code is working in the unit test:
       DataSource ds = (DataSource) ctx.lookup("java:/GP3DS");
       if (ds == null) {
       fail("Datasource java:/GP3DS not found!");
       } else {
       Connection con = ds.getConnection();
       Statement stmt = con.createStatement();
       ResultSet rs = stmt.executeQuery("SELECT TO_CHAR(SYSDATE, 'MM-DD-YYYY HH24:MI:SS') FROM DUAL");
       rs.next();
       System.out.println("SYSDATE on GP3Test: " + rs.getString(1));
       stmt.close();
       con.close();
       }
      


      I don't understand why it should not work in the embedded container, but I cannot find what I am missing.
      I'd greatly appreciate if someone could tell me what I'm doing wrong.