2 Replies Latest reply on Feb 16, 2008 4:05 PM by Ragav Gomatam

    Jboss 5 Beta 4 Resource Injection problem in Ejb 3

    Ragav Gomatam Novice

      Hi all,

      I have a EJb 3 EAR working fine with Jboss 5 beta 3. Copied the EAR to Beta 4 and immediately having problems in Jdbc Datasource Resource injection in ejb 3.

      Ejb 3 Code :-

      package com.ejb3.beans;
      
      import static javax.ejb.TransactionAttributeType.REQUIRED;
      
      import javax.ejb.Stateful;
      import javax.ejb.TransactionAttribute;
      import javax.ejb.EJB;
      import javax.annotation.PostConstruct;
      import javax.annotation.Resource;
      import javax.interceptor.ExcludeDefaultInterceptors;
      import javax.sql.DataSource;
      import javax.annotation.PreDestroy;
      
      import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
      import org.jboss.annotation.spring.Spring;
      
      import com.ejb3.domain.IPerson;
      import com.ejb3.interfaces.Calculator;
      import com.ejb3.interfaces.SayHello;
      
      /**
       * @author Ragav
       *
       */
      
      @Stateful
      @TransactionAttribute(REQUIRED)
      @ExcludeDefaultInterceptors
      public class SayHelloBean extends SimpleJdbcDaoSupport implements SayHello {
      
       @Resource(name = "jdbc/TestDs", type = DataSource.class, shareable = true, mappedName = "java:jdbc/OracleDS")
       private DataSource ds;
      
       @EJB(beanName = "CalculatorBean")
       private Calculator calculator;
      
       @Resource(name = "sqlForEjb")
       private String sql;
      
       @Spring(jndiName = "spring-inject", bean = "person")
       private IPerson p;
      
       public String sayHelloWithEjb3DI() {
       return "Testing ejb3 DI & your age is "
       + getSimpleJdbcTemplate().queryForInt(sql);
       }
      
       public double calulate() {
       return calculator.calculate(2, 3);
       }
      
       @PostConstruct
       public void init() {
       setDataSource(ds);
       }
      
       @PreDestroy
       public void callThis() {
       System.out.println("Invoking method: preDestroy()");
       }
      
       public IPerson getPerson() {
       return this.p;
       }
      
      }
      



      My Data Source File

      <?xml version="1.0" encoding="UTF-8"?>
      
      <!-- ===================================================================== -->
      <!-- -->
      <!-- JBoss Server Configuration -->
      <!-- -->
      <!-- ===================================================================== -->
      
      <!-- $Id: oracle-ds.xml,v 1.6 2004/09/15 14:37:40 loubyansky Exp $ -->
      <!-- ==================================================================== -->
      <!-- Datasource config for Oracle originally from Steven Coy -->
      <!-- ==================================================================== -->
      
      
      <datasources>
       <local-tx-datasource>
       <jndi-name>jdbc/OracleDS</jndi-name>
       <connection-url>jdbc:oracle:thin:@localhost:1521:XE</connection-url>
       <!--
      
       Here are a couple of the possible OCI configurations.
       For more information, see http://otn.oracle.com/docs/products/oracle9i/doc_library/release2/java.920/a96654/toc.htm
      
       <connection-url>jdbc:oracle:oci:@youroracle-tns-name</connection-url>
       or
       <connection-url>jdbc:oracle:oci:@(description=(address=(host=youroraclehost)(protocol=tcp)(port=1521))(connect_data=(SERVICE_NAME=yourservicename)))</connection-url>
      
       Clearly, its better to have TNS set up properly.
       -->
       <driver-class>oracle.jdbc.OracleDriver</driver-class>
       <user-name>system</user-name>
       <password>admin</password>
       <!-- Uses the pingDatabase method to check a connection is still valid before handing it out from the pool -->
       <!--valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name-->
       <!-- Checks the Oracle error codes and messages for fatal errors -->
       <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
       <!-- sql to call when connection is created
       <new-connection-sql>some arbitrary sql</new-connection-sql>
       -->
      
       <!-- sql to call on an existing pooled connection when it is obtained from pool - the OracleValidConnectionChecker is prefered
       <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
       -->
      
       <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
       <metadata>
       <type-mapping>Oracle9i</type-mapping>
       </metadata>
       </local-tx-datasource>
      
      </datasources>
      



      Jboss 5 beta 4 deploy Error



      22:34:51,156 ERROR [STDERR] java.lang.RuntimeException: Unable to inject jndi dependency: env/jdbc/TestDs into property com.ejb3.beans.SayHelloBean.ds: jdbc not bound
      22:34:51,156 ERROR [STDERR] at org.jboss.injection.JndiPropertyInjector.lookup(JndiPropertyInjector.java:77)
      22:34:51,156 ERROR [STDERR] at org.jboss.injection.JndiPropertyInjector.inject(JndiPropertyInjector.java:89)
      22:34:51,156 ERROR [STDERR] at org.jboss.injection.JndiPropertyInjector.inject(JndiPropertyInjector.java:84)
      22:34:51,156 ERROR [STDERR] at org.jboss.injection.JndiPropertyInjector.inject(JndiPropertyInjector.java:56)
      22:34:51,156 ERROR [STDERR] at org.jboss.ejb3.pool.AbstractPool.create(AbstractPool.java:100)
      22:34:51,156 ERROR [STDERR] at org.jboss.ejb3.InfinitePool.get(InfinitePool.java:56)
      22:34:51,156 ERROR [STDERR] at org.jboss.ejb3.pool.ThreadlocalPool.create(ThreadlocalPool.java:58)
      22:34:51,156 ERROR [STDERR] at org.jboss.ejb3.pool.ThreadlocalPool.get(ThreadlocalPool.java:113)
      22:34:51,156 ERROR [STDERR] at org.jboss.ejb3.cache.simple.SimpleStatefulCache.create(SimpleStatefulCache.java:351)
      22:34:51,156 ERROR [STDERR] at org.jboss.ejb3.stateful.StatefulContainer.createSession(StatefulContainer.java:296)
      22:34:51,156 ERROR [STDERR] at org.jboss.ejb3.session.SessionContainer.createSession(SessionContainer.java:457)
      22:34:51,156 ERROR [STDERR] at org.jboss.ejb3.stateful.StatefulLocalProxyFactory.createProxy(StatefulLocalProxyFactory.java:148)
      22:34:51,156 ERROR [STDERR] at org.jboss.ejb3.JndiProxyFactory.getObjectInstance(JndiProxyFactory.java:55)
      22:34:51,156 ERROR [STDERR] at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304)
      22:34:51,156 ERROR [STDERR] at org.jnp.interfaces.NamingContext.getObjectInstance(NamingContext.java:1312)
      22:34:51,156 ERROR [STDERR] at org.jnp.interfaces.NamingContext.getObjectInstanceWrapFailure(NamingContext.java:1329)
      22:34:51,156 ERROR [STDERR] at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:765)
      22:34:51,156 ERROR [STDERR] at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:629)
      22:34:51,156 ERROR [STDERR] at javax.naming.InitialContext.lookup(InitialContext.java:351)
      22:34:51,156 ERROR [STDERR] at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:124)
      22:34:51,156 ERROR [STDERR] at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:86)
      22:34:51,156 ERROR [STDERR] at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:122)


        • 1. Re: Jboss 5 Beta 4 Resource Injection problem in Ejb 3
          jaikiran pai Master

          Works fine on JBoss5 Beta-4 for me. I have a similar EJB (but without the Spring related stuff):

          @Stateless
          @Remote ({UserManager.class})
          @RemoteBinding (jndiBinding = "RemoteUserManagerBean")
          public class UserManagerBean implements UserManager {
          
           /**
           * Instance of logger
           */
           private static Logger logger = Logger.getLogger(UserManagerBean.class);
          
           @PersistenceContext
           private EntityManager entityManager;
          
           @Resource
           private SessionContext sessionContext;
          
           @Resource(name = "jdbc/TestDs", type = DataSource.class, shareable = true, mappedName = "java:DefaultDS")
           private DataSource dataSource;
          
           @PostConstruct
           public void init() {
           setDataSource(dataSource);
           }
          
           private void setDataSource(DataSource ds) {
           System.out.println("Do nothing ---> " + ds);
           }
          }


          The application deploys fine and accesing this bean too works fine.

          Are you sure that the datasource has been deployed, before this bean is being accessed?

          • 2. Re: Jboss 5 Beta 4 Resource Injection problem in Ejb 3
            Ragav Gomatam Novice

            Yeah...I checked the jndi View & see the DS binding there...I also have a Jboss 5 Beta 3 instance...This EAR works there flawlessly....