EJB Interview Questions and Answers

    1 Votes

A great collection of frequently asked EJB Interview Questions with best answers for freshers and 2-6 year experienced java programmers and web developers.

1. What is EJB?

Enterprise Java Beans are the server side components that run inside the EJB container and encapsulates the business logic of an enterprise application.

2. What is a J2EE container?

Containers are the interface between a component and the low level platform specific functionality that supports the component. The application server maintains control and provides services through an interface or framework known as a container.

3. What are the types of J2EE container types according to J2EE specification?

There are five defined J2EE container types (three server side and two client side). Three server side containers are as follows:

  • The server itself, which provides the J2EE runtime environment.
  • An EJB container to manage EJB components.
  • A web container to manage servlets and java server pages.

Two client side containers are as follows:

  • An application container for standalone GUIs, console, and batch type programs.
  • An applet container, meaning a browser, usually with the java plug-in.

4. what is a J2EE component and what are its types?

It is a self-contained functional software unit that is assembled into a J2EE application with its related classes and files and that communicates with other components. There are three types of components: 

  • Client components, which correlate to the client containers. 
  • Web components - Servlets and JSPs. 
  • EJB components - are business components that run on the server, which is logic that solves or meets the needs of a particular business domain such as banking, retail, or finance.

5. what are the characterestics of EJB?

  • It hides server side system level issues from developers.
  • It defines a standard component architecture.
  • It can be easily integrated with other EJB components.
  • It enables you to create components and applications that are reusable across various J2EE-compliant servers.

Example: Online Share Trading, Online Library Application, Online Reservation System.

6. what are the controversies with EJB 2.x?

Controversies With EJB 2.x :

  • It is too heavy-weight Its programming model is complex.
  • It does not let you test the code easily.

7. what are the features of EJB 2.0?

Features of EJB 2.0:

  • Integrated support for JMS
  • Support for local interfaces.
  • It's architecture is improved and supports Container Managed Persistence.
  • It supports container managed relationships among entity beans.
  • Enterprise JavaBeans Query Language (EJB QL).
  • Home methods for entity beans.

8. Explain briefly about the EJB components ?

  • EJB Container : It provides runtime environment for the EJB components and services, such as Life cycle management, connection pooling and security.
  • EJB Component : It is a java class that follows the Enterprise Java Bean specification to implement the business logic of an application. Enterprise Java Bean components are deployed in container and are accessed using the remote and home interfaces of an enterprise bean.
  • EJB Object : Enterprise Java Bean container generates Enterprise Java Bean object, when a client needs to access an instance of the enterprise java bean. The Enterprise Java Bean object acts as an interface between the client and bean instances.
  • Remote Interface : It declares methods, which an enterprise bean provides to clients in order to enable them to perform business operations.
  • Home Object : Home objects are generated by Enterprise Java Bean container. Clients access Enterprise Java Bean objects that are stored at different java virtual machine, using the home object.

Task performed by EJB Object

  • Creating EJB objects
  • Searching EJB Objects
  • Removing EJB Objects

EJB Interfaces

  • Home Interface : It declares the various life cycle methods that involve operations, such as creating, finding, removing enterprise beans.
  • Local Interface : These are used when the enterprise bean and the client machine are stored in the same JVM.

9. what are the system services provided by EJB container?

Container provides system services such as:

  • Persistence
  • Security
  • Transaction
  • Connection pooling
  • Component lifecycle management
  • Threading

10. Mention the steps to call a remote method in EJB?

  • The client calls a local stub that is an object of the remote interface.
  • The stub marshals the parameters of the message into a form that can be sent over the network.
  • The call is sent over the network connection to the skeleton that is present at the server.
  • The skeleton demarshals the parameter in a form suitable for calling the EJB object.
  • The skeleton calls the EJB object that provides the middleware services, such as transaction and security.
  • The EJB object calls the instance of enterprise bean.
  • The Enterprise bean instance performs the tasks that are defined in the called method.

11. Mention the steps to call an EJB using local interface?

The client calls a local object. The local object provides services, such as transactions, connection pooling and security. The local object calls the EJB bean instance, which performs the tasks defined in the method. The EJB instance returns the result and control to the local object, which returns the result to the client.

12. what are EJB design principles?

  • EJB applications are loosely coupled.
  • EJB behavior is specified by interfaces.
  • Implementation is hidden from the client.
  • The container supports the application developer.
  • EJB applications are tiered
    • The session tier is the API to the application.
    • The entity tier is the API to the data sources.

