2 Replies Latest reply on Oct 16, 2008 9:25 AM by Nicklas Karlsson

    Canceling changes (rollback) in a SMPC

    Eric H Novice

      I'm trying to really understand something about conversations and Seam-Managed Persistence Context (SMPC).


      I'm using a conversation in the true wizard style.  A user selects, say, the invoice he is going to make a change to from a table.  That starts a conversation and a conversation-scoped component, let's call it InvoiceEditor.  It looks (partly) like this:


      @Name("invoiceEditor")
      @Scope(ScopeType.CONVERSATION) public class InvoiceEditor {
      
          @In private EntityManager entityManager; // SMPC
      
          private Long invoiceId; // and getters and setters
      
          private Invoice invoice; // and getter
      
          @Begin public void select() {
              invoice = (select it using the entity manager and invoice ID)
          }
      
          @End public void finished() {
      
          }
      }



      Here's the question: what if the user is making some changes to this Invoice object.  He makes a few changes then realizes that he didn't want to change it, he wants to cancel everything he has done.  Or another example, he starts making some changes but doesn't finish and he has to leave in middle, and of course he does not want a half-changed invoice to be persistent.


      Obviously what we want is for the changes made to the invoice component to stay in memory only until they are explicitly committed.


      Here's how I'm trying to do it:  I put @Begin(flushType=MANUAL) and then in the @End method, I put in a entityManager.flush() calls, if I want the changes to stick.  I also put a method like this:


      @End public void cancel() {
      }



      which ends the convo without flushing.


      Is this the right way to do it?  Is there some more Seam way way of doing it?  Also, if this convo times out before flushing, I assume the changes are forgotten (not committed)?


      Thanks in advance for helping me understand this stuff.