The @Stateless annotation marks a class as a stateless session bean in Enterprise JavaBeans (EJB). The container manages a pool of instances, runs each business method in a transaction by default and handles security and concurrency for you. Because a stateless bean keeps no conversational state for a particular client, any pooled instance can serve any call.
Since Jakarta EE 9 the annotation lives in jakarta.ejb.Stateless. In Java EE 8 and older it was javax.ejb.Stateless.
A stateless session bean holds business logic, not data tied to one caller. Two consecutive calls from the same client may run on different instances, so you must not rely on instance fields to remember anything between calls.
The container gives you these services without extra code:
@RolesAllowed.The class needs a public no-argument constructor and must not be final. Client code gets the bean by injection, not with new.
In a modern Jakarta EE application you can also use @Inject for the injection.
By default every business method of a session bean runs with TransactionAttributeType.REQUIRED. It joins the caller's transaction or starts a new one, and commits when the method returns.
| Exception type | Container behaviour |
|---|---|
Unchecked (RuntimeException) | Transaction rolls back, instance is discarded |
| Checked application exception | Transaction commits unless @ApplicationException(rollback = true) |
A bean can be called in three ways.
| View | How to declare | Use |
|---|---|---|
| No-interface | Default for a class with no interface annotations | Same application, simplest |
| Local | @Local on an interface | Same JVM, other modules |
| Remote | @Remote on an interface | Different JVM, uses serialization, slower |
| Type | Annotation | State | Typical use |
|---|---|---|---|
| Stateless | @Stateless | None | Service layer, facades |
| Stateful | @Stateful | Per client | Conversations (rare today) |
| Singleton | @Singleton | One shared instance | Caches, startup tasks |
| Message-driven | @MessageDriven | None | Reacting to JMS messages |
For new code, many teams use CDI beans with @ApplicationScoped and @Transactional instead of @Stateless, because that works across Jakarta EE, Quarkus and other runtimes. EJBs remain fully supported in application servers such as WildFly, Payara and Open Liberty, and the annotation is still the shortest way to get pooling, transactions and security together.
The container guarantees that one instance handles one request at a time, so you do not synchronise instance fields. Avoid mutable shared state such as static fields.
It can, for injected resources and configuration. You must not use fields to remember data between calls, because the next call may use another instance.
Use @Stateless if you run on a full Jakarta EE server and want declarative pooling and transactions. Choose CDI with @Transactional for portability across lightweight runtimes.
Jakarta EE 9 renamed the packages from javax.* to jakarta.*. Use jakarta.ejb.Stateless on current servers.
@Stateless turns a class into a pooled, container-managed business component with transactions, security and injection built in. It must hold no client state, defaults to REQUIRED transactions, can expose no-interface, local or remote views, and now uses the jakarta.ejb package. Consider CDI beans as the lighter alternative for new services.