Thursday, March 20, 2008

Interview Questions on Enterprise Java Beans(EJB)

How EJB evolved over the years?

The evolution of EJB can be captured in following points:
1.0:
- No mandate that Entity EJB must be supported by EJB container
- EJB communication backbone uses RMI-JRMP(Java Remote Method Protocol)
- No CMP Entity EJB
- No Deployment Descriptor XML file, used compiled class, then serializes it into flat file (binary).
1.1:
- Entity Bean supported by EJB container
- has CMP, but never describe how to configure relationship between CMP Entity EJBs, this depends on container tool provided by vendor
- RMI-IIOP is suggested for EJB communication
- Deployment Descriptor XML file was introduced
2.x:
- Message Driven Bean is introduced
- With CMP, you can configure the relationship between CMP Entity EJBs, in ejb-jar.xml
- EJB-QL for CMP EntityEJB query language
- RMI-IIOP is a must for EJB communication
3.0:
-No more deployment descriptor,metadata annotations used instead.
-CMP simplified.It is more like JDO or Hibernate.
-Checked exceptions are reduced.RemoteException is no longer mandatory with each remote business method .
-Inheritance is allowed and a bean can extend some base code.
-EJB-QL enhanced.

What are Entity Bean and Session Bean?

An Entity Bean is :
- a persistent business data object in a business process, which provides an OR (Object Relational) mapping over an underlying RDBMS.It is transactional in nature. An in memory objects can be mapped with a field of table in RDBMS database.
-can be expressed as a Noun e.g. a Customer, a Product, an Account, Purchase Order etc.
-long lasting as they represent data in the database.

Entity beans can be of two types:
CMP Entity Bean: EJB container manages data persistence and a bean developer do not have to bother about business data persistence logic.
BMP Entity Bean: A bean managed persistent entity bean is persisted in the database by hand or say programmatically. A component developer has to code to translate in memory objects to underlying RDBMS or object database.

A Session Bean incorporates business logic or business rules or workflow related logic of an enterprise. A session bean exists only until a session exists between a client and server so it has real short period of its existence.

Session beans are of two types:

Stateless Session Bean
In case where state between a client and a server interaction is not to be persisted then Stateless session bean is to be used. e.g. executing calculating tax or validating a credit card etc. The stateless bean instance does not have longevity.
Stateful Session Bean
Stateful Session bean keeps a conversational state of a client over a series of method calls, keeping it in a persistent area. They are more functional than stateless session bean as they store conversational state.

Whenever a client access a stateful session bean with a series of method calls, before moving the bean instance into passivated state, the conversational state is stored into the persistent area i.e. hard disk. This makes smart resource management. Whenever the same client is making a request again over the stateful session bean then state is pulled out of hard disk into in memory of bean. This is called activated state. Thus state of a client is saved in between context switching in a persistent storage area.

How is container managed entity bean created?

A client first locates an EJB over network through JNDI and then gets a home interface reference on the EJB. On home interface the moment create() method is called; EJB container invokes underlying ejbCreate() method with similar method parameters, creating a persistent data in the underlying database.

What is the difference between Container managed persistent and Bean managed persistent entity bean ?

The major difference between CMP and BMP Entity Beans is managing persistence of data through container in case of CMP and programmatically in BMP.
Generally, you should use CMP unless you're forced to use BMP due to limitations of the mapping tools. In case of mapping some relatively complex data models.
CMP is better than BMP as far as performance is concerned as in case of CMP through OR mapping optimization of transaction process is taken care off by container in the best possible manner. While in case of BMPs you can optimize your queries and improve performance over the generalized container-managed heuristics.

What is the difference between ejbCreate() and ejbPostCreate() in EntityBean?

An ejbCreate() method is called just before persisting the data into underlying database in case of Entity beans. It will create a new record in the database. A create () method defined in Home interface will correspond to an ejbCreate () method in bean class with the same arguments.

ejbPostCreate() is called by EjbContainer immediately after ejbCreate() method.This method may also be used to reset transaction related parameters.

What are the services provided by the EJB container ?

The container provides following services:
-Transaction Management
-Persistence
-Security
-Connection Pooling
-Naming services

What are new features of EJB3?

EJB2.x used to be a too complex technology for developers so in EJB 3 a lot has been done to simplify things for end users of the technology and moreover give EJB a new lease of life.In latest edition of EJB the following features have been included:

-Support for Annotations.e.g. the EJB2 callbacks ( such as the ejbActivate() method) are now defined using annotation. For the ejbActivate() method, this is done with the help of @PostActivate annotation. This annotation is set on a method that will be called by the container.Or to define a stateless session bean, the @Stateless annotation is declared on the bean class.

-Introducing 'Business Interceptors' which allows a developer to intercept each business method of a bean.These interceptors are chained and values of parameters and return values can always be changed.

-Persistence in EJB3.0 is majorly simplified by using EntityManager persistence API.The Entity Beans are POJO objects and support inheritance too.It is very similar to Hibernate,JDO

What is Connection Pooling? Is it advantageous?

An EJB container provides the service of connection pooling in order to provide efficient way of database connectivity to its concurrent and continuous user requests. Whenever a client request for a database connection then an instance is picked from the connection pool to get an access to database as soon as user is through with his work instance is returned to connection pool. There is a limit specified by App server administrator for the availability of number of connections and beyond a specified limit a predefined number increases numbers of connection pool instances. When demand goes back to normal then access amount of connection pool instances are removed.

This mechanism of connection pooling helps in smart and efficient use of system resources and improving the overall performance of the whole system.

What's difference between Servlet/JSP session and EJB session?

From a logical point of view, a Servlet/JSP session is similar to an EJB session. Using a session, in fact, a client can connect to a server and maintain his state.
But, is important to understand, that the session is maintained in different ways and, in theory, for different scopes.

A session in a Servlet, is maintained by the Servlet Container through the HttpSession object, that is acquired through the request object. You cannot really instantiate a new HttpSession object, and it doesn't contains any business logic, but is more of a place where to store objects.

A session in EJB is maintained using the SessionBeans. You design beans that can contain business logic, and that can be used by the clients. You have two different session beans: Stateful and Stateless. The first one is somehow connected with a single client. It maintains the state for that client, can be used only by that client and when the client "dies" then the session bean is "lost".

A Stateless Session Bean doesn't maintain any state and there is no guarantee that the same client will use the same stateless bean, even for two calls one after the other. The lifecycle of a Stateless Session EJB is slightly different from the one of a Stateful Session EJB. Is EJB Container's responsibility to take care of knowing exactly how to track each session and redirect the request from a client to the correct instance of a Session Bean. The way this is done is vendor dependant, and is part of the contract.

How is JDO(Java Data Object) different from VO(Value Object) ?

JDO is
- a persistence technology
-allows you to create POJOs (plain old java objects) and persist them to the database
.
Value objects
-usually referred as data holders but the correct term is DTO:
-represent an abstract design pattern used in conjunction with entity beans, JDBC, and possibly even JDO to overcome commonly found isolation and transactional problems in enterprise apps.
- do not allow you to persist objects - they are simple data holders used to transfer data from the database to the client and back to the database.

What is session facade?

A session façade is an EJB design pattern in which a session bean is works like a wrapper over entity beans. A client does not have a direct access to Entity beans but through session beans only for reducing network overhead. Usually stateless session beans are used as single access point for the clients but stateful session beans can also be used for the same purpose. A layer of session beans exposed to clients to access not only gives a clean approach towards client access to bean components but also reduce network calls so as to make the whole system of high performance.


No comments:

Books

  • Hibernate in Action
  • Spring in Action