Business and Integration Tier Refactorings J2EE

Wrap Entities With Session

Entity beans from the business tier are exposed to clients in another tier.Use a Session Facade to encapsulate the entity beans.

Wrap Entities With Session

Wrap Entities With Session

Motivation

Entity beans are coarse-grained distributed persistent objects. Exposing the entity bean to clients in a different tier results in network overhead and performance degradation. Each client invocation on the entity bean is a remote network method call, which is expensive.

Entity beans mandate container-managed transaction. Exposing the entity bean to the clients may put the burden on the client developer to understand, design, and demarcate transactions when dealing with multiple entity beans. The client developer has to obtain a user transaction from the transaction manager and code the interaction with entity beans to occur within the context of that transaction. Since the client implements the transaction management, it is not possible to use the benefits of container-managed transaction demarcation.

Mechanics

  • Move the business logic to interact with the entity beans out of the application client.
  • Use Extract Class to extract the logic from the client.
  • Use a session bean as a facade to the entity beans.
  • This session bean can contain the entity bean interaction logic and associated workflow logic.
  • Implement session beans to provide a consolidated uniform access layer to the entity beans by applying the Session facade pattern.
  • The number of interactions between the client and the entity beans is now moved into the Session facade in the business tier.
  • Thus, the number of remote method invocations from the client is reduced.
  • Implement transaction logic in session beans if using bean-managed transactions. For container-managed transactions, specify the transaction attributes for the session bean in the deployment descriptor.
  • Since the session bean interacts with the entity beans, the client is no longer responsible for demarcating transactions.
  • Thus, all transaction demarcation is now delegated to either the session bean or the container, depending on whether the designer has chosen user-managed or container-managed transactions.

Introduce Business Delegate

Session beans in the business tier are exposed to clients in other tiers.

Use a business delegate to decouple the tiers and to hide the implementation details.

Introduce Business Delegate

Introduce Business Delegate
Motivation

Session beans are used to implement facades for entity beans. Session beans provide coarse-grained interfaces to usiness services. But, exposing the session bean directly to the application client creates a tight coupling between the application client code and the session bean.

Exposing the session bean to the application client increases the prevalence of session bean calls throughout the client code. Thus,any change to the session bean interface impacts every point in the application client code where the session bean is called,and thus creates highly brittle code. The clients also are exposed to service level exceptions encountered when dealing with enterprise beans. This effect is further exaggerated if you consider applications with different types of clients, where each such client uses the session bean interface to obtain some service.

Mechanics

  • For each session bean that is directly exposed to clients across the tier, introduce a business delegate.
  • Business delegates are plain Java classes that encapsulate the business tier details and intercept service level exceptions on behalf of the client.
  • Implement each Business Delegate to deal with its session bean, typically as a facade. A business delegate is designed with a one-to-one relationship with its session facade.
  • Business delegates reduce the coupling between the client tier and the business services (session beans) by hiding the implementation details.
  • The clients deal with the business delegates by invoking methods on them locally.
  • Encapsulate code related to lookup services and caching in business delegates.
  • Business delegates can use a service locator to look up business services.

Merge Session Beans

Create a one-to-one mapping between session bean and entity bean.
Map coarse-grained business services to session beans. Eliminate or combine session beans that act solely as entity bean proxies into session beans that represent coarse-grained business services.

Motivation

A one-to-one mapping of a session bean to an entity bean does not yield any benefits. Such mapping only introduces a layer of session beans acting as proxies. Typically this happens when developers create session beans to front entity beans, rather than to represent coarse-grained services.

Some designers interpret to mean that every entity bean should be protected by its own session bean. This is not a correct interpretation, since it results in design of session beans as proxies rather than as facades.

In Figure , different clients are servicing different interactions. Each interaction involves one or more entity beans. With a one-to-one mapping of a session bean to an entity bean, the client has to interact with each session bean fronting an entity bean. Since the session bean is essentially a proxy to the entity, this scenario is similar to exposing the entity bean directly to the client.

Merge Session Beans

Merge Session Beans

Mechanics

  • Implement session beans as facades to entity beans. Thus, each session bean provides a coarse-grained business service interface to the clients.
  • Consolidate fine-grained session beans or a set of session beans that are proxies to entity beans into a single session bean.
  • Session beans represent coarse-grained business service.
  • Entity beans represent coarse-grained, transactional persistent data.
  • Consolidate a set of related interactions that involve one or more entity beans into a single session facade instead of implementing each interaction using a unique session bean.
  • This results in a fewer number of session beans that provide a uniform coarse-grained business service access to entity beans.
  • The number of Session facades is related to the grouping of interactions and not to the number of entity beans.

Eliminate Inter-Entity Bean Communication

Inter-entity bean relationships introduce overhead in the model.
Reduce or eliminate the inter-entity bean relationships by using coarse-grained entity bean(Composite Entity) with dependent objects.

Eliminate Inter-Entity Bean Communication

Eliminate Inter-Entity Bean Communication

Motivation

Entity beans have significantly more overhead than plain Java objects. Calls to entity bean methods are remote and incur network overhead. Also, entity beans must interact with an external data source.

Even if two entity beans are in the same container, remote method invocation semantics apply(the container is involved in the communication) when one entity bean calls the other bean. Some container implementations may optimize such calls, because they recognize that the call comes from an object within the same container, but this is vendor-specific and cannot be relied upon.

Another issue is the inability for the entity bean to demarcate a transaction. When using entity beans, you are only allowed to have container-managed transactions. This means that, depending on the transaction attribute of the entity bean method, the container may start a new transaction, participate in the current transaction, or do neither. When a client invokes a method on an entity bean, the transaction includes the chain of dependent entity beans and binds them into the transaction's context.This reduces the performance throughput of the entity beans as a whole, because any transaction may lock multiple entity beans and possibly introduce deadlock situations.

Mechanics

  • Design and implement entity beans as coarse-grained objects with root and dependent objects.
  • Transform an entity-bean-to-entity-bean relationship into an entity-bean-to-dependent-object relationship.
  • Dependent objects are not entity beans. Rather, they are objects contained within an entity bean. A relationship between an entity bean and its dependent objects is a local relationship with no network overhead.
  • Optimize load and store operations for Composite Entity using the Lazy Loading Strategy and Store Optimization (Dirty Marker) Strategy respectively.
  • Extract and move business logic related to working with other entities from the entity bean into a session bean.
  • Use Extract Method and/or Move Method to move such business logic into a session bean, applying the Session façade pattern.

Move Business Logic to Session

Inter-entity bean relationships introduce overhead in the model.
Encapsulate the workflow related to inter-entity bean relationships in a session bean (Session Facade).

Move Business Logic to Session

Move Business Logic to Session

Motivation

The problem is that an entity may contain business logic that deals with other entity beans. This creates a direct or indirect dependency on another entity bean. The same problems discussed in Eliminate Inter-Entity Bean Communication apply to this scenario too.

Mechanics

  • Extract and move business logic related to working with other entities from the entity bean into a session bean.
  • Use Extract Method and/or Move Method to move such business logic into a session bean applying the Session façade pattern.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

J2EE Topics