4 Replies Latest reply on Aug 22, 2008 4:53 PM by rphadnis

    EJB3 transaction propagation

    rphadnis

      Hello,
      I need help with transactions in EJB3.

      I have an EJB which is called at a regular frequency using a timer. The EJB has a method with the @Timeout annotation. The method also specifies the REQUIRES_NEW transaction attribute. In this method, I am retrieving a list of records from the database. For each record, I am calling another method (update) in the same bean to update that record. The update method also specifies REQUIRES_NEW transaction attribute.

      What I want to know is, if the updating of all the records takes more time than the transaction time out specified in the jboss configuration, will the update of all records be rolled back ?

      What seems to be happening is the list is long and it takes more than 300 seconds to do the update of all records. Once that happens none of the records get updated. But I thought by adding the TransactionAttributeType.REQUIRES_NEW attribute on the update method,

      I am trying to specify that each record should be updated in a separate transaction.

      Here is some pseudo code:

      @Timeout
      @TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
       public void updateList(Timer timer) {
       List<Record> list = getRecords();
       for (Record r : list) {
       update(r);
       }
       logger.log("done");
      }
      
      @TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)
      private void update(Record r) {
       //call another webservice to get update data
       // set updated fields in record
       //call merge on the injected RecordDA
       recordDA.merge(r);
      }
      
      


      Please help.

      Rahul Phadnis

        • 1. Re: EJB3 transaction propagation
          zilbi

          not sure but i think what happens is the the call you make for update() are not recognized by the container as ejb calls and that's why a new transaction is not opend.

          think you need to get a ref to this ejb using the session context and invoke update using it. should look something like that

          YourLocalSession localRef = context.getBusinessObject( YourLocalSession.class );
          for...
          {
           localRef .update();
          } // end for


          hope it helps,
          zilbi

          • 2. Re: EJB3 transaction propagation
            rphadnis

            So are the transaction related annotations on methods called from a bean method not applied ?

            Also update(Record r) is marked as a private method and not defined in the bean interface.

            • 3. Re: EJB3 transaction propagation
              jaikiran

              zilbi is right. A plain Java call will not start a new transaction. You have to invoke that method through the EJB object.


              Also update(Record r) is marked as a private method and not defined in the bean interface.


              You will have to make it accessible through the interface.

              • 4. Re: EJB3 transaction propagation
                rphadnis

                Thank you for the reply. Is that documented somewhere ? It means that putting transaction attributes on methods that are called by the bean methods is not correct and probably should n't be allowed.