Using Objectify to concurrently write data on GAE

半腔热情 提交于 2020-01-05 16:57:31

问题


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:


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.



来源:https://stackoverflow.com/questions/16932157/using-objectify-to-concurrently-write-data-on-gae

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!