2 Replies Latest reply on Aug 11, 2006 2:59 AM by akovcic

    @TransactionAttribute ignored on generic interface methods

    akovcic

      hi,

      I have a problem with transactions annotations for stateless session bean methods defined in generic bussiness interface. Annotations are ignored and defaults are used. Here is the code with junit test case that fails:


      Some session bean with mandatory transactional settings on both methods (one setting is ignored)

      import javax.ejb.Stateless;
      import javax.ejb.TransactionAttribute;
      import javax.ejb.TransactionAttributeType;
      
      @Stateless(name = "FrogCalculator")
      public class FrogCalculatorBean implements FrogCalculator {
      
       @TransactionAttribute(value = TransactionAttributeType.MANDATORY)
       public Frog add(Frog t1, Frog t2) {
       if (t1 == null || t2 == null)
       throw new IllegalArgumentException();
      
       return new Frog(t1.value + t2.value);
       }
      
       @TransactionAttribute(value = TransactionAttributeType.MANDATORY)
       public Frog substract(Frog n1, Frog n2) {
       if (n1 == null || n2 == null)
       throw new IllegalArgumentException();
      
       return new Frog(n1.value - n2.value);
       }
      
      }
      

      Business interface of above bean. It extends generic interface.
      public interface FrogCalculator extends GenericCalculator<Frog> {
      
       public Frog substract(Frog n1, Frog n2);
      
      }
      

      Generic interface
      public interface GenericCalculator<T> {
      
       public T add(T t1, T t2);
      
      }
      

      Just a modell class
      public class Frog {
      
       int value;
      
       public Frog(int num){
       this.value = num;
       }
      
       public String toString(){
       return String.valueOf(value);
       }
      
      }
      

      Junit test case
      import javax.ejb.EJBTransactionRequiredException;
      import javax.persistence.EntityManager;
      import javax.transaction.TransactionRequiredException;
      
      import junit.framework.Test;
      
      import org.hibernate.Hibernate;
      
      import com.one2snap.dao.UserDAO;
      import com.one2snap.model.test.EntityA;
      import com.one2snap.test.EJBTestCase;
      
      public class FrogCalculatorTestCase extends EJBTestCase {
      
       private int campaignId;
       private Frog n1, n2;
      
       private FrogCalculator calculator;
      
       /**
       * Constructor.
       *
       * @param name
       */
       public FrogCalculatorTestCase(String name) {
       super(name);
       wrapInTransaction = false;
       }
      
       public static Test suite() throws Exception {
       return getDeploySetup(FrogCalculatorTestCase.class);
       }
      
       @Override
       protected void setUp() throws Exception {
       calculator = (FrogCalculator) getInitialContext().lookup("FrogCalculator/local");
       n1 = new Frog(10);
       n2 = new Frog(2);
       }
      
       public void testTransactionSetup1() throws Exception {
       try {
       calculator.add(n1, n2);
       fail("Should raize an EJBTransactionRequiredException exception"); // FAILS HERE!!!
       }
       catch (EJBTransactionRequiredException e) {
       log.info("Expected exception", e);
       }
       }
      
       public void testTransactionSetup2() throws Exception {
       try {
       calculator.substract(n1, n2);
       fail("Should raize an EJBTransactionRequiredException exception"); // OK, excepton is thrown a line before
       }
       catch (EJBTransactionRequiredException e) {
       log.info("Expected exception", e);
       }
       }
      
      }
      


      So it looks like annotation:

      @TransactionAttribute(value = TransactionAttributeType.MANDATORY)

      is ignored for the method 'public Frog add(Frog t1, Frog t2)' defined in generic interface as 'public T add(T t1, T t2)'.
      Known bug or something else?

      If I define transactional setting for the same method in the deployment descriptor (ejb-jar.xml) test runs withouth an error:
      <ejb-jar>
       <assembly-descriptor>
       <container-transaction>
       <method>
       <ejb-name>FrogCalculator</ejb-name>
       <method-name>add</method-name>
       </method>
       <trans-attribute>Mandatory</trans-attribute>
       </container-transaction>
       </assembly-descriptor>
      </ejb-jar>
      


      This proves that transaction annotation is ignored for the 'add' method

      PS.

      JUnit test is run agains JBoss embeddable:

      14:30:01,166 INFO [Version] Hibernate EntityManager 3.2.0.CR1
      14:30:01,182 INFO [Version] Hibernate Annotations 3.2.0.CR1

      The same problem with beans deployed under JBoss 4.0.4GA