this.tx.begin();
local = localHome.findByPrimaryKey(pk);
// code doesn't see changes made directly
// because bean instance was cached between transactions
assertFalse(newName.equals(local.getName());
try {
// this should throw optimistic concurrency
// exception (on commit)
local.setName("George");
this.tx.commit();
fail("expected OptimisticConcurrencyException not thrown");
}
catch (RollbackException expected) {
// unfortunately there seems to be no better way to
// assert that underlying exception was
// an OptimisticConcurrencyException
assertTrue("Unexpected exception type: "+expected,
expected.getMessage()
.indexOf("Optimistic concurrency violation") > 0);
}
}
}
...
我希望您同意对长期缓存的失效进行控制是有益的。随着它的出现,甚至出现了WebLogic 7.0和8.1的解决方案。与可用于为只读bean清空缓存的CachingHome/CachingLocalHome接口类似,一个EntityEJBHome和一个EntityEJBLocalHome,再加上同系列的invalidate()方法,使应用程序能让特定实体bean的所有缓存或者一部分缓存无效。WebLogic Server中的任何CMP本地接口都可转换为EntityEJBLocalHome。利用前面的例子,我们可以在updatePersonNameViaJdbc()方法调用后插入下面的代码:
...
// flush cache
assertTrue("PersonLocalHome not instance of EntityEJBLocalHome: "+ localHome, localHome instanceof EntityEJBLocalHome);
EntityEJBLocalHome entityEJBLocalHome = (EntityEJBLocalHome)localHome;
entityEJBLocalHome.invalidate(pk); 软件测试 ...
现在当下一次调用findByPrimaryKey()时,bean实例将被从数据库中重新加载,并且所有一切变得更好。除了invalidate()方法,还有invalidateAll()和invalidate(Collection)方法。
文章来源于领测软件测试网 https://www.ltesting.net/