EJB Interview Questions and Answers for experienced

100 EJB Interview Questions and Answers
  1. What is EJB?

    • Answer: Enterprise JavaBeans (EJB) is a server-side component architecture for developing and deploying enterprise-level applications. It simplifies the development of distributed, transactional, and secure applications by providing a framework for managing the complexities of enterprise systems. EJB components run within an EJB container, which provides services like transaction management, security, and persistence.
  2. Explain the different types of EJBs.

    • Answer: EJB 3.x and later primarily defines Session Beans (Stateful, Stateless, Singleton), Message-Driven Beans (MDBs), and Entity Beans (although JPA is now the preferred persistence mechanism). Stateful session beans maintain conversational state across multiple method calls from a client. Stateless session beans do not maintain client-specific state. Singleton session beans provide a single instance of a bean across the application. Message-driven beans process messages asynchronously, usually from JMS queues or topics. Entity beans (though less common now) represent persistent data.
  3. What is a Stateful Session Bean?

    • Answer: A Stateful Session Bean maintains conversational state for a specific client across multiple method calls. The bean's instance is associated with a particular client, and its data persists for the duration of the client's interaction. When the client disconnects, the state is typically removed. Use this when you need to preserve conversation context.
  4. What is a Stateless Session Bean?

    • Answer: A Stateless Session Bean doesn't maintain client-specific state. Each method invocation is treated independently; there's no association between subsequent calls from the same client. This offers better scalability and concurrency as instances can be pooled and reused.
  5. What is a Singleton Session Bean?

    • Answer: A Singleton Session Bean ensures only one instance of the bean exists across the entire application. It's useful for tasks requiring application-wide shared resources or state (e.g., caching, counters).
  6. What is a Message-Driven Bean (MDB)?

    • Answer: An MDB is an asynchronous component that receives messages from JMS queues or topics. It's ideal for handling time-consuming tasks or decoupling components. It doesn't have client-specific state.
  7. Explain the role of the EJB container.

    • Answer: The EJB container is the runtime environment that manages EJB components. It provides essential services like transaction management, security, concurrency control, persistence, resource pooling, and deployment.
  8. What are EJB deployment descriptors?

    • Answer: Deployment descriptors (usually `ejb-jar.xml`) provide metadata about EJB components, such as their transaction attributes, security roles, and resource references. They instruct the container on how to deploy and manage the beans.
  9. How does EJB handle transactions?

    • Answer: EJB uses declarative transaction management through annotations or XML descriptors. This specifies transaction attributes (e.g., Required, RequiresNew, Mandatory, Supports, NotSupported) that determine how the container handles transactions for bean methods. The container ensures ACID properties (Atomicity, Consistency, Isolation, Durability).
  10. Explain different transaction attributes in EJB.

    • Answer: `Required`: A transaction is required; if one doesn't exist, a new one is started. `RequiresNew`: A new transaction is always started, regardless of the existence of a surrounding transaction. `Mandatory`: A transaction must already exist; otherwise, an exception is thrown. `NotSupported`: The method runs without a transaction; if one exists, it's suspended. `Supports`: The method runs within an existing transaction, but if one doesn't exist, it runs without a transaction. `Never`: A transaction must not exist; otherwise, an exception is thrown.
  11. What is the difference between local and remote interfaces in EJB?

    • Answer: Local interfaces are used for beans within the same JVM as the client. They offer better performance since there's no need for remote communication. Remote interfaces are used for beans in different JVMs; communication happens via RMI or other remote protocols.
  12. How do you handle concurrency in EJB?

    • Answer: EJB containers manage concurrency for stateless beans by pooling instances. For stateful beans, the container ensures that only one client accesses a particular instance at a time. You might also use synchronization mechanisms within bean methods for specific concurrency needs.
  13. Explain the role of interceptors in EJB.

    • Answer: Interceptors allow you to add cross-cutting concerns to EJBs without modifying their core logic. This is useful for logging, auditing, or security checks. Interceptors are invoked before and/or after bean methods.
  14. How are EJBs deployed?

    • Answer: EJBs are packaged into JAR files (typically `ejb-jar`) and deployed to an application server. The application server's deployment tools handle the process of installing and configuring the beans within the EJB container.
  15. What are dependency injection and inversion of control in EJB?

    • Answer: Dependency Injection (DI) is a design pattern where dependencies are provided to a bean rather than the bean creating them. Inversion of Control (IoC) is a broader principle where the container manages the lifecycle and dependencies of beans. EJB uses DI extensively through annotations like `@Inject`.
  16. How do you manage persistence in EJB?

    • Answer: While Entity Beans were used historically, using Java Persistence API (JPA) is the recommended approach for persistence. JPA provides an object-relational mapping (ORM) framework to interact with databases.
  17. What are the advantages and disadvantages of using EJB?

    • Answer: Advantages: Transaction management, security, concurrency control, resource pooling, simplified development for enterprise applications. Disadvantages: Can be complex to learn, potential performance overhead (though optimized in modern versions), can be tightly coupled to the application server.
  18. Explain the concept of EJB timers.

    • Answer: EJB timers allow scheduling the execution of bean methods at specific times or intervals. This is useful for background tasks or scheduled jobs.
  19. How do you handle exceptions in EJB?

    • Answer: Use try-catch blocks within bean methods to handle exceptions. Consider using declared exceptions in method signatures for exceptions that should be handled by clients. Application servers might also provide logging and monitoring of exceptions.
  20. What is a CMP bean (Container Managed Persistence)?

    • Answer: CMP beans were used in earlier versions of EJB to handle persistence. The container managed the persistence logic. This approach is largely outdated with JPA taking its place.

Thank you for reading our blog post on 'EJB Interview Questions and Answers for experienced'.We hope you found it informative and useful.Stay tuned for more insightful content!