如何确保与具有客体化的Appengine实体的并发请求的一致性?

时间:2014-09-23 04:56:41

标签: java google-app-engine concurrency objectify

在我的应用程序中,并发写入发生在单个实体上,我注意到当发生超过3次写入/秒时,并非所有请求都保持不变。 我正在从一个事务中发出请求,但我也没有看到并发修改异常,我正在使用Objectify所以不抛出异常可能是一个客观化的事情。 我知道对于并发写入我需要实现分片计数器,但即使在那里我也要绝对确定如果写入被丢弃,我会被告知它。 在物化中是否存在等价的@version,或者在我的客观化实现中是否可以使用JPA / JDO中的@version机制? 继承我的实体的代码:

            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) {

                }
            }
        }

要检查实体是否已被持久化,我只需在我的appengine仪表板上查看我的数据存储查看器。

对于持续存在场地的每个请求,我也尝试检索n检查实体是否已经持久化并且在代码运行期间奇怪地返回它正确返回 - 但数据存储查看器却讲述了另一个故事:/ 用于检查数据是否已正确保留的代码。

               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();
                } 

1 个答案:

答案 0 :(得分:1)

您的代码实际上并未在事务中执行任何数据存储区操作。阅读有关交易的Objectify文档:

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