J2EE Blueprints Design Pattern
Batch Session Bean

 name

Batch Session Bean

 abstract

Batch-updates multiple server-side objects within the context of a single network communication. Avoids the unnecessary overhead of constructing and interating collections of Entity beans when a collection of objects all need to be updated in the same way. Can also decrease network traffic by moving the update iteration into the database.

 intent

Efficiently update multiple server-side objects

 category

Object Behavioral

 motivation

Occasionally a distributed application will need to update multiple server objects simultaneously. But updating a large number of server objects one at a time, with each update requiring one or more remote method invocations, can be very expensive. In the case of a client program accessing Entity beans, for example, the client acquires a list of primary keys from an EJB finder method, and then individually updates the enterprise beans through those Beans' remote interface. Unfortunately, the server has to create and perform transaction management for each of these instances, resulting in unacceptable overhead.

One solution to this problem is the Batch Session Bean pattern, which encapsulates the transaction to be performed on multiple server objects into a server-side stateless session bean. This object receives a collection of objects to be used as parameters for updating the collection of Entity beans.

For example, the Java Pet Store allows an administrator to individually mark orders as "approved" or "denied". Once the entire batch of orders has been marked, the user submits the update to the Java Pet Store control servlet, which builds a list of order numbers and status strings, and passes that list to a stateless session bean, AdminOrdersDAO. AdminOrdersDAO iterates the collection of order number/status pairs it receives, building an SQL statement that updates all orders in the collection (with the same status) in a single operation.

Essentially, the Batch Session Bean pattern improves performance by modifying multiple database rows with a single database update statement, thereby avoiding the unnecessary overhead of Entity bean construction and maintenance. It also decreases network traffic by moving iteration on collections of server-side objects to the server.


 applicability

Use the Batch Session Bean pattern:

  • to create or update a large number of server-side objects at the same time
  • especially when the update depends upon the state of the server-side object

 structure

Figure 1. UML Diagram for the Batch Session Bean.

 participants

Client

  • indicates the operation to be performed on the server
  • specifies server side objects to be updated
  • specifies new state for each object to be updated
BatchSessionBean

  • represents a service that updates a collection of database rows
BatchSessionDAO

  • encapsulates the SQL that updates multiple database rows
  • isolates other components from database implementation details
  • can be multiple subclasses, implementing the same interface

 collaborations

Client

  • acquires input from user about which objects to update
  • acquires input from user about new state of identified objects
  • transmits acquired input to Batch Session Bean
BatchSessionBean

  • receive input from client indicating which objects to update
  • receive input from client indicating new state for each identified object
  • defers database update operation to BatchSessionDAO
BatchSessionDAO

  • updates multiple database rows simultaneously on behalf of BatchSessionBean
  • encapsulates SQL
  • isolates BatchSessionBean from details of database implementation

 consequences

The Batch Session Bean pattern has the following consequences:

  • Decreases network traffic. Updating multiple objects occurs with a single transaction.

  • Duplicates functionality. This class does nothing that couldn't be done directly with Entity beans -- it simply does it more efficiently, when updating multiple persistent beans in a similar way at the same time.

  • Avoids unnecessary Entity bean overhead. The data used to create Entity beans is updated directly in the database, logically updating the EBJs without having to physically construct them.


 implementation-issues

  • Beware large argument lists. If each Entity bean requires a great deal of information for update, serializing and transmitting the entire collection of update information may be too expensive. How to overcome this problem depends a great deal on the particular application.

  • Decide how to deal with partial failure. It is possible that some of the items in a collection will fail the operation being applied to them. The design needs to decide how to handle failure of part of a collection.


 sample-code

The classes implementing the Batch Session Bean participant in the example above are: AdminClientControllerHome.java AdminClientController.java AdminClientControllerEJB.java

The class BatchSessionDAO participant class in the example above is: AdminOrderDAO.java

The Client classes are: AdminRequestProcessor.java ManageOrdersBean.java