4 Replies Latest reply on Sep 13, 2010 9:37 AM by rszulgo

    Calling ejb2.1 business method in transaction from ejb3

    rszulgo

      Greetings,

      I have an issue related to the call of ejb2.1 business method that has to be  executed in transaction by en ejb3 method. The ejb2.1 and ejb3 are managed by container and ejb3 creates and uses a Remote interface of ejb2.1. The ejb2 business method is defined in ejb-jar.xml as follows:

       

       

      <container-transaction>
           <method>
                <ejb-name>Document</ejb-name>
                <method-name>set</method-name>
           </method>
           <trans-attribute>Mandatory</trans-attribute>
      </container-transaction>
      
      <container-transaction>
      <method>
      <ejb-name>Document</ejb-name>
      <method-name>set</method-name>
      </method>
      <trans-attribute>Mandatory</trans-attribute>
      </container-transaction>

       

      So the caller (ejb3) of method set() needs to start a transaction. I decorated that ejb3 method with:

       

       

      @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
      

       

      to start a new transaction, however I get an exception:

       

      javax.transaction.TransactionRequiredException: Transaction Required
           at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:426)
           at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:181)
           at org.jboss.ejb.plugins.security.PreSecurityInterceptor.process(PreSecurityInterceptor.java:97)
           at org.jboss.ejb.plugins.security.PreSecurityInterceptor.invoke(PreSecurityInterceptor.java:81)
           at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:205)
           at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:138)
           at org.jboss.ejb.SessionContainer.internalInvoke(SessionContainer.java:650)
           at org.jboss.ejb.Container.invoke(Container.java:1029)
           at sun.reflect.GeneratedMethodAccessor359.invoke(Unknown Source)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
           at java.lang.reflect.Method.invoke(Method.java:597)
           at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:157)
           at org.jboss.mx.server.Invocation.dispatch(Invocation.java:96)
           at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
           at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
           at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:668)
           at org.jboss.invocation.local.LocalInvoker$MBeanServerAction.invoke(LocalInvoker.java:169)
           at org.jboss.invocation.local.LocalInvoker.invoke(LocalInvoker.java:118)
           at org.jboss.invocation.InvokerInterceptor.invokeLocal(InvokerInterceptor.java:209)
           at org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.java:195)
           at org.jboss.proxy.TransactionInterceptor.invoke(TransactionInterceptor.java:61)
           at org.jboss.proxy.ejb.SecurityContextInterceptor.invoke(SecurityContextInterceptor.java:64)
           at org.jboss.proxy.SecurityInterceptor.invoke(SecurityInterceptor.java:68)
           at org.jboss.proxy.ejb.StatefulSessionInterceptor.invoke(StatefulSessionInterceptor.java:118)
           at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:101)
           at $Proxy254.set(Unknown Source)
           at pg.seti.ad.dict.ejb3.DictManagerBean.saveDict(DictManagerBean.java:270)
      

       

      When I've changed in the ejb-jar.xml descriptor <trans-attribute> value from Mandatory to RequiresNew to start new transaction whenever the transaction was started or not, the exception doesn't occurs. It looks like the transaction does not propagate from ejb3 to ejb2.1.

       

      Any help greatly appreciated, thanks.

        • 1. Re: Calling ejb2.1 business method in transaction from ejb3
          jaikiran

          How do you lookup and invoke on the EJB3 bean?

          • 2. Re: Calling ejb2.1 business method in transaction from ejb3
            rszulgo

            I use that function to return an object of ejb2.1 remote interface. I pass to that function JNDI of ejb2.1 and an interface class as a 'narrow' argument. Then on that object I invoke create() method to create ejb2.1.

             

             

            private static Object lookupHome(java.util.Hashtable environment, String jndiName, Class narrowTo) throws javax.naming.NamingException {
            
                  // Obtain initial context
            
                  javax.naming.InitialContext initialContext = new javax.naming.InitialContext(environment);
            
                  try {
            
                     Object objRef = initialContext.lookup(jndiName);
            
                     // only narrow if necessary
            
                     if (java.rmi.Remote.class.isAssignableFrom(narrowTo))
            
                        return javax.rmi.PortableRemoteObject.narrow(objRef, narrowTo);
            
                     else
            
                        return objRef;
            
                  } finally {
            
                     initialContext.close();
            
                  }
            
               }
            
            
            

             

             

            Business method in ejb3 is invoked simply in that way:

             

             

            ejb2.deleteFullComplex("/list");
            

             

            deleteFullComplex() method needs to be executed in a transaction mandatory.

             

            EJB3 object is created like a normal POJO via constructor method.

             

            Thanks for your time,

            -- Radek

            • 3. Re: Calling ejb2.1 business method in transaction from ejb3
              thunder.farmer

              EJB3 object is created like a normal POJO via constructor method --  probably this is root cause.

               

              post more related code and config xml

              • 4. Re: Calling ejb2.1 business method in transaction from ejb3
                rszulgo

                Seems that change of the way of creating ejb3 session bean to JNDI lookup works for me, but ...

                How to initialize the object? I had a constructor with parameters. How to be sure that my object is properly initialized with dynamic parameters? Is the only way calling manually init() method after the lookup?