D.3 Business Tier Antipatterns

Everything Is an EJB

Mistake

EJBs are used when they provide few benefits, leading to an unnecessarily bloated or complex application.

Watch for It When

EJB features such as CMP, transactions, or security are not needed, few clients access the business tier, entity beans are simply a wrapper to an existing database, session beans are used as a gateway to other remote services, or business services do not need to be distributed.

Solution

Often entity beans can be replaced with local commands or JavaBeans working in conjunction with DAOs. Direct database access, flat files, and JDO can also provide lighter-weight persistence mechanisms than entity EJBs.

Session EJBs can often be replaced with business delegates or other, non-EJB façades.

Round-Tripping

Mistake

Frequent calls to remote EJB getter and setter methods slow down the entire application and saturate the network.

Watch for It When

Remote entity EJBs are used directly, get or set methods of remote EJBs are called frequently, especially when iterating over a collection of EJBs (Figure D-1).

Figure D-1. Round-tripping
figs/j2ee_ad01.gif

Solution

Refactor remote entity EJB calls to use a facade and a DTO. Use a session EJB as a façade to copy the entity EJBs into DTOs before they are sent across the network. Set values by sending back modified DTOs.

Stateful When Stateless Will Do

Mistake

A stateful session EJB is used unnecessarily, causing much higher overhead for the EJB server.

Watch for It When

Using any stateful session EJBs, arguments are passed into the create method of a session EJB to avoid passing the data later, or session EJBs are used specifically to store state, such as in a shopping cart.

Solution

Convert stateful into stateless by passing all state with every call to the bean. If there are too many arguments, consider using a client-side adaptor to store them. Store other persistent data (even if it is not permanent) using entity EJBs.



J2EE Design Patterns
J2EE Design Patterns
ISBN: 0596004273
EAN: 2147483647
Year: 2006
Pages: 113

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net