EJB is an enterprise-level JavaBean, which is a kind of distributed concrete implementation. The enterprise bean of EJB is divided into three kinds, namely session bean, which is divided into stateless session bean (stateless Conversation Bean), Statefule session bean (stateful conversation bean) ; Message driven bean (message-driven bean), persistence (persisted) entity bean (Entity Bean). This article mainly explains the session Bean
1. Session Bean
The role of a session bean: business logic operations. such as registered users, order registration, database operations and so on.
Session
Getting the EJB object from the client to the end of the client life cycle or releasing the EJB object is called a session.
A session and an object are one-to-two correspondence, a session corresponds to an instantiated object, and two sessions correspond to two objects.
Object state
The state of an object is made up of the values of its instance variables (that is, member variables)
Instance variables: variables associated with an instance, different instances, with variable values. General non-static variables
Class variables: variables associated with a class, in the same class, whose variable values are all the same. is usually a static variable
The difference between stateful session Bean and stateless session bean is in the process of multiple sessions (Request/Method call), the value of the instance variable can be saved
1.1. Stateful Session Bean
Stateful session Bean, which is the ability of an EJB to hold state information between multiple requests (method calls) on the same client
in the face of a state of EJB is equivalent to each call to this object when the server created a new object for this request to use
depending on the session, the server side will be assigned a different Session , then how to distinguish between sessions at the server side of the session ? by Jsessionid This value to differentiate
1.2. Stateless Bean
In the case of a stateful EJB, the server gives me the same object every time the object is called.
It is managed in the form of a singleton, so it is not possible to distinguish between different clients
This is not to say that EJBS cannot exist, and that EJB containers do not save EJB states. Because the EJB container does not manage the state of the srateless session bean, it performs better than the srateful session Bean
2. Sample Code
Class statetest{
Private int value=0;
public void ChangeValue (int value) {
System.out.print (value);
System.out.print (This.value);
This.value=value;
}
}
Set Statetest to stateful and stateless, respectively
Client Calls
Statetest test1 = new Statetest ();
Test1.changevalue (1);
Test1.changevalue (2);
Statetest test2 = new Statetest ();
Test2.changevalue (1);
Test2.changevalue (2);
Execution results
No status
1 0 2 1 1 1 2 2
Have status
1 0 2 1 1 0 2 1
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
EJB (Enterprise-class JavaBean) session Bean