Re: Two step transactions?

"Justin T. Sampson" <[email protected]> Fri, 15 Nov 2013 01:33:20 -0800
Newsgroups gmane.comp.java.prevayler
Message-ID <CAAshuuHgRUXZaHzWTpLv0M3MF_ht3U1ZgS+jo4FzieJ90Y2j2g@mail.gmail.com>
I definitely sympathise with this idea, and I've played with implementing
something similar. There's one fundamental flaw in the correctness of this
approach that you'll have to watch out for, though.

Remember that Prevayler's transaction processing is highly pipelined. If
three transactions come in at the same time, they're processed like this:

1. Journal all three transactions in a single batch.
2. Execute first transaction.
3. Execute second transaction.
4. Execute third transaction.

With a separate preparation phase before journaling, those transactions
might be processed like this:

1. Prepare first transaction.
2. Prepare second transaction.
3. Prepare third transaction.
4. Journal all three transactions in a single batch.
5. Execute first transaction.
6. Execute second transaction.
7. Execute third transaction.

The thing is, at the point where the third transaction is being prepared,
the other transactions haven't executed yet, but at the point where it's
being executed, they have been, so the validation that was done is already
outdated.

This problem is not insurmountable, but it's just an example of the kind of
things you have to start worrying about if you try to break up the
atomicity of your transactions.

Cheers,
Justin
On Nov 15, 2013 12:05 AM, "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