You are on page 1of 3

Assignment 3

1) What are the different types of EJB explain with example?

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.

Eg: public interface javax.ejb.SessionBean extends javax.ejb.EnterpriseBean {


public abstract void ejbActivate();
public abstract void ejbPassivate();
public abstract void ejbRemove();
public abstract void setSessionContext(SessionContext ctx);
}

 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.

Eg: public interface javax.ejb.EntityBean extends javax.ejb.EnterpriseBean {


public abstract void ejbActivate();
public abstract void ejbLoad();
public abstract void ejbPassivate();
public abstract void ejbRemove();
public abstract void ejbStore();
public abstract void setEntityContext(EntityContext ctx);
public abstract voic unsetEntityContext();
}

 Message-Driven Beans (MDB) provide an easier method to implement asynchronous


communication than using straight JMS. MDBs were created to receive asynchronous JMS
messages. The container handles much of the setup required for JMS queues and topics. It
sends all messages to the interested MDB. Previously, EJBs could not send or receive JMS
messages. It took creating MDBs for an EJB-type object to receive JMS messages. This
provides all of the asynchronous and publish/subscribe abilities to an enterprise object that
is able to be synchronous with other Java objects. The purpose of an MDB is to exist within
a pool and to receive and process incoming messages from a JMS queue. The container
invokes a bean from the queue to handle each incoming message from the queue. No object
invokes an MDB directly: all invocation for an MDB comes from the container. After the
container invokes the MDB, it can invoke other EJBs or Java objects to continue the
request. A MDB is similar to a stateless session bean because it does not save
conversational state and is used for handling multiple incoming requests. Instead of
handling direct requests from a client, MDBs handle requests placed on a queue.

2) what are the types of Session beans Explain.

Ans: There are two types of session beans:

 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.

4) Explain ORM (Object Relational Mapping) as working principle behind


EJB Entity Beans.
Ans: Object/relational mapping (ORM) is a solution of the object/relational paradigm
mismatch gaining an increasing acceptance at the moment. It can be defined as the automated
persistence of objects in a Java application to the tables in a relational database, using metadata
that describes the mapping between the objects and the database (“Hibernate in action”). ORM
technique bridges between object and relational models of data. It means that each class
represents a certain database entity, and each record will be a class object. The major advantage
of ORM is that it releases programmers from the tedious work of writing SQL by hand, which
saves time and makes the code more readable and, hence, maintainable. Applications using
ORM middleware, can be expected to be cheaper, more performant, less vendor specific, and
more adaptable to the changes in internal objects or SQL schemas. There is a range of tools
(mappers) implementing ORM technology. The full-featured mappers are expected to support
different databases, provide certain optimization techniques, define mapping metadata,
persistent class appearance and lifecycle of persistent objects, address concurrency,
inheritance, polymorphism, transaction and cache management problems and implement other
functionality which is difficult to hand-code in real terms. Hibernate is an open-source ORM
implementation providing core and additional functionality of a full-featured mapper. It’s free
and not so complex (to get started), which makes it widely acceptable as a decent persistence
solution. In EJB, persistence is implemented by Entity beans. Until recently this technology
(EJB 2.1) has been less successful, basically, because of the excessive complexity and
questionable performance of EJB Entity beans. At the moment the specification of EJB 3.0 is
available which aims at simplicity, standardization and extension of functionality.

You might also like