EJB series,
My blog website: https://www.peretang.com/basic-knowledge-of-session-bean/
What is session
Connections between clients and servers within a limited period of time
Why use Session bean
Most EJB services are dedicated to Session bean.
1. Concurrency and thread security: The container uses many technologies to automatically ensure that developers do not have to worry about concurrency or thread security issues.
2. Remote Processing and Web Service Provision
3. Transaction and Security Management
4. interceptor
Session bean Specification
1. Have at least one business interface (not required after EJB3.1)
2. The session bean must be specific and cannot be defined as final or abstract.
3. the constructor in the Session bean must have no parameters.
4. You can define the business method and lifecycle callback method in the Session bean class or parent class.
5. A session bean can be a subclass of another session bean or any other POJO. When it is a subclass of another session bean, the lifecycle callback method and dependency injection annotation defined in the parent class are inherited by the current bean class.
6. the Session bean method cannot start with "ejb". All business methods must be public, not final or static. If the session bean is remote, all input and output parameters must implement the serialization interface.
Business Interface
1. Local interface: Perform collaborative operations in the same container (JVM) instance @ Local
2. Remote interface: @ Remote access through RMI for collaborative operations in different container (JVM) Instances
3. Web service endpoint interface: @ WebService is unique to stateless beans. You can expose stateless beans as SOAP-based Web Services.
Process multiple business interfaces: you cannot mark the same interface with more than one access type annotation. You can choose to use the parent interface, and then inherit the sub-interface to avoid code duplication.
Stateless Session bean
Used for modeling tasks that do not maintain session Status
There are two creation modes. One is pooling. The Container creates a session bean pool, creates and manages the corresponding number of session beans; the other is the singleton mode (supported only in EJB3.1 and later versions)
Annotation: @ Stateless notifies the container that this class is a Stateless bean, and the container automatically provides services such as concurrency control, thread security, pooling, and transaction management for the bean.
Stateful Session bean
Used to model tasks that require session State maintenance. The EJB container keeps the session state for us.
The @ Remove annotation must be used.
Note: Select session data as appropriate and use data that occupies a small amount of resources as much as possible. Remember to use passivation and deletion.
Alternative: for Web applications, use HttpSession to maintain the status
Best Implementation of Session bean
1. Select the Session bean type, that is, stateful bean or stateless bean.
2. Analyze the Session bean interface type (@ Local, @ Remote)
3. Do not inject stateful session beans into stateless session beans or Servlets.
4. Split the cross-section transaction items (using the interceptor AOP for processing)
5. Data Types stored in stateful session beans (careful selection)
6. The @ Remove annotation method must be defined for stateful session beans.
Reference
EJB 3 in Action, Second Edition