java – Appengine本地数据存储中的不一致的事务行为?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Appengine本地数据存储中的不一致的事务行为?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2422字,纯文字阅读大概需要4分钟。
内容图文
![java – Appengine本地数据存储中的不一致的事务行为?](/upload/InfoBanner/zyjiaocheng/791/d369b0407bd2454fb4dce40037f52fe5.jpg)
Appengine文档说这是关于数据存储区中的事务:
http://code.google.com/appengine/docs/java/datastore/transactions.html#Isolation_and_Consistency
In a transaction, all reads reflect the current, consistent state of the
Datastore at the time the transaction started. This does not include
previous puts and deletes inside the transaction. Queries and gets inside
a transaction are guaranteed to see a single, consistent snapshot of the
Datastore as of the beginning of the transaction.
考虑到这一点,我创建了以下两个单元测试来测试它(针对本地数据存储区).我会考虑下面的两个测试来通过.但是,只有“test1”通过,而“test2”失败.唯一的区别是在“test1”中提交tx1.
这是本地数据存储区中的错误,对GAE文档的误解,还是单元测试中的错误?或者是其他东西?
谢谢!
@Test(expected = EntityNotFoundException.class)
public void test1() throws EntityNotFoundException {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
// Create 2 Transactions...
Transaction txn1 = datastore.beginTransaction();
Transaction txn2 = datastore.beginTransaction();
try {
Key entityWithStringKey = KeyFactory.createKey("TestEntity", "test");
Entity entityWithString = new Entity(entityWithStringKey);
datastore.put(txn1, entityWithString);
entityWithString = datastore.get(txn2, entityWithStringKey);
// The above should throw EntityNotFoundException
assertNull(entityWithString);
}
finally {
if (txn1.isActive()) {
txn1.rollback();
}
if (txn2.isActive()) {
txn2.rollback();
}
}
@Test(expected = EntityNotFoundException.class)
public void test2() throws EntityNotFoundException {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
// Create 2 Transactions...
Transaction txn1 = datastore.beginTransaction();
Transaction txn2 = datastore.beginTransaction();
Key entityWithStringKey = KeyFactory.createKey("TestEntity", "test");
try {
Entity entityWithString = new Entity(entityWithStringKey);
datastore.put(txn1, entityWithString);
txn1.commit();
} finally {
if (txn1.isActive()) {
txn1.rollback();
}
}
try {
Entity entityWithString = datastore.get(txn2, entityWithStringKey);
assertNull(entityWithString);
// The above should throw EntityNotFoundException
}
finally {
if (txn2.isActive()) {
txn2.rollback();
}
}
}
解决方法:
我怀疑当你调用datastore.beginTransaction时事务并没有实际开始 – 它在事务首次命中数据库时开始 – 这将是最小化数据库端锁定的最佳方法.
在测试2中,您可以尝试在txn1.commit()之前在txn2上添加一个额外的get().然后第二个get()(你当前执行txn2 get)应该返回null.
内容总结
以上是互联网集市为您收集整理的java – Appengine本地数据存储中的不一致的事务行为?全部内容,希望文章能够帮你解决java – Appengine本地数据存储中的不一致的事务行为?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。