+

Search Tips   |   Advanced Search

Example: Using a read-only entity bean

A usage scenario and example for writing an Enterprise JavaBeans (EJB) application that uses a read-only entity bean.


Usage scenario

A customer has a database of catalog pricing and shipping rate information that is updated daily no later than 10:00 PM local time (22:00 in 24-hour format). They want to write an EJB application that has read-only access to this data. That is, this application never updates the pricing database. Updating is done through some other application.


Example

The customer's entity bean local interface might be:

	public interface ItemCatalogData extends EJBLocalObject {
	 
	  public int getItemPrice();
	 
	  public int getShippingCost(int destinationCode);
	 
	}

The code in the stateless SessionBean method (assume it is a TxRequired) that invokes this EntityBean to figure out the total cost including shipping, would look like:

	.....
	// 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);

At application assembly time, the customer sets the EJB caching parameters for this bean as follows:

On the first call to the getItemPrice() method after 22:00 each night, the EJB container reloads the pricing information from the database. If the clock strikes 22:00 between the call to getItemPrice() and getShippingCost(), the getShippingCost() method still returns the value it had prior to any changes to the database that might have occurred at 22:00, since the first method invocation in this transaction occurred prior to 22:00. Thus, the item price and shipping cost used remain in sync with each other.


Related:

  • Enterprise beans
  • Concurrency control
  • Developing applications that use JNDI
  • WebSphere extensions to the Enterprise JavaBeans specification
  • Enterprise bean development best practices