使用Objectify在GAE上同时写入数据 [英] Using Objectify to concurrently write data on GAE

查看:129
本文介绍了使用Objectify在GAE上同时写入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

举例来说,我有以下物品化模型:

  @Cache 
@实体
公共类CompanyViews实现Serializable,持久性{

@Id
私有长ID;
私人创建日期;
私人修改日期;
private long companyId;

........

私人整数计数器;

........

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

@Override
public void persist(Boolean async){
ObjectifyService.register(Feedback.class);
//设置一些变量
setUuid(UUID.randomUUID()。toString()。toUpperCase());
setModified(new Date());
if(getCreated()== null){
setCreated(new Date());

//执行持续
if(async){
ofy()。save()。entity(this);
} else {
ofy()。save()。entity(this).now();
}
}
}

我想使用计数器字段来跟踪视图的数量,或打开数字或基本上使用整数字段来计算某些内容。



现在发生的情况是,对于一个GAE实例,将调用以下内容:



A:

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

以及另一个实例:

B :

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

如果他们同时读取计数器或在其他实例持续存在之前读取计数器, ,他们将相互覆盖。



在MySQL / Postgres中,您获得行级锁定,如何为GAE上的Objectify实体执行行级锁定?

解决方案

您需要使用 transactions



请注意,由于您更新了相同的实体,因此您将拥有约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?

解决方案

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.

这篇关于使用Objectify在GAE上同时写入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