Home  >  Article  >  Java  >  What is a session? Why use session beans?

What is a session? Why use session beans?

PHP中文网
PHP中文网Original
2017-06-21 10:14:222004browse

My blog article URL

What is a session

The connection between the client and the server within a limited time period

Why use session beans

Because most of EJB services are specifically provided to session beans

1. Concurrency and thread safety: Containers use many technologies to automatically ensure that developers do not have to worry about concurrency or thread safety. Thread safety issues
2. Service provision of remote processing and Web services
3. Affairs 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 concrete, and the session bean cannot be defined as final or abstract
3. The session bean must have a constructor without parameters
4. You can define business methods and life cycle callback methods 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 life cycle callback methods and dependency injection annotations defined in the parent class will be inherited by the current bean class
6. Session bean methods cannot start with "ejb". All business methods must be public and cannot be final or static methods. In addition, if the session bean is remote, all input and output parameters must implement the serialization interface

Business interface

1. Local interface: @Local Collaborative operations in the same container (JVM) instance
2. Remote interface: @Remote Collaborative operations in different container (JVM) instances are accessed through RMI
3. Web service endpoint interface: @WebService is unique to stateless beans and can expose stateless beans as SOAP-based Web services

Handling 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 to model tasks that do not maintain session state

There are two creation modes, one is pooling, in which the container creates a session bean pool, creates a corresponding number of session bean examples, and manages them; the other is singleton mode (requires EJB3.1 and later versions Only supported)

Annotation: @Stateless notifies the container that this class is a stateless bean, and the container will automatically provide the bean with: concurrency control, thread safety, pooling, transaction management and other services

Stateful session bean

Used to model tasks that require maintaining session state. The EJB container will maintain the session state for us

There must be a method annotated with @Remove

Note: Select the session data appropriately and try to use data that takes up less resources; remember to use passivation and deletion

Alternative: If it is a web application, use HttpSession to maintain state

Best implementation of session bean

1. Select the session bean type, that is, whether it is a stateful bean or a stateless bean

2. Analyze session bean interface type (@Local, @Remote)

3. Do not inject stateful session beans into stateless session beans or Servlets

4. Split cross-cutting transaction items (use interceptor AOP to process)

5. Data types stored in stateful session beans (choose carefully)

6. Stateful session beans must define @Remove annotated methods

The above is the detailed content of What is a session? Why use session beans?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Java basic String classNext article:Java basic String class