Quick revision
The short answer
Stateless session beans hold no client-specific state between calls, so the container keeps a pool of instances and hands any free one to each call. Stateful session beans keep conversational state for one client across many calls, and message-driven beans consume messages, typically from JMS queues or topics, instead of being called directly. For all three, the container provides transactions, security and concurrency control declaratively, through annotations rather than code.
What to remember
In this answer, we saw that stateless session beans hold no client state and are pooled, stateful session beans keep conversational state for one client and may be passivated, and message-driven beans process messages from JMS destinations without being called directly. For all of them, the container provides transactions, security and concurrency control declaratively, the same ideas that Spring later delivered through proxies and annotations.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Stateless session beans hold no client-specific state between calls, so the container keeps a pool of instances and hands any free one to each call. Stateful session beans keep conversational state for one client across many calls, and message-driven beans consume messages, typically from JMS queues or topics, instead of being called directly. For all three, the container provides transactions, security and concurrency control declaratively, through annotations rather than code.
Why this question is asked
Enterprise JavaBeans (EJB), now Jakarta Enterprise Beans, were the heart of Java EE applications for many years, and plenty of production systems on WildFly, WebLogic, WebSphere and Open Liberty still use them. Even engineers who mostly write Spring code meet them when maintaining or migrating such systems, and many Spring features, such as @Transactional, were shaped by EJB's ideas. Interviewers ask about the three bean types to check that we understand what a container manages for us and why the types exist. In this answer, we will recap what an EJB container does, look at each bean type with an example, see the declarative services they all share, and compare them with what we would write in Spring today.
1A Quick Recap of the EJB Container
An EJB is a Java class managed by the EJB container inside a Jakarta EE application server. We never create it with new. Instead, clients get a reference by injection, with @EJB or @Inject, and every call goes through the container, which wraps the bean with services: it starts and commits transactions, checks security roles, controls how many threads can enter the bean, and manages the bean's lifecycle. This is the same idea as a Spring proxy, applied by the server.
The three bean types differ in one main question: what happens to the state of a bean between calls, and who calls it.
2Stateless Session Beans
A stateless session bean, marked @Stateless, does not remember anything about a particular client between method calls. It may hold references to other beans or resources, but not client data. Because any instance is as good as any other, the container keeps a pool of instances and gives each incoming call whichever one is free. When the call ends, the instance goes back to the pool. Each instance serves only one call at a time, so its fields are safe from concurrent access without any locking in our code.
Stateless beans are the workhorse type: business services, calculations and data access. They scale well because the container can size the pool to the load.
3Stateful Session Beans
A stateful session bean, marked @Stateful, belongs to one client and keeps its fields between calls, which is called conversational state. A typical example is a multi-step checkout where items are added in several calls and confirmed at the end. The container creates a new instance for each client and keeps it until the client calls a method annotated @Remove or the bean times out.
Because these beans hold state for many clients, the container may passivate idle ones, serialising them to disk to free memory, and activate them again on the next call. That is why fields must be serialisable or marked transient. Stateful beans cost more memory than stateless ones, and in clustered servers their state must be replicated, so most designs keep them to a minimum.
4Message-Driven Beans
A message-driven bean, marked @MessageDriven, is not called by clients at all. The container connects it to a messaging destination, typically a JMS queue or topic, and calls its onMessage() method each time a message arrives. Like stateless beans, message-driven beans hold no client state, and the container pools them so several messages can be processed in parallel. The client that sent the message does not wait, which makes this the EJB way to do asynchronous, decoupled processing.
| Type | Client state | Instances | Called by |
|---|---|---|---|
@Stateless | None between calls | Pooled, shared by all clients | Clients, via injection |
@Stateful | Kept for one client | One per client, may be passivated | One client, many calls |
@MessageDriven | None | Pooled | The container, when a message arrives |
@Singleton, one shared instance per application, added in EJB 3.1.5Declarative Services the Container Provides
The reason EJBs exist is that the container adds services without our code. Transactions are declarative: every business method runs with @TransactionAttribute(REQUIRED) by default, so it joins the caller's transaction or starts a new one, and a runtime exception rolls it back. We can change this per method with values such as REQUIRES_NEW or NOT_SUPPORTED. Security is declarative too: @RolesAllowed("manager") on a method makes the container reject callers without that role. Concurrency is controlled for us: stateless and message-driven instances serve one call at a time, a stateful bean rejects or serialises concurrent calls from the same client, and singletons use @Lock(READ) or @Lock(WRITE).
Spring later offered the same ideas without an application server: @Transactional for transactions, @PreAuthorize or @Secured for security, and singleton beans that must be written to be thread-safe. In a migration, a @Stateless bean usually becomes a Spring @Service, a message-driven bean becomes a @JmsListener method, and a stateful bean becomes session-scoped data or, better, state stored in a database.
6Follow-up questions
Is a stateless bean thread-safe?
Each instance is, because the container sends only one call at a time to an instance. But the class must still not store client data in fields, because the next call may come from a different client to the same instance. Shared resources the bean uses, such as a static cache, still need their own thread safety.
What is the difference between @Stateless and @Singleton?
A stateless bean has a pool of instances, each serving one call at a time. A singleton bean has exactly one instance for the whole application, which all calls share, so the container applies locks (by default @Lock(WRITE), one caller at a time) unless we choose READ locks or bean-managed concurrency.
Can EJBs and CDI beans work together?
Yes. Since Java EE 6, session beans are also CDI beans, so they can be injected with @Inject and can inject CDI beans. Modern Jakarta EE code often uses plain CDI beans with @Transactional from Jakarta Transactions, keeping EJBs for message-driven beans and timers.
7Conclusion
In this answer, we saw that stateless session beans hold no client state and are pooled, stateful session beans keep conversational state for one client and may be passivated, and message-driven beans process messages from JMS destinations without being called directly. For all of them, the container provides transactions, security and concurrency control declaratively, the same ideas that Spring later delivered through proxies and annotations.
Open with
EJBs are classes managed by the container in a Jakarta EE server, and every call goes through the container, which adds services around the bean. There are three main kinds.
Then, if they want more
A stateless session bean, @Stateless, keeps no client state between calls, so the container keeps a pool of instances and gives each call any free one; each instance handles one call at a time, so it is safe without locking, and this is the type for most business services.
A stateful session bean, @Stateful, belongs to one client and keeps its fields across calls, like a shopping cart during checkout; it lives until a @Remove method or a timeout, and the container can passivate idle ones to disk, so its fields should be serialisable.
A message-driven bean, @MessageDriven, is not called by clients; the container wires it to a JMS queue or topic and calls onMessage for each message, pooled like stateless beans, and if it throws, the transaction rolls back and the message is redelivered. Across all of them, the container handles transactions declaratively, REQUIRED by default, security with @RolesAllowed, and concurrency.
In Spring terms, a stateless bean maps to a @Service with @Transactional and a message-driven bean to a @JmsListener.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Jakarta EE specifications
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What are stateless session beans, stateful session beans and message-driven beans in EJB?": https://scrivio-interview-library.netlify.app/java/jakarta-ee-platform/0ea44adb69/