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)方法。
文章來源于領測軟件測試網 http://www.kjueaiud.com/