Register Login

Enterprise JavaBeans Interview Questions and Answers

Updated May 19, 2025

What is EJB and what does EJB stand for?

EJB stands for Enterprise JavaBeans. It is a server-side component architecture for modular construction of enterprise applications. It is part of the Jakarta EE platform (formerly Java EE). EJBs are used to encapsulate business logic of an application.

What are some of the important features of EJB?

  • Scalable, portable, distributed, and transactional architecture.
  • Encapsulates only business logic without system-level programming.
  • Platform-independent and written in Java.
  • Lifecycle managed by the EJB container.

What is an EJB container?

An EJB container is a runtime environment (part of an application server) that provides services such as transaction management, security, concurrency, and life cycle management. Examples: GlassFish, JBoss (WildFly), WebLogic.

What are session beans in EJB?

Session beans encapsulate business logic and can be stateful, stateless, or singleton. They are not persistent and are used to serve client requests during a session.

How many types of session beans are available in EJB?

  • Stateless session beans
  • Stateful session beans
  • Singleton session beans

What is stateless and stateful session bean in EJB?

Stateless beans do not maintain any client state across method calls. Stateful beans maintain state across multiple method calls and transactions with a specific client.

What is the use of EJB in a web application?

EJBs provide business logic, transaction management, and security in a web application. They simplify development of enterprise-scale applications.

How to call EJB from Java class?

Use JNDI (Java Naming and Directory Interface) lookup to obtain a reference to an EJB. Example:

Context context = new InitialContext();
MyBeanRemote bean = (MyBeanRemote) context.lookup("java:global/myApp/MyBean");

How is EJB different from JavaBeans?

EJB JavaBeans
Runs on server side and supports transactions, security, etc. Client-side component without enterprise features.
Non-visual, remote objects. Can be visual or non-visual.
Has deployment descriptors or annotations. Uses getter/setter methods and is configured manually.

Why don’t we use static in Java EE EJB?

Using static non-final fields makes EJBs harder to manage in distributed environments, leading to inconsistent state across JVMs.

How to get the primary key value after insert in EJB?

If using JPA with EJB, you can retrieve the primary key after persist using:

entityManager.persist(entity);
Long id = entity.getId();

What are the six transaction attributes that can be assigned to EJB methods?

  • Required
  • RequiresNew
  • NotSupported
  • Mandatory
  • Never
  • Supports

Which EJB protocol is used for communication?

EJB traditionally uses RMI-IIOP (Remote Method Invocation over Internet Inter-ORB Protocol) for communication in distributed environments.

How to check the EJB version?

  • Inspect ejb-jar.xml or application.xml files.
  • Version is usually declared in the root tag attributes.

What is a home interface in EJB?

Home interface is used in EJB 2.x and defines methods for creating, finding, or removing EJB instances. EJB 3.x replaces it with annotations and dependency injection.

Where is ejb-jar.xml located?

  • For JAR files: /META-INF/ejb-jar.xml
  • For WAR files: /WEB-INF/ejb-jar.xml

What is an EJB singleton?

A singleton bean is instantiated once per application and shared among clients. It is useful for caching and shared state scenarios.

Why should we use EJB over Spring?

  • Built-in support for transactions, security, and remoting.
  • Standardized and supported in application servers.
  • Message-driven beans simplify messaging integration.

What is the difference between EJB and J2EE?

J2EE (now Jakarta EE) is a platform that includes various APIs. EJB is a component within that platform, specifically for business logic and enterprise services.

What is EntityManager in EJB?

EntityManager is a JPA interface used in EJBs to manage persistent entities. It supports CRUD operations, transactions, and query execution.

Explain the difference between Context, InitialContext and Session Context.

  • Context: The general JNDI naming interface.
  • InitialContext: Starting point for JNDI operations.
  • SessionContext: Provides access to container-managed information specific to session beans.

What is the difference between EJB 2.x and EJB 3.x?

EJB 3.x introduced a simplified programming model compared to EJB 2.x. It uses annotations, POJO-based development, and eliminates the need for home interfaces and complex deployment descriptors. EJB 3.x also integrates JPA (Java Persistence API) for ORM, while EJB 2.x uses entity beans for persistence.

What is Jakarta EE and how is it related to EJB?

Jakarta EE is the successor of Java EE, governed by the Eclipse Foundation. EJB is part of Jakarta EE, and the package names have been changed from javax.ejb to jakarta.ejb in Jakarta EE 9 and later versions. New projects are encouraged to adopt Jakarta EE packages for future compatibility.

What are Message-Driven Beans (MDB) in EJB?

Message-Driven Beans are a type of EJB used to process asynchronous messages from a queue or topic in a JMS (Java Messaging Service). MDBs are stateless and are typically used for loosely coupled systems requiring asynchronous processing.

What are interceptors in EJB?

Interceptors are used to implement cross-cutting concerns like logging, security, and auditing in EJB. You can define methods annotated with @AroundInvoke to execute custom logic before and after business methods in EJBs.

What is dependency injection in EJB?

EJB supports dependency injection using the @EJB annotation. It allows automatic injection of EJB components into other beans or servlets, promoting loose coupling and easier testing.

How are exceptions handled in EJB?

EJB distinguishes between system exceptions and application exceptions. System exceptions are unchecked and usually cause the container to discard the bean. Application exceptions are checked exceptions defined by the application and can be handled by the caller.

What is Passivation and Activation in Stateful Session Beans?

Passivation is the process of moving a stateful bean from memory to a secondary store when it is idle, while activation is the reverse process of bringing it back into memory when needed. This optimizes memory usage on the server.

What is the difference between container-managed and bean-managed transactions?

In container-managed transactions, the EJB container manages transaction boundaries automatically based on annotations or deployment descriptors. In bean-managed transactions, the developer explicitly manages transaction boundaries using the UserTransaction API.

Can EJBs be used in microservices architecture?

While EJBs can technically be used, they are not typically preferred in microservices architectures due to their heavyweight nature and tight coupling with application servers. Lightweight frameworks like Spring Boot are more commonly used for microservices.

How is security handled in EJB?

EJB uses declarative and programmatic security. Declarative security is handled via annotations like @RolesAllowed, while programmatic security uses EJBContext methods like isCallerInRole(). It relies on JAAS for authentication and authorization.


×