2 Replies Latest reply on Feb 23, 2011 5:39 AM by swenvogel

    Flushing EntityManger but database record is not written.

    swenvogel

      Hi,


      i have a question about manually flushing the entity manager.
      In the following test code snippet i persist a DispatchOrder
      object and afterwards directly call flush.


      The problem is that the new record is not written to the
      database until the method end. Because of that the status
      updates in the loop are also not visible until the method ends.


      In my use case i have a long running process and users should
      be able to query the process status through the informations
      in the database. But this problem users can only the result
      not the progress.


      Any ideas how to do this?





      @Name("dispatchProcess")
      @Scope(ScopeType.SESSION)
      public class DispatchProcess {
      
        @In
        private EntityManager entityManager;
      
        private DispatchOrder order;
      
        public void start() {
          order.setStatus(DispatchStatus.InProgress);
                
          Session session = (Session) entityManager.getDelegate();
          session.setFlushMode(FlushMode.MANUAL);
                
          entityManager.persist(order);
          entityManager.flush();
      
          for (long i = 0; i < 3; ++i) {
               order.getFinishedRecipients().add(i);
      
               Thread.sleep(3000);
               entityManager.flush();
          }
          
          order.setStatus(DispatchStatus.Finished);
        }
      
      
        public void setOrder(DispatchOrder order) {
          this.order = order;
        }        
      
      }