Session Bean Methods
Table 3.1 provides summary of session bean methods.
Table 3.1 Summary of Session Bean Methods
Method |
Purpose |
What You Need to Do |
setSessionContext(SessionContext) |
The EJB container calls this method to set the associated session context. |
You store the reference to the session context in an instance variable, if you need to query it later. The session context provides access to runtime session context such as identifying the caller, access or change current transaction state, and so on. |
ejbCreate<method>(...) |
The container calls this method so that you can initialize your session bean instance. A client creates a stateful instance using the create bean class methods defined in the session bean's home interface. The container calls the corresponding ejbCreate method. |
You initialize the bean here. Each stateful session must have at least one ejbCreate<method>(...) and each one can take different arguments. A stateless session bean can have only one ejbCreate method, with no parameters. |
Business Methods |
The session bean's component interface defines the business methods callable by a client. The container delegates an invocation of a business method to the matching business method that is implemented in the session bean class. |
You write business logic in these methods. |
ejbActivate() |
This method is called when the instance is activated from its passive state. |
For stateful session bean, open resources, if any. For example, open sockets, JDBC connections, and so on. For a stateless session bean, this is empty because the container never activates the bean instance. |
ejbPassivate() |
This method is called when the container intends to passivate the bean instance. |
For a stateful session bean, close resources, if any. For example, close sockets, JDBC connections, and so on. For a stateless session bean, this is empty because the container never passivates the bean instance. |
ejbRemove() |
A container invokes this method before it ends the life of the session object. This happens as a result of a client's invoking a remove operation, or when a container decides to terminate the session object after a timeout. |
Close resources, if any, and assign corresponding instance fields to null. For example, close sockets, JDBC connections, and so on. For a stateless session bean, this is empty because the container never passivates the bean instance. |