示例:使用只读实体 Bean
有关编写使用只读实体 Bean 的 Enterprise JavaBeans (EJB) 应用程序的使用方案和示例。
使用方案
客户有一个包含分类定价和运费信息的数据库,每天在不迟于当地时间晚上 10:00 点钟(按 24 小时计就是晚上 22:00 点钟)对此数据库进行更新。他们想编写对此数据具有只读访问权的 EJB 应用程序。即,此应用程序从不更新定价数据库。通过某个其他应用程序执行更新。
示例
客户的实体 Bean 本地接口可能是:
public interface ItemCatalogData extends EJBLocalObject {
public int getItemPrice();
public int getShippingCost(int destinationCode);
}
调用此 EntityBean 来计算包括装运在内总成本的无状态 SessionBean 方法(假设它是 TxRequired),其中的代码类似于:
.....
// Some transactional steps occur prior to this point, such as removing the item from
// inventory, etc.
// Now obtain the price of this item and start to calculate the total cost to the purchaser
ItemCatalogData theItemData =
(ItemCatalogData) ItemCatalogDataHome.findByPrimaryKey(theCatalogNumber);
int totalcost = theItemData.getItemPrice();
// ... some other processing, etc. in the interim
// ...
// ...
// Add the shipping costs
totalcost = totalcost + theItemData.getShippingCost(theDestinationPostalCode);
应用程序组装期间,客户按如下所示设置此 Bean 的 EJB 高速缓存参数:
- ActivateAt = ONCE
- LoadAt = DAILY
- ReloadInterval = 2200
不推荐使用的功能部件: 不推荐使用 IBM 部署描述符扩展(包括 WAR 文件扩展 (WEB-INF/ibm-web-ext.xmi) 和应用程序扩展 (META-INF/ibm-application-ext.xmi))的 reloadInterval 和 reloadingEnabled 属性。depfeat
每晚 22:00 时之后第一次调用 getItemPrice() 方法时 EJB 容器会从数据库重新装入定价信息。如果在调用 getItemPrice() 和 getShippingCost() 之间时间正好到达 22:00 时,那么 getShippingCost() 方法还是会返回 22:00 时可能已发生的对数据库作出的任何更改之前的值,这是因为此事务中的第一次方法调用发生在 22:00 时之前。这样,商品价格和装运成本保持彼此同步地使用。