4 Replies Latest reply on Jun 13, 2008 10:51 AM by jaikiran pai

    Transactions  Question: Subsequent calls the method fail due

    lpmon Novice

      I have a stateless bean method that calls
      em.createNativeQuery(sql).executeUpdate();

      I call it from another method in the same SLSB in a loop. If, for example the first call fails, then all subsequent calls fail with this error:

      javax.persistence.TransactionRequiredException: Executing an update/delete query

      Am I required to do something to "clean-up" the first error? I thought the REQUIRES_NEW transaction annotation would start over with a clean slate so to speak.

      Please advise.

      @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
      public int runStatement(String sql) throws Exception
      {
      try {
      return em.createNativeQuery(sql).executeUpdate();
      }
      catch (Exception ex)
      {
      log.error(....);
      throw(ex);
      }
      }

      also: The calling method catches the re-thrown exception.

        • 1. Re: Transactions  Question: Subsequent calls the method fail
          jaikiran pai Master

           

          "lpmon" wrote:

          I call it from another method in the same SLSB in a loop.

          I thought the REQUIRES_NEW transaction annotation would start over with a clean slate so to speak.




          If you are calling the method as a plain java method invocation in that loop, then the TransactionAttribute will have no affect. You will have to call the method on the EJB proxy for the transaction attributes to take affect. Something like this:
          public void outerMethod(...) {
          
           for (int i=0;i<10; i++) {
          
           //get the proxy
           MyBeanInterface bean = (MyBeanInterface) this.sessionContext.getBusinessObject(UserManager.class);
          
           // call the inner method on the proxy
           bean.runStatement("some sql");
           }
          
          }


          • 2. Re: Transactions  Question: Subsequent calls the method fail
            jaikiran pai Master

            Typo.

            "jaikiran" wrote:


            //get the proxy
            MyBeanInterface bean = (MyBeanInterface) this.sessionContext.getBusinessObject(UserManager.class);



            This was intended to be:

            //get the proxy
            MyBeanInterface bean = (MyBeanInterface) this.sessionContext.getBusinessObject(MyBeanInterface.class);
            


            • 3. Re: Transactions  Question: Subsequent calls the method fail
              lpmon Novice

              Thanks for the input. I have never heard that you must use the proxy in this scenario. I have seen this statement (from JBoss trailbalzer):

              "In EJB 3.0 applications, transaction properties are most frequently declared for methods in session beans. If a method requires transaction, all operations in the method, including database updates, are only committed after the method exits normally."

              If it is true that a proxy based call is required then it would be very important to include that in or near the above paragraph. Where is this documented? I would like to read up on this. I expect that reading has other useful info.

              I made a change to use the proxy call and I still get the same exact result!

              In other words I now, in a non SLSB class, lookup the interface and make a call in a loop. The called method has @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) .

              FYI: If I call it passing a series of valid SQL all statements execute okay. I have some scenarios where some sql statements in a list will error (long story). I need to ignore those and continue.

              I am guessing there is something else I need to do (like .. reset the em, whatever that means ) when the error occurs but I am not sure what. The fact that the same em is used between calls is probably the root cause.

              • 4. Re: Transactions  Question: Subsequent calls the method fail
                jaikiran pai Master

                 

                "lpmon" wrote:
                Thanks for the input. I have never heard that you must use the proxy in this scenario. I have seen this statement (from JBoss trailbalzer):

                "In EJB 3.0 applications, transaction properties are most frequently declared for methods in session beans. If a method requires transaction, all operations in the method, including database updates, are only committed after the method exits normally."

                If it is true that a proxy based call is required then it would be very important to include that in or near the above paragraph. Where is this documented? I would like to read up on this.


                If you are calling a method from within the bean (without using the proxy or what's called the EJBObject in EJB2.x terms) then it will be just another java method invocation. No new transactions will be started even if the transaction attribute on the method being called is REQUIRES_NEW. You might find this explained in a J2EE book.

                "lpmon" wrote:

                I made a change to use the proxy call and I still get the same exact result!


                The fact that the same em is used between calls is probably the root cause.


                I see what you are saying. I always thought that when a new transaction is started, a *new instance* of the container managed entitymanager will be injected. I will have to read the specs to understand what's the expected behaviour.