Java Reference
In-Depth Information
EJBs running in EJB containers (the supporting software that wraps around the
EJBs) represent the best possible environment for developing the middle tier for ro-
bust, scalable applications. EJBs are written in Java. They are independent of the
platform and the operating system. They are able to scale horizontally (inside a sin-
gle system or across multiple systems) and vertically (using multiple systems for in-
dividual deployment tiers).
T
HE
D
IFFERENT
K
INDS OF
EJB
S
EJBs come in three flavors:
SessionBeans
,
EntityBeans
, and
MessageBeans
.
Session-
Beans
are intended for storing session-related data or may be used as a simple remote
API.
EntityBeans
are representations of persistent objects usually in databases.
Mes-
sageBeans
are activated in response to messages and are intended to provide the logic
for processing the message.
SessionBeans
can be either stateless or stateful. A
stateless
SessionBean
is simply
a remote API. It has no knowledge of you before you invoke its methods and it
keeps no memory of you after you invoke it. In the client, you create references to
access it, you call its methods passing arguments, and you get back a result. This
type of EJB might be used for a complex calculation or, perhaps, to perform a very
simple database operation that does not need any transaction logic, such as access-
ing commonly reused data or values.
For a
stateful
SessionBean
, the word
session
is truly appropriate. When you cre-
ate a reference to a stateful
SessionBean
in a client, a parallel copy of the
Session-
Bean
is created in the EJB container. One client, one bean. As you interact with the
SessionBean
, you can change its internal variables and the
SessionBeans
maintains
this state from one call to another. Although stateful
SessionBean
could be used to
represent database objects, they might more commonly be used to represent tem-
porary working copies of data that might or might not be eventually committed to
more persistent storage. For example, a stateful
SessionBean
might be created to
represent a shopping cart. The application might store and remove items in the
cart. At some point, the application might discard the cart or save it to the database,
possibly using an
EntityBean
to do so.
An
EntityBean
is intended to be a representation of a persistent object that is
usually stored in a database. If you like, you can code the logic to retrieve and store
the object (bean-managed persistence), or you can let the EJB container do the
work for you (container-managed persistence).
EntityBeans
extend the
EntityBean
parent class. Unless you are using con-
tainer-managed persistence, an
EntityBean
will require additional methods for cre-
ating new entities (which you can think of as rows in a database), saving and