13. Mention the advantages of EJB components?

  • It supports the integration of components from different vendors.
  • Enterprise Java Bean can be authored without a detailed knowledge of the environment.
  • Applications can be assembled from separate components source.
  • An Enterprise Java Beans’s interaction with its clients is specified entirely in terms of Java interfaces.
    • Methods are exposed through interfaces that clients can call, and thereby set out a ‘contract’ between the client and the Enterprise Java Bean. 
    • Enterprise Java Bean application does not maintain resources.
    • Supports portability.

 

14. What are the features of EJB application?
  • Enterprise Java Beans get access to external resources (databases, legacy systems) through their container.
    • Programmer does not have to worry about resource allocation and de-allocation. It is the container’s job to manage there sources, and make the access as efficient as possible.
    • EJB container is configured by system admin not through programming APIs.
  • Defined as business logic ONLY
    • No low-level plumbing.
    • It can be reused across multiple application servers.
    • It implement interfaces that allows EJB container to manage them.
15. what are the fundamentals of EJB architecture?
  • Client view of an Enterprise Java Bean is defined strictly by interfaces.
  • Enterprise Java Beans are isolated and supported by an container.
  • Enterprise Java Bean container manages database transactions.
  • Enterprise Java Bean container manages access and security.
  • Creating and locating Enterprise Java Beans is standardized.
  • Instance can be pooled for efficiency.
  • Container manages resources.
16. How EJB are supported by the container?

EJB method calls from client are intercepted by EJB container before they are delegated to EJB beans. Proxy objects (Home object and EJB object) which are generated by container. Container encapsulates EJB beans and acts as its security manager. EJB container provides system services to EJB beans.

17. How EJB container manages access and security?

  • Container handles access control
    • Which methods are accessible to which roles.
    • Access control is declaratively specified in deployment descriptor.
    • Programmatic access control is allowed.
  • Container also provide authentication scheme
    • Bean provider should never have to code authentication procedures.

18. What EJB architecture contracts are specified in EJB specification?

Contracts are specified in EJB specification.

  • Client view contract
    • Contract between client and container.
  • Component contract
    • Contract between an Enterprise Bean and its Container.

19. what is a client view contract specified in EJB specification?

Client of an EJB can be

  • Web tier components: Servlet and JSP
  • Standalone Java application
  • Applet
  • Another EJB in same or different container
  • Web services client (in EJB 2.1)

Client view contract is comprised of:

  • Home interface for local or remote clients
  • Contains methods for creating and locating beans.
  • Logic Interface (Is called Remote interface)
  • For local or remote clients.
  • Contains business methods.

20. what is component contract specified in EJB specification?

  • Enables EJB method invocations from clients.
  • Manage the life cycle of EJB bean instances.
  • Implements home and remote interfaces.
  • Provide persistence for CMP entity beans.
  • Manage transactions, security, exceptions, etc...
  • Implements callbacks.

21. How client creates and locates EJB?

There is a well-defined way for the client to create new EJBs, or to find existing ones.

  • Client uses JNDI to get a proxy object (actually a reference to stub of a EJB Home object).
  • Client then calls either create() or find() method of the Home object to get another proxy object (actually a reference to stub of a EJB object).
  • Clients always deal with proxy objects – never directly with EJB bean instance.
  • Container may pool bean instances.
    1. Container knows when is the good time to create and remove bean instances.
    2. When a client asks container to create a bean via create() method, the container is likely to return a bean instance from the pool.
    3. This is all transparent to client.

22. What are the types of Enterprise Java Beans?

Session Beans

  • Stateful session beans
  • Stateless session beans

Entity Beans

  • Bean Managed Persistence (BMP)
  • Container Managed Persistence (CMP)
  • Message Driven Beans

23. What is a session bean?

Session bean represents a transient conversation between client and server. When the client is done with the execution, the session bean and its data are gone.


24. What is an entity bean?

An entity bean is about storing persistent data in one row of a database table. When the client terminates or the server shuts down, the underlying services saves the entity bean data.
25. What is a message driven bean?

A message-driven bean includes features of a session bean and a Java Message Service, allowing a business component to receive messages asynchronously.

