EJB Life Cycle Example - Java Code Geeks
EJB Life Cycle Example - Java Code Geeks
ejb3
6 minutes read
Does not exist. In this state, the bean instance simply does not exist.
Ready state. A bean instance in the ready state is tied to particular
client and engaged in a conversation.
The various state transitions as well as the methods available during the
various states are discussed below.
https://examples.javacodegeeks.com/java-development/enterprise-java/ejb3/ejb-life-cycle-example/ 1/8
10/16/24, 10:35 PM EJB Life Cycle Example - Java Code Geeks
2.1 Moving from the Does Not Exist to the Ready State
When a client invokes a create method on a stateful session bean, the EJB
container creates a new instance and invokes the callback method public
void setSessionContext(SessionContext ctx). This method has the parameter
javax.ejb.SessionContext , which contains a reference to the session
context, that is, the interface to the EJB container, and can be used to self-
reference the session bean object.
If a stateful session bean is set up to use the NRU(not recently used) cache-
type algorithm, the session bean can time out while in passivated state.
When this happens, it moves to the does not exist state; that is, it is
https://examples.javacodegeeks.com/java-development/enterprise-java/ejb3/ejb-life-cycle-example/ 2/8
10/16/24, 10:35 PM EJB Life Cycle Example - Java Code Geeks
removed. Prior to removal the EJB container will call the callback method
ejbRemove. If a stateful session bean is set up to use the LRU (least
recently used) algorithm, it cannot time out while in passivated state.
Instead this session bean is always moved from the ready state to the
passivated state when it times out.
The exact timeout can be set using the idleTimeoutSeconds attribute on the
@Session annotation. The cache-type algorithm can be set using the
attribute on the same annotation.
2.4 Moving from the Ready to the Does Not Exist State
When a client application invokes a remove method on the stateful session
bean, it terminates the conversation and tells the EJB container to remove
the instance. Just prior to deleting the instance, the EJB container will call
the callback method ejbRemove . If your session bean needs to execute
some custom logic prior to deletion, you can implement it using this callback
method.
An inactive stateful session bean that is set up to use the NRU (not recently
used) cache-type algorithm can time out, which moves it to the does not
exist state, that is, it is removed. Prior to removal the EJB container
will call the callback method ejbRemove . If a stateful session bean set up to
use the LRU (least recently used) algorithm times out, it always moves to
the passivated state, and is not removed.
The exact timeout can be set using the idleTimeoutSeconds attribute on the
@Session annotation. The cache-type algorithm can be set using the
cacheType attribute on the same annotation.
Does not exist. In this state, the bean instance simply does not exist.
Ready state. When WebLogic Server is first started, several bean instances
are created and placed in the Ready pool. More instances might be created
by the container as needed by the EJB container.
https://examples.javacodegeeks.com/java-development/enterprise-java/ejb3/ejb-life-cycle-example/ 3/8
10/16/24, 10:35 PM EJB Life Cycle Example - Java Code Geeks
The various state transitions as well as the methods available during the
various states are discussed below.
3.1 Moving from the Does Not Exist to the Ready State
When the EJB container creates a stateless session bean instance to be
placed in the ready pool, it calls the callback method public void
setSessionContext(SessionContext ctx) . This method has the parameter
javax.ejb.SessionContext, which contains a reference to the session context,
that is, the interface to the EJB container, and can be used to self-reference
the session bean object.
https://examples.javacodegeeks.com/java-development/enterprise-java/ejb3/ejb-life-cycle-example/ 4/8
10/16/24, 10:35 PM EJB Life Cycle Example - Java Code Geeks
business method. Once execution has finished, the session bean instance is
ready to execute another business method.
3.3 Moving from the Ready to the Does Not Exist State
When the EJB container decides to reduce the number of session bean
instances in the ready pool, it makes the bean instance ready for garbage
collection. Just prior to doing this, it calls the callback method ejbRemove . If
your session bean needs to execute some cleanup action prior to garbage
collection, you can implement it using this callback method. The callback
method is not tied to the remove method invoked by a client. For a stateless
session bean, calling the remove method invalidates the reference to the
bean instance already in the ready pool, but it does not move a bean
instance from the ready to the does not exist state, as the management of
stateless session bean instances is fully done by the EJB container.
The EJB container typically creates and maintains a pool of stateless session
beans, beginning the stateless session bean’s lifecycle. The container
performs any dependency injection and then invokes the method annotated
@PostConstruct , if it exists. The bean is now ready to have its business
methods invoked by a client.
At the end of the lifecycle, the EJB container calls the method annotated
@PreDestroy , if it exists. The bean’s instance is then ready for garbage
collection.
https://examples.javacodegeeks.com/java-development/enterprise-java/ejb3/ejb-life-cycle-example/ 5/8
10/16/24, 10:35 PM EJB Life Cycle Example - Java Code Geeks
3. ejbRemove: At this stage the bean instance is ready to move for the
garbage collection.
https://examples.javacodegeeks.com/java-development/enterprise-java/ejb3/ejb-life-cycle-example/ 6/8
10/16/24, 10:35 PM EJB Life Cycle Example - Java Code Geeks
01 package ejb;
02
03 import java.util.List;
04 import javax.ejb.Stateless;
05 import javax.persistence.EntityManager;
06 import javax.persistence.PersistenceContext;
07
08 @Stateless
09 public class NewsEntityFacade implements NewsEntityFaca
10 @PersistenceContext
11 private EntityManager em;
12
13 public void create(NewsEntity newsEntity) {
14 em.persist(newsEntity);
15 }
16
17 public void edit(NewsEntity newsEntity) {
18 em.merge(newsEntity);
19 }
20
21 public void remove(NewsEntity newsEntity) {
22 em.remove(em.merge(newsEntity));
23 }
24 }
6. Conclusion
In this tutorial, we learn about various stages through which an enterprise
bean go through its lifetime. For more information on EJB technology, see
http://www.oracle.com/technetwork/java/ejb-141389.html.
Sign up
https://examples.javacodegeeks.com/java-development/enterprise-java/ejb3/ejb-life-cycle-example/ 7/8
10/16/24, 10:35 PM EJB Life Cycle Example - Java Code Geeks
1 COMMENT Oldest
2 Reply
https://examples.javacodegeeks.com/java-development/enterprise-java/ejb3/ejb-life-cycle-example/ 8/8