Two step transactions?

Karl Wettin <[email protected]> Fri, 15 Nov 2013 08:35:33 +0100
Newsgroups gmane.comp.java.prevayler
Message-ID <[email protected]>
This is more or less just me thinking out loud, but how would you people feel about a transaction that validates fields prior to adding it to the journal and executing it? 

Why? I'm not quite sure. I just sort of enjoy the idea of separating the code that loads objects and throws possible exception from the part that actually execute the change on the prevalence.  And I suppose it opens up for catching some bad use of transactions and fail faster in business logic.  

I was thinking something along the lines of this:


interface Validating<P, V> {
  V prepare(P prevalence)  throws Exception;
}

interface ValidatingTransaction<P, V> extends Validating<P, V> {
  void executeOn(P prevalence, Date executionTime, V validation) throws Exception;
}

interface ValidatingTransactionWithQuery<P, R, V>, Validating<P, V> {
  R executeAndQuery(P prevalence, Date executionTime, V validation) throws Exception;
}

class AddBarToFoo implements ValidatingTransaction<Root, Preparation> {

  static class Preparation {
    Bar bar;
    Foo foo;
  }

  long barIdentity;
  long fooIdentity;

  Preparation prepare(Root root) throws Exception {
    Preparation p = new Preparation();
    p.bar = root.bars.get(barIdentity); 
    if (p.bar == null) throw new NullPointerException("No bar with identity " + barIdentity);
    p.foo = root.foos.get(foodentity); 
    if (p.foo == null) throw new NullPointerException("No foo with identity " + barIdentity);
    return preparation;
  }

  void executeOn(P prevalence, Date executionTime, Preparation p) throws Exception {
    p.foo.bars.add(bar);
  }
}


class BusinessLogic {
  void logic() throws Exception {
     …
     try {
       Service.getInstance().getPrevayler().execute(new AddBarToFoo(foo.identity, bar.identity);
     } catch (PreparationException pe) {
       // Preparation failed, transaction not appended to the journal
     } catch (Exception e) {
       // Execution failed, transaction was appended to the journal
     }
  }
}



------------------------------------------------------------------------------
DreamFactory - Open Source REST & JSON Services for HTML5 & Native Apps
OAuth, Users, Roles, SQL, NoSQL, BLOB Storage and External API Access
Free app hosting. Or install the open source package on any LAMP server.
Sign up and see examples for AngularJS, jQuery, Sencha Touch and Native!
http://pubads.g.doubleclick.net/gampad/clk?id=63469471&iu=/4140/ostg.clktrk
_______________________________________________
To unsubscribe go to the end of this page: http://lists.sourceforge.net/lists/listinfo/prevayler-discussion
_______________________________________________
"Databases in Memoriam" -- http://www.prevayler.org