Assignment 3: 1) What Are The Different Types of EJB Explain With Example? Ans
Assignment 3: 1) What Are The Different Types of EJB Explain With Example? Ans
Ans: There are three types of EJBs: session beans, entity beans, and message-driven beans.
A session bean implements one or more business tasks. A session bean might contain
methods that query and update data in a relational table. Session beans are often used to
implement services. For example, an application developer might implement one or several
session beans that retrieve and update inventory data in a database. Session beans are
transient because they do not survive a server crash or a network failure. If, after a crash,
you instantiate a bean that had previously existed, the state of the previous instance is not
restored. State can be restored only to entity beans.
An entity bean is a complex business entity. An entity bean models a business entity or
models multiple actions within a business process. Entity beans are often used to facilitate
business services that involve data and computations on that data. For example, an
application developer might implement an entity bean to retrieve and perform computation
on items within a purchase order. Your entity bean can manage multiple, dependent,
persistent objects in performing its necessary tasks. An entity bean is a remote object that
manages persistent data, performs complex business logic, potentially uses several
dependent Java objects, and can be uniquely identified by a primary key. Entity beans are
normally coarse-grained persistent objects, because they utilize persistent data stored
within several fine-grained persistent Java objects.
Stateless Session Beans--Stateless session beans do not share state or identity between
method invocations. They are useful mainly in middle-tier application servers that
provide a pool of beans to process frequent and brief requests.
Stateful Session Beans--Stateful session beans are useful for conversational sessions, in
which it is necessary to maintain state, such as instance variable values or transactional
state, between method invocations. These session beans are mapped to a single client for
the life of that client.
Stateless Session Beans
A stateless session bean does not maintain any state for the client. It is strictly a single
invocation bean. It is employed for reusable business services that are not connected to any
specific client, such as generic currency calculations, mortgage rate calculations, and so on.
Stateless session beans may contain client-independent, read-only state across a call.
Subsequent calls are handled by other stateless session beans in the pool. The information is
used only for the single invocation. The EJB container maintains a pool of these stateless beans
to service multiple clients. An instance is taken out of the pool when a client sends a request.
There is no need to initialize the bean with any information.
Stateful Session Beans
A stateful session bean maintains its state between method calls. Thus, there is one instance
of a stateful session bean created for each client. Each stateful session bean contains an
identity and a one-to-one mapping with an individual client. The state of this type of bean is
maintained across several calls through serialization of its state, called passivation. This is
why the state that you passivate must be serializable. However, this information does not
survive system crashes. To maintain state for several stateful beans in a pool, it serializes the
conversational state of the least recently used stateful bean to a secondary storage. When the
bean instance is requested again by its client, the state is activated to a bean within the pool.
Thus, all resources are used performant, and the state is not lost.
3) What are entity beans? Why they are used?
Ans: An entity bean is a complex business entity. An entity bean models a business entity or
models multiple actions within a business process. Entity beans are often used to facilitate
business services that involve data and computations on that data. For example, an
application developer might implement an entity bean to retrieve and perform computation
on items within a purchase order. Your entity bean can manage multiple, dependent,
persistent objects in performing its necessary tasks. An entity bean is a remote object that
manages persistent data, performs complex business logic, potentially uses several dependent
Java objects, and can be uniquely identified by a primary key. Entity beans are normally
coarse-grained persistent objects, because they utilize persistent data stored within several
fine-grained persistent Java objects.
Entity beans are often used to facilitate business services that involve data and computations
on that data.