- Business Tier Design Considerations
- Business and Integration Tiers Bad Practices
Business and Integration Tiers Bad Practices
Mapping the Object Model Directly to the Entity Bean Model
Problem Summary
One of the common practices in designing an EJB application is to map the object model directly into entity beans; that is, each class in the object model is transformed into an entity bean. This results in a large number of fine-grained entity beans.
The container and network overhead increases as the number of enterprise beans increases. Such mapping also transforms object relationships into entity-bean-to-entity-bean relationships. This is best avoided, since entity-bean-to-entity-bean relationships introduce severe performance implications for remote entity beans.
Solution Reference
Identify the parent-dependent object relationships in the object model and design them as coarse-grained entity beans. This results in fewer entity beans, where each entity bean composes a group of related objects from the object model.
-
Refactoring See “Reduce Inter-Entity Bean Communication” on page 98.
-
Pattern See Composite Entity (391)
Consolidate related workflow operations into session beans to provide a uniform coarse-grained service access layer.
-
Refactoring See “Merge Session Beans” on page 96.
-
Pattern See Session Façade (341)
Mapping the Relational Model Directly to the Entity Bean Model
Problem Summary
When designing an EJB model, it is bad practice to model each row in a table as an entity bean. While entity beans are best designed as coarse-grained objects, this mapping results in a large number of fine-grained entity beans, and it affects scalability.
Such mapping also implements inter-table relationships that is, primary key/foreign key relationships) as entity-bean-to-entity-bean relationships.
Solution Reference
Design your enterprise bean application using an object-oriented approach instead of relying on the preexisting relational database design to produce the EJB model.
-
Bad Practice See solution reference for “Mapping the Object Model Directly to the Entity Bean Model” on page 53.
Avoid inter-entity relationships by designing coarse-grained business objects by identifying parent-dependent objects.
-
Refactoring See “Reduce Inter-Entity Bean Communication” on page 98.
-
Refactoring See “Move Business Logic to Session” on page 100.
-
Pattern See Composite Entity (391)
Mapping Each Use Case to a Session Bean
Problem Summary
Some designers implement each use case with its own unique session bean. This creates fine-grained controllers responsible for servicing only one type of interaction. The drawback of this approach is that it can result in a large number of session beans and significantly increase the complexity of the application.
Solution Reference
Apply the Session Façade pattern to aggregate a group of the related interactions into a single session bean. This results in fewer session beans for the application, and leverages the advantages of applying the Session Façade pattern.
-
Refactoring See “Merge Session Beans” on page 96.
-
Pattern See Session Façade (341)
Exposing All Enterprise Bean Attributes via Getter/Setter Methods
Problem Summary
Exposing each enterprise bean attribute using getter/setter methods is a bad practice. This forces the client to invoke numerous fine-grained remote invocations and creates the potential to introduce a significant amount of network chattiness across the tiers. Each method call is potentially remote and carries with it a certain network overhead that impacts performance and scalability.
Solution Reference
Use a value object to transfer aggregate data to and from the client instead of exposing the getters and setters for each attribute.
-
Pattern See Transfer Object (415).
Embedding Service Lookup in Clients
Problem Summary
Clients and presentation-tier objects frequently need to look up the enterprise beans. In an EJB environment, the container uses JNDI to provide this service.
Putting the burden of locating services on the application client can introduce a proliferation of lookup code in the application code. Any change to the lookup code propagates to all clients that look up the services. Also, embedding lookup code in clients exposes them to the complexity of the underlying implementation and introduces dependency on the lookup code.
Solution Reference
Encapsulate implementation details of the lookup mechanisms using a Service Locator (315).
-
Pattern See Service Locator (315)
Encapsulate the implementation details of business tier-components, such as session and entity beans, using Business Delegate (302). This simplifies client code since they no longer deal with enterprise beans and services. Business Delegate (302) can in turn use the Service Locator (315).
-
Refactoring See “Introduce Business Delegate” on page 94.
-
Pattern See Business Delegate (302).
Using Entity Beans as Read-Only Objects
Problem Summary
Any entity bean method is subject to transaction semantics based on its transaction isolation levels specified in the deployment descriptor. Using an entity bean as a read-only object simply wastes expensive resources and results in unnecessary update transactions to the persistent store. This is due to the invocation of the ejbStore() methods by the container during the entity bean's life cycle. Since the container has no way of knowing if the data was changed during a method invocation, it must assume that it has and invoke the ejbStore() operation. Thus, the container makes no distinction between read-only and read-write entity beans. However, some containers may provide read-only entity beans, but these are vendor proprietary implementations.
Solution Reference
Encapsulate all access to the data source using Data Access Object (462) pattern. This provides a centralized layer of data access code and also simplifies entity bean code.
-
Pattern See Data Access Object (462).
Implement access to read-only functionality using a session bean, typically as a Session Façade that uses a DAO.
-
Pattern See Session Façade (341)
You can implement Value List Handler (444) to obtain a list of Transfer Objects (415).
-
Pattern See Value List Handler (444).
You can implement Transfer Objects (415) to obtain a complex data model from the business tier.
-
Pattern See Transfer Object Assembler (433).
Using Entity Beans as Fine-Grained Objects
Problem Summary
Entity beans are meant to represent coarse-grained transactional persistent business components. Using a remote entity bean to represent fine-grained objects increases the overall network communication and container overhead. This impacts application performance and scalability.
Think of a fine-grained object as an object that has little meaning without its association to another object (typically a coarse-grained parent object). For example, an item object can be thought of as a fined-grained object because it has little value until it is associated with an order object. In this example, the order object is the coarse-grained object and the item object is the fine-grained (dependent) object.
Solution Reference
When designing enterprise beans based on a preexisting RDBMS schema,
-
Bad Practice See “Mapping the Relational Model Directly to the Entity Bean Model” on page 53.
When designing enterprise beans using an object model,
-
Bad Practice See “Mapping the Object Model Directly to the Entity Bean Model” on page 53.
Design coarse-grained entity beans and session beans. Apply the following patterns and refactorings that promote coarse-grained enterprise beans design.
-
Pattern See Composite Entity (391).
-
Pattern See Session Façade (341).
-
Refactoring See “Reduce Inter-Entity Bean Communication” on page 98.
-
Refactoring See “Move Business Logic to Session” on page 100.
-
Refactoring See “Business Logic in Entity Beans” on page 50.
-
Refactoring See “Merge Session Beans” on page 96.
Storing Entire Entity Bean-Dependent Object Graph
Problem Summary
When a complex tree structure of dependent objects is used in an entity bean, performance can degrade rapidly when loading and storing an entire tree of dependent objects. When the container invokes the entity bean's ejbLoad() method, either for the initial load or for reloads to synchronize with the persistent store, loading the entire tree of dependent objects can prove wasteful. Similarly, when the container invokes the entity bean's ejbStore() method at any time, storing the entire tree of objects can be quite expensive and unnecessary.
Solution Reference
Identify the dependent objects that have changed since the previous store operation and store only those objects to the persistent store.
-
Pattern See Composite Entity (391) and Store Optimization (Dirty Marker) Strategy (397).
Implement a strategy to load only data that is most accessed and required. Load the remaining dependent objects on demand.
-
Pattern See Composite Entity (391) and Lazy Loading Strategy on page396.
By applying these strategies, it is possible to prevent loading and storing an entire tree of dependent objects.
Exposing EJB-related Exceptions to Non-EJB Clients
Problem Summary
Enterprise beans can throw business application exceptions to clients. When an application throws an application exception, the container simply throws the exception to the client. This allows the client to gracefully handle the exception and possibly take another action. It is reasonable to expect the application developer to understand and handle such application-level exceptions.
However, despite employing such good programming practices as designing and using application exceptions, the clients may still receive EJB-related exceptions, such as a java.rmi.RemoteException. This can happen if the enterprise bean or the container encounters a system failure related to the enterprise bean.
The burden is on the application developer, who may not even be aware of or knowledgeable about EJB exceptions and semantics, to understand the implementation details of the non-application exceptions that may be thrown by business tier-components. In addition, non-application exceptions may not provide relevant information to help the user rectify the problem.
Solution Reference
Decouple the clients from the business tier and hide the business-tier implementation details from clients, using business delegates. Business delegates intercept all service exceptions and may throw an application exception. Business delegates are plain Java objects that are local to the client. Typically, business delegates are developed by the EJB developers and provided to the client developers.
-
Refactoring See “Introduce Business Delegate” on page 94.
-
Pattern See Business Delegate (302).
Using Entity Bean Finder Methods to Return a Large Results Set
Problem Summary
Frequently, applications require the ability to search and obtain a list of values. Using an EJB finder method to look up a large collection of entity beans will return a collection of remote references. Consequently, the client has to invoke a method on each remote reference to get the data. This is a remote call and can become very expensive, especially impacting performance, when the caller invokes remote calls on each entity bean reference in the collection.
Solution Reference
Implement queries using session beans and DAOs to obtain a list of Transfer Objects (415) instead of remote references. Use a DAO to perform searches instead of EJB finder methods.
-
Pattern See Value List Handler (444).
-
Pattern See “Data Access Object” on page462.
Client Aggregates Data from Business Components
Problem Summary
The application clients (in the client or presentation tier) typically need the data model for the application from the business tier. Since the model is implemented by business components—such as entity beans, session beans, and arbitrary objects in the business tier—the client must locate, interact with, and extract the necessary data from various business components to construct the data model.
These client actions introduce network overhead due to multiple invocations from the client into the business tier. In addition, the client becomes tightly coupled with the application model. In applications where there are various types of clients, this coupling problem multiplies: A change to the model requires changes to all clients that contain code to interact with those model elements comprised of business components.
Solution Reference
Decouple the client from model construction. Implement a business-tier component that is responsible for the construction of the required application model.
-
Pattern See Transfer Object Assembler (433).
Using Enterprise Beans for Long-Lived Transactions
Problem Summary
Enterprise beans (pre-EJB 2.0) are suitable for synchronous processing. Furthermore, enterprise beans do well if each method implemented in a bean produces an outcome within a predictable and acceptable time period.
If an enterprise bean method takes a significant amount of time to process a client request, or if it blocks while processing, this also blocks the container resources, such as memory and threads, used by the bean. This can severely impact performance and deplete system resources.
An enterprise bean transaction that takes a long time to complete potentially locks out resources from other enterprise bean instances that need those resources, resulting in performance bottlenecks.
Solution Reference
Implement asynchronous processing service using a message-oriented middleware (MOM) with a Java Message Service (JMS) API to facilitate long-lived transactions.
-
Pattern See “Service Activator” on page496.
Stateless Session Bean Reconstructs Conversational State for Each Invocation
Problem Summary
Some designers choose stateless session beans to increase scalability. They may inadvertently decide to model all business processes as stateless session beans even though the session beans require conversational state. But, since the session bean is stateless, it must rebuild conversational state in every method invocation. The state may have to be rebuilt by retrieving data from a database. This completely defeats the purpose of using stateless session beans to improve performance and scalability and can severely degrade performance.
Solution Reference
Analyze the interaction model before choosing the stateless session bean mode. The choice of stateful or stateless session bean depends on the need for maintaining conversational state across method invocations in stateful session bean versus the cost of rebuilding the state during each invocation in stateless session bean.
-
Pattern See Transfer Object Assembler (433), Stateless Session Façade Strategy on page 345, and Stateful Session Façade Strategy on page 345.
-
Design See “Session Bean—Stateless Versus Stateful” on page 46 and “Storing State on the Business Tier” on page 48.