简体   繁体   中英

How to ensure consistency with concurrent requests on Appengine entities with objectify?

In my application concurrent writes happen to a single entity, I notice that when more than 3 writes/sec happen not all requests are persisted. I am making the requests from inside an transaction but I dont see concurrent modification exception either, I am using Objectify so the exception not being thrown might be an objectify thing. I know that for concurrent writes I need to implement shard counters but even there I want to be absolutely sure that if a write is dropped I am informed about it. Is there an equivalent of @version in objectify or is there a way I can make use of the @version mechanism from JPA/JDO in my objectify implementation? Heres the code that persists my entity:

            while (true) {

                try {
                    ofy().transact(new VoidWork() {
                        public void vrun() {
                            Venue_Model tnxVenue = ofy().load()
                                    .type(Venue_Model.class).id(tnxVenueID)
                                    .now();
                            tnxVenue.doStuff();

                            ofy().save().entities(tnxVenue).now();
                        }
                    });
                    //There are cases where this part of the code is reached but updates to the entity are not reflected in the data store viewer.
                    break;
                } catch (ConcurrentModificationException e) {
                    // TODO Auto-generated catch block
                    if (retries == 0) {
                        e.printStackTrace();

                                + person_ref.getKey() + e.toString());
                        break;
                    }
                    retries--;


                } catch (Exception e) {

                }
            }
        }

To check if the entity has been persisted I just look at my data store viewer on my appengine dashboard.

For each request that persists a venue I also try to retrieve n check if the entity has been persisted and curiously during the code run it returns correctly- the data store viewer however tells a different story :/ Code to check if data has persisted correctly.

               venue = ofy().load().type(Venue_Model.class).id(venue_id).now(); // search if person has checked in
                if (!venue.allCheckins.contains(person_ref)) {
                    log.warning("attempt " + i + " for"
                            + person_ref.getKey());   // Never entered :/
                    venue.doStuff(person_ref, subVenueName);
                    ofy().save().entities(venue).now();
                } 

Your code does not actually execute any datastore operations within a transaction. Read the Objectify documentation regarding transactions:

https://code.google.com/p/objectify-appengine/wiki/Transactions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM