3 Replies Latest reply on Mar 16, 2010 2:54 PM by radu

    Recover transaction after validation error and EntityHome

    radu

      I'm using generated EntityHome and EntityQuery classes in my application.


      I have to insert about 400 new entities that I read from a queue and I need to inject a new EntityHome on each operation.


      The problem is that if one transaction fails because of a validation, all 400 transactions will fail with Transaction is not active error.


      How can I create a new EntityHome instance every time, so I invalidate the existing transaction and start a new one?


      Ex: I have the


      @Name(testEntityHome)
      TestEntityHome extends EntityHome<Test> {
      ...
           @Override
           public String persist() {
                  ...
              }
      }
      



      The TestEntityHome is injected in another component that performs the save operation:


      @Name("mailObserver")
      public class MailObserver {
      ...
          @Observer("newMailMessage")
          public void onMessage(Massage message) {
              TestEntityHome testEntityHome = (TestEntityHome)Component.getInstance("testEntityHome", true);
              ....
              testEntityHome.persist();
              ....
          }
      }
      



      In the message reader class, an event is raised for each new message, so MailObserver.onMessage will be called:


      public class MessageReader {
          ....
          public void getMessages() {
              ....
              for (int i = 0; i < msgs.length; i++) {
                  Events.instance().raiseEvent("newMailMessage", msgs[i]);
              }
          }
      }
      



      If one transaction fails, I will loose all transactions since the same instance is used and the entity manager is not flushed after persist() call.