26. When we should use session bean?
  • Session bean should be used to model workflow, processes or tasks, manage activities (such as making reservation, purchase etc).
  • It should be used specific to a particular client.
  • It should be used to coordinate processes among entity beans.
27. What is a façade design pattern?

A design pattern that unifies many small interfaces into one big simplified one. It is wrapper around the small interfaces. Facade as an "object that provides a single, simplified interface to the more general facilities of a subsystem".

28. What are the types of a session bean?

  • Stateless: execute a request and return a result without saving any client specific state information
    • Transient
    • Temporary piece of business logic needed by a specific client for a limited time span.
  • Stateful: maintains client specific state.

29. What is a stateless session bean?

  • Does not retain client specific state from one method invocation to the next.
  • Bean instance can be reassigned to serve a method.
  • Invocation from another client once current method invocation is done.
  • Value of instance variables of a bean instance is not preserved between calls.

30. Give examples of a stateless session bean?

Catalog

  • No client specific state needs to be preserved.
  • Common catalog data for all clients.

Interest calculator

  • No client specific state needs to be preserved.
  • Common business logic for all clients.

31. Explain the life cycle of a stateless session bean?

This is the pooled state.

In the ready state:

  • EJB Container Invokes the newInstance() method, which creates a new stateless session bean instance by calling the stateless session bean’s default constructor.
  • Invokes the setSessionContext() method to associate the bean’s instance with information about the environment in which bean instance will execute.
  • Invokes the ejbCreate() method defined in the stateless session bean class. The ejbCreate() method for a stateless session bean contains no arguments because a stateless session bean does not store client-specific information. (Only create and remove EJB Objects not instances.)

32. How container reuses stateless session bean instances?

Container transparently reuses bean instances to serve different clients

  • Pool of bean instances are created by container at appropriate time (ex: at the time of system boot or when the size of pool becomes too small).
  • Bean instances are then recycled.
  • Smaller number of bean instances (pool of bean instances) can serve larger number of clients at a single time.

33. Explain briefly about resource usage of stateless session beans?

  • Load-balancing & Failover (between EJB servers) is easier since no state needs to be preserved.
    • Any bean instance in any EJB server can serve any client call
  • High scalability since a client call can be served by any EJB server in a clustered architecture.
  • In order to handle increased number of clients, just add more memory or more EJB servers.

34. What is a home interface?

  • Defines methods for creating, finding and removing beans.
  • Implemented by Container
    • EJB home object.
  • Client gets “reference to stub object of the EJB home object” via JNDI.
  • Can be remote or local.

35. What is a remote or logic interface?

  • Defines business methods
    • Business methods are methods that deals with application specific business logic.
  • Implemented by Container
    • EJB object.
  • Client gets “reference to stub object of the EJB object” through create() or find() method of EJB Home interface.
  • Can be remote or local.

36. What are the advantages and disadvantages of local interface?

Advantages

  • More efficient access due to co-location due to no RMI over IIOP overhead.
  • Ability to share data between client and bean through call by reference.

Disadvantages

  • Tight coupling of client and bean.
  • Less flexibility in distribution.

37. What is EJB-JAR deployment descriptor?

  • Gives the container instructions on how to manage the enterprise bean.
  • Allows declarative customization.
  • Controls behaviors for:
    • Transaction
    • Security
    • Life cycle
    • State management
    • Persistence

38. Explain stateful session bean?

Stateful Session Bean maintains the state of a client. A client starts a conversation with a stateful session bean when it invokes the bean’s method in an EJB object. When the conversation ends, the conversational state is preserved. Stateful session beans are dedicated to a single client resulting in no swapping or pooling of instances.

EJB container controls the life cycle of a stateful session bean. EJB container assigns one stateful session bean instance to serve a single client at a time. It does retain client specific state (session state) from one method invocation to the next. Value of instance variables of a bean instance is preserved between calls from same client.

Bean instances are created on demand for client, and removed afterwards. Bean instances are to be maintained for each client. A client can be in active or inactive state. It is not as scalable as stateless session bean.

39. Give some exmples of stateful session bean?

In an online banking application, a stateful session bean is used for performing banking transactions. A client can deposit and withdraw funds using a stateful session bean method.
  • Shopping cart
  • Client specific state needs to be preserved for each client Items that a user wants to buy.
  • Travel ticket purchasing
  • Client specific state needs to be preserved for each client.
  • Tickets to purchase and then confirm/cancel.
