EJB Interview Questions and Answers
EJB 2.0 vs EJB 3.0 | EJB 3.0 vs Spring | EJB 1.0 vs EJB 2.0 |
Ques 1. What do you mean by EJB?
Enterprise java bean (EJB) is a server side component which runs on application server or we call container, developed for the purpose of distributed and enterprise level application.
Container will provide support for system level services like Transaction Management, security which make developer task easy and he can focus on business logic.
Ques 2. What are the different types of EJB?
Mainly three types of EJB:
- Entity Bean
- Session Bean
- Message Driven Bean(MDB)
Types of EJB:(1) Entity Bean: It represents an entity which is mapped with database or we can say it makes OR object Relational mapping with Database. Entity bean typically represent table in RDBMS and each instance represent row in the table.
Two types of entity bean:- CMP Entity bean: Container managed entity bean its responsibility of container to manage the bean persistence behavior.
- BMP Entity bean: Programmer manage the bean persistence behavior.
(2) Session bean: Session bean is responsible for developing business logic it makes the client server relationship so session beans exist till there is a session exist between client and server, it doesn't contain persistent business concept.
Types of session bean:
- Stateless session bean: When there is not need to maintain state of a particular client stateless session bean is used .They alive for short period of time. For example if we are validating the credit card we can use stateless session bean.
- Stateful session bean: Stateful session bean maintain the conversational state of client over the series of method call before the bean instance goes to passive state conversational state is saved to persistence area like Hard disk and again when same client send a request and bean instance come into the active state it will come out from hard disk to main memory. For Example when we do online banking transaction ,online reservation we use stateful session bean.
(3) Message Driven Beans: These beans are work as a listener for messaging services like JMS.
Ques 3. Explain the life cycle method of EJB?
Life Cycle of Entity Bean:
- First stage is Does Not Exist Stage then Container creates the instance of EJB and call SetEntityContext() method which will set all entity context to bean and now it will become available on pool, to get a particular identity of an EJB object it has to move from Pooled stage to ready stage which is done by calling the create() method which in turns call ejbCreate() and ejbPostCreate() method.
- There is another way by which directly entity bean can move to pooled stage to ready stage that's is call ejbActivate() method. Now we are ready to invoke the business method of entity bean .After completion of business method if we want to move again in pooled stage from ready stage we can call remove() method which in turns call ejbRemove() or directly call ejbPassivate() method.
- At the end container remove the instance of EJBfrom pool and call unSetEntityContext().
Life Cycle of Stateful Session Bean:- Stateful session beans life cycle starts when client call create() method.The container create the instance of session bean and call setSessionContext() and ejbCreate() method.
- Now the stateful session bean is ready to serve the client request after serving the request if it is not used after a long time container can move this bean to passive stage by calling the ejbPassivate() method.similarly when bean is in passive stage and client invoke the business method the container call ejbActivate() method to move bean from passive stage to active or ready stage.
- At the end of life cycle client call remove() method and container will call ejbRemove() method and bean is ready for garbage collection.
Life Cycle of Stateless session bean: - Stateless session bean has short life cycle it can have two stage does not exist and ready stage. ejb container create the instance of stateless session bean and call setSessionContext() and ejbCreate() method.Now the bean is ready to invoke business method on this.it will not maintain the state so remove () method is been called after completion of business method which in turns call ejbRemove() and now its ready for garbage collection.
Life cycle of Message Driven bean:- MDBs have same life cycle like stateless session bean. setMessageDrivenContext() method and ejbCreate() method is called by container to create the instance of MDB.now its ready to receive message and at the end of lifecycle client will call remove() method which in turns call ejbRemove() and now its ready for garbage collection.
Ques 4. What is the difference between EAR, JAR and WAR file?
J2EE defines three types of archives:
- Java Archives (JAR): A JAR file encapsulates one or more Java classes, a manifest, and a descriptor. JAR files are the lowest level of archive. JAR files are used in J2EE for packaging EJBs and client-side Java Applications.
- Web Archives (WAR): WAR files are similar to JAR files, except that they are specifically for web applications made from Servlets, JSPs, and supporting classes.
- Enterprise Archives (EAR): An EAR file contains all of the components that make up a particular J2EE application.
Ques 5. What is Session Bean?
A session bean is a non-persistent object that implements some business logic running on the server. One way to think of a session object is as a logical extension of the client program that runs on the server.
Session beans are used to manage the interactions of entity and other session beans,access resources, and generally perform tasks on behalf of the client.
There are
two basic kinds of session bean: stateless and stateful.- Stateless session beans are made up of business methods that behave like procedures; they operate only on the arguments passed to them when they are invoked. Stateless beans are called stateless because they are transient; they do not maintain business state between method invocations. Each invocation of a stateless business method is independent from previous invocations. Because stateless session beans are stateless, they are easier for the EJB container to manage, so they tend to process requests faster and use less resources.
- Stateful session beans encapsulate business logic and state specific to a client. Stateful beans are called 'stateful' because they do maintain business state between method invocations, held in memory and not persistent. Unlike stateless session beans, clients do not share stateful beans. When a client creates a stateful bean, that bean instance is dedicated to service only that client. This makes it possible to maintain conversational state, which is business state that can be shared by methods in the same stateful bean.