EJB Session – Basic knowledge of session bean

basic-knowledge-of-session-bean


What is a session

For a limited period of time, the connection between the client and the server

 

Why use session bean

Because most of the EJB services are dedicated to the session bean

1. Concurrency and thread safety: containers use a lot of technology to automatically ensure that developers do not have to worry about concurrent or thread safety
2. Remote processing and service provisioning of Web services
3. Transactions and security management
4. Interceptors

 

Session bean specification

1. At least one service interface (EJB3.1 not need)
2. Session bean must be specific, can not be defined as the final session bean, or abstract
3. The session bean must have a constructor for the non-argument
4. You can define business methods and lifecycle callback methods in a session bean class or super class
5. A session bean can be another subclass of a session bean or any other POJO. When it is a subclass of another session bean, the lifecycle callback method and the dependency injection defined in the parent class are inherited by the current bean class
6. Session bean method name can not start with "ejb", all business methods must be public, can not be final or static method, and if the session bean is remote, then all participants and parameters must be implement serialized interface

 

Business interface

1. Local interface: @Local, can access in the same container (JVM) instance
2. Remote interface: @Remote Collaboration in different container (JVM) instances is accessed through RMI
3. Web service interface: @ WebService for the stateless bean unique, you can expose the stateless bean as a SOAP-based Web service

Processing multiple business interfaces: can not use one or more access type annotation mark the same interface, you can choose to use the parent interface, and then sub-interface inheritance to avoid duplication of code

 

Stateless session bean

Used to model tasks that do not maintain session state

There are two creation patterns, one for pooling, creating a session bean pool by the container, and creating a corresponding number of session bean samples and managing it; the other is a singleton mode (requires EJB3.1 and later )

@Stateless notification container This class is a stateless bean, the container will automatically provide the bean: concurrency control, thread security, pooling, transaction management and other services

 

Stateful session bean

Used to model tasks that need to maintain session state, the EJB container will keep the state of the session

There must have a method that use the @Remove annotation

Note: the appropriate choice of session data, try to use the possession of resources, small data; remember to use passivation and deletion

If the Web application, use HttpSession to instead stateful session bean to maintain the state

 

Session bean best implementation

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

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

3. Do not put a stateful session bean into a stateless session bean or servlet

4. Divide the transaction (using an interceptor or AOP)

5. The data type stored in the stateful session bean (carefully selected)

6. Stateful session beans must define the @Remove annotation method

 

Leave a Reply