40. what are the life cycle stages of a stateful session bean?

The life cycle of a stateful session bean consists of the following stages:

  • Ready: In this stage, a stateful session bean instance remains in the shared pool and services client requests.
  • Passive: In this stage, the EJB container stores the client state in secondary storage and removes the bean instance from the memory. This is done to conserve resources.
  • Does Not Exist: In this stage, a stateful session bean is permanently removed from the shared pool.
41. In what cases stateful session bean instance enters the ready stage?

A stateful session bean instance enters the Ready stage in the following cases:
  • When EJB container creates a new stateful session bean instance.
  • When EJB container activates a passivated stateful session bean.
42. In what cases stateful session bean instance enters the passive stage?

A stateful session bean enters the Passive stage in its life cycle:
  • When EJB Container releases it from serving an idle client and returns the stateful session bean to the shared pool to service another client’s requests.
  • Before passivation, EJB container saves the client state associated with the stateful session bean instance in a secondary storage device, such as a hard disk in the server.
43. How can we prevent the passivation of certain fields in a stateful session bean?

You can prevent the passivation of certain fields in a stateful session bean by declaring them as transient. The objects that are declared transient are not serialized and are not available when deserialized. EJB container passivates and activates the stateful session bean instances in the shared pool to reduce the overhead of storing a large number of bean instances.

44. What is passivation and activation in EJB?

EJB container maintains a limited number of stateful session bean instances in the shared pool. It saves the bean state of an idle bean instance and stores it in a secondary storage device. This process is called passivation.

When the client of a passivated bean calls another bean method, the bean state is restored. This process is called activation. It is not necessary that a bean state be restored to the same stateful session bean instance, which was servicing the client request before bean passivation.

45. What are the various algorithms used to implement passivation in EJB server?

The implementation of the passivation logic varies from one EJB server to another.Various algorithms used to implement passivation are:

  • Least Recently Used (LRU): Selects the stateful session bean instance, which has been inactive for the longest duration. This is also known as eager passivation.
  • Not Recently Used (NRU): Selects the stateful session bean instance, which has become inactive recently.
  • First In First Out (FIFO): Selects the stateful session bean instance, which had entered the pool first and is inactive. This is also known as lazy passivation.

46. What is just in time algorithm used in EJB server?

Most EJB servers use just-in-time algorithm, also referred to as on demand strategy, for activation of beans. According to this algorithm, a passivated bean is activated when a client decides to continue interaction with the bean instance.
An EJB server maintains a directory for preserving the state of passivated stateful session beans, known as the persistent store directory. The persistent store directory contains one subdirectory for each passivated bean. The implementation details of the persistent store directory may vary from one EJB server to another.

47. In what cases stateful session bean instance enters the ready stage?

At the end of its life cycle, a stateful session bean instance moves into the Does Not Exist stage. A stateful session bean instance enters this stage in the following cases:

  • When EJB container invokes the ejbRemove() method.
  • When the timeout of the bean instance, specified by the bean developer, occurs EJB container destroys stateful session bean instances when the number of instances is more than the number of active client requests.

48. When EJB container fails to call ejbRemove()?

EJB Container fails to call ejbRemove() in the following cases:

  • When EJB container failure occurs.
  • When a stateful session bean method execution throws a system exception.

49. What are the steps to create a stateful session bean?

  • Invokes the newInstance() method, which instantiates a new stateful session bean instance.
  • Invokes the setSessionContext() method to associate the stateful session bean instance with the bean context.
  • Invokes the ejbCreate() method of the stateful session bean class. You can pass arguments to the ejbCreate() method to initialize a new stateful session bean instance. 
  • A stateful session bean can have more than one ejbCreate() method, where each method has a different number and type of arguments.
  • A stateful session bean instance can also move to the Ready stage, when EJB container activates a passivated stateful session bean by invoking the ejbActivate() method.

50. What are the sequence of operations initiated by the client in EJB?

  • Client calls a create() method on the home object.
  • Home object requests a bean instance from a pool, home object returns EJB object to client.
  • Client calls business methods on EJB object.
  • EJB object passes the call to Bean instance.
  • Client calls remove() on the EJB object.
  • EJB object releases bean instance.

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

JSP Interview Questions and Answers
Servlet Interview Questions and Answers
Advanced Servlet Interview Questions and Answers
Spring Interview Questions and Answers