1

Let's for example say I have the following objectify model:

@Cache
@Entity
public class CompanyViews implements Serializable, Persistence {

    @Id
    private Long id;
    private Date created;
    private Date modified;
    private Long companyId;

    ........

    private Integer counter;

    ........

    @Override
    public void persist() {
        persist(false);
    }

    @Override
    public void persist(Boolean async) {
        ObjectifyService.register(Feedback.class);
        // setup some variables
        setUuid(UUID.randomUUID().toString().toUpperCase());
        setModified(new Date());
        if (getCreated() == null) {
            setCreated(new Date());
        }   
        // do the persist
        if (async) {
            ofy().save().entity(this);
        } else {
            ofy().save().entity(this).now();
        }           
    }
}

I want to use the counter field to track the number of views, or number opens or basically count something using an integer field.

What happens now is that for one GAE instance, the following will be called:

A:

CompanyViews views = CompanyViews.findByCompanyId(...);
views.setCounter(views.getCounter() + 1);
views.persist();

and for another instance:

B:

CompanyViews views = CompanyViews.findByCompanyId(...);
views.setCounter(views.getCounter() + 1);
views.persist();

If they both read the counter at the same time or read the counter before the other instance has persisted it, they will overwrite each other.

In MySQL / Postgres you get row-level locking, how does one do a "row-level lock" for Objectify entities on GAE?

1 Answer 1

1

You need to use transactions when concurrently updating entities.

Note that since you update same entity you will have a limitation of about 1 write/s. To work around that look into sharding counters.

1
  • Thanks Peter, I guess it would be better to insert new entries and calculate stats once per day then and just put it inside a transaction. Jun 6, 2013 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.