EJB Interview Questions and Answers

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 in Java. It simplifies the development of distributed, transactional, and secure applications by providing a standardized framework for managing business logic.
  2. What are the advantages of using EJB?

    • Answer: Advantages include simplified development, improved scalability and performance, enhanced security, robust transaction management, and platform independence.
  3. What are the different types of EJBs?

    • Answer: The main types are Session Beans (Stateful, Stateless, Singleton), Message-Driven Beans (MDBs), and Entity Beans (now largely replaced by JPA).
  4. Explain Stateful Session Beans.

    • Answer: Stateful Session Beans maintain conversational state with the client. Each instance is associated with a single client and retains data between method calls. They are destroyed when the client disconnects or the bean is explicitly removed.
  5. Explain Stateless Session Beans.

    • Answer: Stateless Session Beans do not maintain conversational state. Each method call is independent of others. They are highly scalable and easily pooled by the container.
  6. Explain Singleton Session Beans.

    • Answer: Singleton Session Beans guarantee only one instance exists throughout the application's lifetime. They are useful for managing resources or providing application-wide services.
  7. What are Message-Driven Beans (MDBs)?

    • Answer: MDBs are asynchronous components that process messages from JMS queues or topics. They are ideal for handling time-consuming tasks or integrating with asynchronous systems.
  8. What is the role of the EJB Container?

    • Answer: The EJB container manages the lifecycle of EJBs, provides services like transaction management, security, concurrency control, and persistence, and handles resource pooling.
  9. What is the difference between a local and a remote interface in EJBs?

    • Answer: A local interface is used for clients within the same JVM as the EJB, while a remote interface is used for clients in different JVMs. Remote interfaces typically use RMI or other distributed communication protocols.
  10. Explain EJB deployment descriptors.

    • Answer: Deployment descriptors (typically `ejb-jar.xml`) are XML files that contain metadata about EJBs, such as their interfaces, transaction attributes, security roles, and resource references.
  11. What are EJB transactions?

    • Answer: EJB transactions ensure data consistency and integrity. They guarantee that operations are either completely successful or completely rolled back in case of failure.
  12. Explain different transaction attributes in EJBs.

    • Answer: Common attributes include `Required`, `RequiresNew`, `Mandatory`, `NotSupported`, `Supports`, and `Never`, which specify how the EJB interacts with existing transactions.
  13. What is Container-Managed Persistence (CMP)?

    • Answer: CMP (largely obsolete) was a mechanism where the EJB container managed the persistence of entity beans. It is now largely superseded by JPA.
  14. What is Bean-Managed Persistence (BMP)?

    • Answer: BMP (also largely obsolete) required the bean itself to handle its own persistence logic. JPA offers a much improved and standardized approach.
  15. What is JPA and how does it relate to EJBs?

    • Answer: Java Persistence API (JPA) is the standard for object-relational mapping (ORM) in Java. It's the preferred method for persistence in modern EJB applications, replacing CMP and BMP.
  16. What are EJB interceptors?

    • Answer: EJB interceptors allow you to add cross-cutting concerns (like logging, security, or auditing) to EJB methods without modifying the bean code itself.
  17. What are EJB timers?

    • Answer: EJB timers allow you to schedule the execution of EJB methods at specific times or intervals.
  18. How does EJB handle concurrency?

    • Answer: The EJB container provides mechanisms for managing concurrency, including thread pooling and synchronization to prevent race conditions and data corruption.
  19. Explain EJB security.

    • Answer: EJB security relies on JAAS (Java Authentication and Authorization Service) to authenticate users and control access to EJB methods based on roles and permissions.
  20. What are the different deployment options for EJBs?

    • Answer: EJBs are typically deployed to application servers like WildFly, JBoss EAP, GlassFish, or WebSphere.
  21. How do you manage EJB resources (databases, JMS queues)?

    • Answer: Resources are managed using resource references in the deployment descriptor and injected into the EJB using dependency injection.
  22. What are some common EJB design patterns?

    • Answer: Patterns like Session Facade, Data Access Object (DAO), and Business Delegate are frequently used in EJB applications.
  23. What is the difference between @PostConstruct and @PreDestroy?

    • Answer: `@PostConstruct` is called after the bean is instantiated and initialized, while `@PreDestroy` is called before the bean is destroyed.
  24. How do you handle exceptions in EJBs?

    • Answer: Exceptions are handled using standard Java exception handling mechanisms, often with appropriate logging and potentially throwing application-specific exceptions.
  25. What are some best practices for developing EJB applications?

    • Answer: Best practices include using appropriate bean types, designing for loose coupling, using dependency injection, adhering to naming conventions, and thorough testing.
  26. What is the role of the `@Stateless` annotation?

    • Answer: The `@Stateless` annotation marks a session bean as stateless, meaning it doesn't maintain client-specific state between method calls.
  27. What is the role of the `@Stateful` annotation?

    • Answer: The `@Stateful` annotation marks a session bean as stateful, meaning it maintains client-specific state between method calls.
  28. What is the role of the `@Singleton` annotation?

    • Answer: The `@Singleton` annotation marks a session bean as a singleton, ensuring only one instance exists throughout the application's lifetime.
  29. What is the role of the `@MessageDriven` annotation?

    • Answer: The `@MessageDriven` annotation marks a bean as a message-driven bean, allowing it to asynchronously process messages from JMS queues or topics.
  30. What is the role of the `@Local` annotation?

    • Answer: The `@Local` annotation defines a local business interface for an EJB, allowing clients within the same JVM to access it.
  31. What is the role of the `@Remote` annotation?

    • Answer: The `@Remote` annotation defines a remote business interface for an EJB, allowing clients in different JVMs to access it.
  32. How do you inject dependencies into an EJB?

    • Answer: Dependencies are injected using annotations like `@EJB`, `@Resource`, and `@Inject`.
  33. Explain the lifecycle of a stateless session bean.

    • Answer: Stateless session beans are pooled by the container. They are created when needed and released back to the pool when no longer in use. The container manages their lifecycle.
  34. Explain the lifecycle of a stateful session bean.

    • Answer: Stateful session beans are associated with a specific client. They are created when the client connects and destroyed when the client disconnects or the bean is explicitly removed.
  35. Explain the lifecycle of a singleton session bean.

    • Answer: Singleton session beans have only one instance throughout the application's lifetime. The container creates it during deployment and destroys it during undeployment.
  36. Explain the lifecycle of a message-driven bean.

    • Answer: Message-driven beans are created by the container when a message arrives on the associated queue or topic and are destroyed by the container when they are no longer needed.
  37. How do you configure transaction attributes for an EJB?

    • Answer: Transaction attributes are configured using the `@TransactionAttribute` annotation or in the deployment descriptor.
  38. What are some common EJB exceptions?

    • Answer: Common exceptions include `EJBException`, `NoSuchObjectException`, `FinderException`, and various application-specific exceptions.
  39. How do you test EJBs?

    • Answer: EJBs can be tested using various techniques, including container-managed testing (using the application server) and dependency injection frameworks like JUnit.
  40. What are the differences between EJB 2.x and EJB 3.x?

    • Answer: EJB 3.x introduced significant simplification, including the use of annotations, removal of deployment descriptors, and improved integration with POJOs.
  41. What is the role of the EJB timer service?

    • Answer: The EJB timer service allows you to schedule the execution of EJB methods at specific times or intervals, useful for tasks like batch processing or scheduled reports.
  42. How do you handle security in EJBs?

    • Answer: Security is handled using declarative security roles and permissions, often defined in the deployment descriptor or through annotations.
  43. What are the benefits of using a container-managed security approach?

    • Answer: Container-managed security leverages the application server's security infrastructure, simplifying development and improving security.
  44. What are the drawbacks of using EJBs?

    • Answer: Drawbacks include the complexity of the container setup, potential performance overhead in some cases, and the learning curve associated with the technology.
  45. What are some alternatives to EJBs?

    • Answer: Alternatives include Spring, microservices architectures, and other lightweight frameworks.
  46. When should you choose EJBs over other technologies?

    • Answer: Choose EJBs when you need robust transaction management, high scalability, and a mature, standardized framework for building large-scale enterprise applications.
  47. What is the purpose of the `@PersistenceContext` annotation?

    • Answer: `@PersistenceContext` injects an EntityManager instance into an EJB, providing access to the persistence context for JPA operations.
  48. What is the purpose of the `@PersistenceUnit` annotation?

    • Answer: `@PersistenceUnit` injects an EntityManagerFactory instance into an EJB, which is used to create EntityManagers.
  49. Explain the concept of a persistence context in JPA.

    • Answer: A persistence context is a set of managed entities. It acts as a cache and ensures consistency between the database and the application's objects.
  50. What are the different persistence context types in JPA?

    • Answer: Common types include `EXTENDED`, `TRANSACTION`, and `APPLICATION` which define the lifespan of the persistence context.
  51. How do you handle concurrency issues in JPA?

    • Answer: Concurrency issues in JPA are typically handled using optimistic locking, pessimistic locking, or versioning.
  52. Explain optimistic locking in JPA.

    • Answer: Optimistic locking assumes that conflicts are rare and checks for conflicts only when an entity is updated. It typically uses versioning.
  53. Explain pessimistic locking in JPA.

    • Answer: Pessimistic locking assumes that conflicts are frequent and acquires database locks to prevent concurrent access to entities.
  54. What are some best practices for using JPA with EJBs?

    • Answer: Best practices include using appropriate transaction management, choosing the right persistence context type, and efficiently handling queries.
  55. How does EJB interact with other technologies like JMS?

    • Answer: EJBs can interact with JMS using MDBs to consume messages from queues or topics, or by using JMS APIs directly within session beans.
  56. How can you improve the performance of your EJB applications?

    • Answer: Performance can be improved through techniques like connection pooling, efficient query design, caching, and optimization of EJB methods.
  57. What are the different ways to deploy an EJB application?

    • Answer: Deployment is typically done using the application server's administrative console or command-line tools.
  58. How do you monitor and troubleshoot EJB applications?

    • Answer: Monitoring and troubleshooting can be done using the application server's monitoring tools, logging, and debugging techniques.
  59. What are some common performance bottlenecks in EJB applications?

    • Answer: Bottlenecks can occur due to database access, network latency, inefficient code, or improper resource management.
  60. How do you handle large datasets in EJB applications?

    • Answer: Large datasets can be handled using techniques like pagination, lazy loading, and optimized queries to avoid loading all data at once.
  61. What is the role of the EJB deployment descriptor (ejb-jar.xml)?

    • Answer: The `ejb-jar.xml` file (less common in EJB 3.x and later) contains metadata about the EJBs, such as their interfaces, transaction attributes, and security roles.
  62. How do you secure EJBs using roles and permissions?

    • Answer: Security is implemented by defining roles and associating them with permissions to access EJB methods. This is often done declaratively.
  63. What is the difference between a container-managed relationship and a bean-managed relationship in EJBs?

    • Answer: In container-managed relationships, the EJB container handles the relationship between beans. In bean-managed relationships, the bean itself is responsible for managing the relationship.
  64. What is the role of the `@SecurityRole` annotation?

    • Answer: `@SecurityRole` defines a security role for use in securing EJB methods.
  65. What is the role of the `@RolesAllowed` annotation?

    • Answer: `@RolesAllowed` specifies the roles that are allowed to access a particular EJB method.
  66. What is the role of the `@PermitAll` annotation?

    • Answer: `@PermitAll` indicates that a method can be accessed by any user.

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