Q : What are the different kinds of enterprise beans?
A :
Session Bean: is a non-persistent object that implements some business logic running on the server. Session
beans do not survive system shut down. There are two types of session beans
�� Stateless session beans (i.e. each session bean can be reused by multiple EJB clients).
�� Stateful session beans (i.e. each session bean is associated with one EJB client).
Entity Bean: is a persistent object that represents object views of the data, usually a row in a database. They
have the primary key as a unique identifier. Multiple EJB clients can share each entity bean. Entity beans can
survive system shutdowns. Entity beans can have two types of persistence
�� Container-Managed Persistence (CMP) - The container is responsible for saving the bean’s state.
�� Bean-Managed Persistence (BMP) – The entity bean is responsible for saving its own state.
Message-driven Bean: is integrated with the Java Message Service (JMS) to provide the ability to act as a
message consumer and perform asynchronous processing between the server and the message producer.
Q : What is the difference between session and entity beans?
A :Session Beans Entity Beans
Use session beans for application logic. Use entity beans to develop persistent object model.
Expect little reuse of session beans. Insist on reuse of entity beans.
Session beans control the workflow and transactions of a
group of entity beans.
Domain objects with a unique identity (i.e.-primary key) shared
by multiple clients.
Life is limited to the life of a particular client. Handle
database access for a particular client.
Persist across multiple invocations. Handles database access
for multiple clients.
Do not survive system shut downs or server crashes.
Do survive system shut downs or server crashes.
Q : What is the difference between stateful and stateless session beans?
A :Stateless Session Beans Stateful Session Bean
Do not have an internal state. Can be reused by different
clients.
Do have an internal state. Reused by the same client.
Need not be activated or passivated since the beans are
pooled and reused.
Need to handle activation and passivation to conserve system
memory since one session bean object per client.
Q : What is the difference between Container Managed Persistence (CMP) and Bean Managed Persistence (BMP) entity beans?
A :Container Managed Persistence (CMP) Bean Managed Persistence (BMP)
The container is responsible for persisting state of the bean. The bean is responsible for persisting its own state.
Container needs to generate database (SQL) calls. The bean needs to code its own database (SQL) calls.
The bean persistence is independent of its database (e.g.
DB2, Oracle, Sybase etc). So it is portable from one data
source to another.
The bean persistence is hard coded and hence may not be
portable between different databases (e.g. DB2, Oracle etc).
Q : Can an EJB client invoke a method on a bean directly?
A : An EJB client should never access an EJB directly. Any access is done through the container. The container will
intercept the client call and apply services like transaction, security etc prior to invoking the actual EJB.
Q : How does an EJB interact with its container and what are the call-back methods in entity beans?
A : EJB interacts with its container through the following mechanisms
�� Call-back Methods: Every EJB implements an interface (extends EnterpriseBean) which defines several
methods which alert the bean to various events in its lifecycle. A container is responsible for invoking these
methods. These methods notify the bean when it is about to be activated, to be persisted to the database, to
end a transaction, to remove the bean from the memory, etc. For example the entity bean has the following
call-back methods:
public interface javax.ejb.EntityBean {
public void setEntityContext(javax.ejb.EntityContext c);
public void unsetEntityContext();
public void ejbLoad();
public void ejbStore();
public void ejbActivate();
public void ejbPassivate();
public void ejbRemove();
}
�� EJBContext: provides methods for interacting with the container so that the bean can request information
about its environment like the identity of the caller, security, status of a transaction, obtains remote reference
to itself etc. e.g. isUserInRole(), getUserPrincipal(), isRollbackOnly(), etc
�� JNDI (Java Naming and Directory Interface): allows EJB to access resources like JDBC connections, JMS
topics and queues, other EJBs etc.
Q : What is the difference between EJB 1.1 and EJB 2.0? What is the difference between EJB 2.x and EJB 3.0?
A : EJB 2.0 has the following additional advantages over the EJB 1.1
�� Local interfaces: These are beans that can be used locally, that means by the same Java Virtual Machine,
so they do not required to be wrapped like remote beans, and arguments between those interfaces are
passed directly by reference instead of by value. This improves performance.
�� ejbHome methods: Entity beans can declare ejbHomeXXX(…) methods that perform operations related to
the EJB component but that are not specific to a bean instance. The ejbHomeXXX(…) method declared in the
bean class must have a matching home method XXXX( …) in the home interface.
�� Message Driven Beans (MDB): is a completely new enterprise bean type, which is designed specifically to
handle incoming JMS messages.
�� New CMP Model. It is based on a new contract called the abstract persistence schema, which will allow the
container to handle the persistence automatically at runtime.
�� EJB Query Language (EJB QL): It is a SQL-based language that will allow the new persistence schema to
implement and execute finder methods. EJB QL also used in new query methods ejbSelectXXX(…), which is
similar to ejbFindXXXX(…) methods except that it is only for the bean class to use and not exposed to the
client (i.e. it is not declared in the home interface)
Let’s look at some of the new features on EJB 2.1
�� Container-managed timer service: The timer service provides coarse-grained, transactional, time-based
event notifications to enable enterprise beans to model and manage higher-level business processes.
�� Web Service support: EJB 2.1 adds the ability of stateless session beans to implement a Web Service
endpoint via a Web Service endpoint interface.
�� EJB-QL: Enhanced EJB-QL includes support for aggregate functions and ordering of results.
Current EJB 2.x model is complex for a variety of reasons:
�� You need to create several component interfaces and implement several unnecessary call-back methods.
�� EJB deployment descriptors are complex and error prone.
�� EJB components are not truly object oriented, as they have restrictions for using inheritance and
polymorphism.
�� EJB modules cannot be tested outside an EJB container and debugging an EJB inside a container is very
difficult.
Q: What are the implicit services provided by an EJB container?
A :
Lifecycle Management: Individual enterprise beans do not need to explicitly manage process allocation,
thread management, object activation, or object destruction. The EJB container automatically manages the
object lifecycle on behalf of the enterprise bean.
State Management: Individual enterprise beans do not need to explicitly save or restore conversational
object state between method calls. The EJB container automatically manages object state on behalf of the
enterprise bean.
Security: Individual enterprise beans do not need to explicitly authenticate users or check authorization
levels. The EJB container automatically performs all security checking on behalf of the enterprise bean.
Transactions: Individual enterprise beans do not need to explicitly specify transaction demarcation code to
participate in distributed transactions. The EJB container can automatically manage the start, enrolment,
commitment, and rollback of transactions on behalf of the enterprise bean.
Persistence: Individual enterprise beans do not need to explicitly retrieve or store persistent object data from
a database. The EJB container can automatically manage persistent data on behalf of the enterprise bean.
EJB Interview Questions
Monday, July 20, 2009
|
digg it
Leave your comment
Posted by Sitesh Gupta
No Responded To This Post
Leave A Reply