【发布时间】:2017-04-28 08:44:04
【问题描述】:
我正在尝试在同一个实体组中编写两个实体,一个通过 objectify,另一个通过低级数据存储 api。我创建了一个数据存储事务来执行此操作。我的代码如下所示 -
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction transaction = datastore.beginTransaction();
try {
Summary summary = Ofy.ofy().load().key(com.googlecode.objectify.Key.create(parent)).now();
// Change summary
Ofy.ofy().save().entity(summary).now();
Key key = KeyFactory.createKey(parent, "Data", id);
Entity entity = new Entity(key);
// add stuff to entity
datastore.put(entity);
transaction.commit();
} finally {
if (transaction.isActive()) {
transaction.rollback();
}
}
但是,我最近遇到了一些数据不一致的情况,如果此事务不能保证两个实体的事务更新,则可以解释这一点。我的问题是,交易应该以这种方式进行吗?
根据此处的最后一个常见问题条目 - https://github.com/objectify/objectify/wiki/FrequentlyAskedQuestions,我们可以在事务中混合对象化和数据存储写入,但代码示例可能使用对象化事务。这是唯一的方法吗?
【问题讨论】:
标签: google-app-engine google-cloud-datastore objectify