Objectify requires the Work executed for a transaction to be idempotent.
When a stale write occurs, the run block is executed again.

In your example, this would happen if there were two concurrent writes on
the entity that 'tk' points at. If you load inside the transaction, then
someString will be applied on the most recent write. By loading outside the
transaction you will overwrite whatever the other concurrent write put in
the datastore. This is almost always bad.

In this example, you should definitely read inside the transaction.

It can get a bit hairier when you have to write in batches because of XG
limits and you need, for example, reference data. At that point you're
making a bit of a tradeoff between correctness, complexity and performance.



On 12 October 2016 at 20:48, Rajesh Gupta <
rajesh.gu...@veersoftsolutions.com> wrote:

> Hello All,
>
> Is it ok to read the object outside of the transaction block, and then
> modify and save the object in the transaction block
>
> Or, should we always read and write the object with in the transaction
> block for the transactions to work properly
>
>
>       private void prepare(Key<Trivial> tk) { final Trivial trivNew =
> ofy().transactionless().load().key(tk).now(); Key<Trivial> k =
> ofy().transact(new Work<Key<Trivial>>() { @Override public Key<Trivial>
> run() { trivNew.setSomeString("hellofoo"); return
> ofy().save().entity(trivNew).now(); } }); }
> --
> Regards,
> Rajesh
> *www.VeersoftSolutions.com <http://www.VeersoftSolutions.com>*
> *www.GainERP.com <https://www.gainerp.com>*
> *Accounting/Inventory/Orders on Google Cloud Platform and Mobile*
>
> --
> You received this message because you are subscribed to the Google Groups
> "objectify-appengine" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to objectify-appengine+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-appengine+unsubscr...@googlegroups.com.
To post to this group, send email to google-appengine@googlegroups.com.
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-appengine/CABezLV7pcXXt9D_%2BXGdcF%2BBuErZLH777j2JnTNA3%3DjS2p%2BFb5w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to