When a passivated stateful session bean is needed again, the container must restore its state from secondary storage back into memory. The bean needs to know when this happens to reinitialize any non-serializable resources (like database connections, JMS connections).
ejbActivate() is a callback method that the EJB container calls on a stateful session bean AFTER it has been passivated and is now being restored to the ready state. It’s the counterpart to ejbPassivate().
- Container decides — based on client request, container selects a passivated bean
- Deserialization — container reads bean state from storage
- ejbActivate() called — container calls this method AFTER state is restored
- Bean reinitializes — open resources, reconnect to services that weren’t serialized
For entity beans, ejbActivate() is called when an entity bean is moved from the pool to the ready state (associated with a specific primary key).
- Callback method — container calls it, not the client
- Stateful SB — reinitialize transient resources after passivation
- Entity Bean — called when bean moves from pool to ready (associated with PK)
- No parameters — container doesn’t pass any arguments
- Built from: Passivation — ejbActivate is the reverse process
- Built from: Activation — the overall process this method participates in
- Contrasts with: ejbPassivate() — called before passivation
- Related: Stateful Session Bean — primary user of this callback
- Related: Entity Bean — also uses this callback in lifecycle
- Only for stateful session beans and entity beans (NOT stateless)
- Don’t do business logic here — just resource reinitialization
- If this method throws an exception, the bean may be discarded