Re: Two step transactions?

Naveen Chawla <[email protected]> Fri, 15 Nov 2013 09:01:33 +0000
Newsgroups gmane.comp.java.prevayler
Message-ID <CAGs7EcXRLKCKVTRbnPZoGptf4p4oRD=Th4jZ4aCGfMkeT-G2_A@mail.gmail.com>
I'm against this current version of your proposal because:
1. It's more code than just performing validation in "execute" method
itself (e.g. before modifying the prevalent object).
2. It appears to unnecessarily add an intermediate "validation" object.

A better implementation, in my opinion, although I'm still not sure that
pre-execution validation in Prevayler itself is a good idea, would be:

interface ValidatingTransaction<P>{
    void execute(...
    boolean validate();
}


On 15 November 2013 07:35, Karl Wettin <[email protected]> wrote:

> 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
>

------------------------------------------------------------------------------
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