Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Enterprise JavaBeans™ Specification, Version 2.0 - Sun Microsystems.pdf
Скачиваний:
14
Добавлен:
24.05.2014
Размер:
2.71 Mб
Скачать

Sun Microsystems Inc

Stateless session beans

Enterprise JavaBeans 2.0, Public Draft

Session Bean Component Contract

Figure 10 OID for the removal of a session object

container provided classes

client

EJB

EJB

container session

synchro-

instance

 

Home

Object

context

nization

 

 

 

 

 

 

 

remove()

ejbRemove()

6.8 Stateless session beans

Stateless session beans are session beans whose instances have no conversational state. This means that all bean instances are equivalent when they are not involved in serving a client-invoked method.

The term “stateless” signifies that an instance has no state for a specific client. However, the instance variables of the instance can contain the state across client-invoked method calls. Examples of such states include an open database connection and an object reference to an EJB object.

The home interface of a stateless session bean must have one create method that takes no arguments and returns the session bean’s remote interface. There can be no other create methods in the home interface. The session bean class must define a single ejbCreate method that takes no arguments.

Because all instances of a stateless session bean are equivalent, the container can choose to delegate a client-invoked method to any available instance. This means, for example, that the Container may delegate the requests from the same client within the same transaction to different instances, and that the Container may interleave requests from multiple transactions to the same instance.

A container only needs to retain the number of instances required to service the current client load. Due to client “think time,” this number is typically much smaller than the number of active clients. Passivation is not needed for stateless sessions. The container creates another stateless session bean instance if one is needed to handle an increase in client work load. If a stateless session bean is not needed to handle the current client work load, the container can destroy it.

75

5/31/00

Sun Microsystems Inc.

Session Bean Component Contract

Enterprise JavaBeans 2.0, Public Draft

Stateless session beans

Because stateless session beans minimize the resources needed to support a large population of clients, depending on the implementation of the container, applications that use stateless session beans may scale somewhat better than those using stateful session beans. However, this benefit may be offset by the increased complexity of the client application that uses the stateless beans.

Clients use the create and remove methods on the home interface of a stateless session bean in the same way as on a stateful session bean. To the client, it appears as if the client controls the life cycle of the session object. However, the container handles the create and remove calls without necessarily creating and removing an EJB instance.

There is no fixed mapping between clients and stateless instances. The container simply delegates a client’s work to any available instance that is method-ready.

A stateless session bean must not implement the javax.ejb.SessionSynchronization interface.

6.8.1 Stateless session bean state diagram

When a client calls a method on a stateless session object, the container selects one of its method-ready instances and delegates the method invocation to it.

The following figure illustrates the life cycle of a STATELESS session bean instance.

5/31/00

76

Sun Microsystems Inc

Stateless session beans

Enterprise JavaBeans 2.0, Public Draft

Session Bean Component Contract

Figure 11 Lifecycle of a STATELESS Session bean

does not exist

1. newInstance()

 

ejbRemove()

 

2. setSessionContext(sc)

 

 

 

3. ejbCreate()

 

 

 

 

 

 

 

 

method

method-ready

pool

method()

action initiated by client

ejbCreate()

action initiated by container

 

 

The following steps describe the lifecyle of a session bean instance:

A stateless session bean instance’s life starts when the container invokes newInstance() on the session bean class to create a new instance. Next, the container calls setSessionContext() followed by ejbCreate() on the instance. The container can perform the

instance creation at any time—there is no relationship to a client’s invocation of the create() method.

The session bean instance is now ready to be delegated a business method call from any client.

When the container no longer needs the instance (usually when the container wants to reduce the number of instances in the method-ready pool), the container invokes ejbRemove() on it. This ends the life of the stateless session bean instance.

6.8.2 Operations allowed in the methods of a stateless session bean class

Table 3 defines the methods of a stateless session bean class in which the session bean instances can access the methods of the javax.ejb.SessionContext interface, the java:comp/env environment naming context, resource managers, and other enterprise beans.

77

5/31/00

Sun Microsystems Inc.

Session Bean Component Contract

Enterprise JavaBeans 2.0, Public Draft

Stateless session beans

If a session bean instance attempts to invoke a method of the SessionContext interface, and the access is not allowed in Table 3, the Container must throw the java.lang.IllegalStateException.

If a session bean instance attempts to access a resource manager or an enterprise bean and the access is not allowed in Table 3, the behavior is undefined by the EJB architecture.

Table 3

Operations allowed in the methods of a stateless session bean

 

 

 

 

 

 

 

Bean method can perform the following operations

 

Bean method

 

 

 

Container-managed transaction

Bean-managed transaction

 

 

 

 

demarcation

demarcation

 

 

 

 

 

 

 

 

 

constructor

-

-

 

 

 

 

 

setSessionContext

SessionContext methods: getEJBHome

SessionContext methods: getEJBHome

 

JNDI access to java:comp/env

JNDI access to java:comp/env

 

 

 

 

 

 

 

ejbCreate

SessionContext methods: getEJBHome,

SessionContext methods: getEJBHome,

 

getEJBObject

getEJBObject, getUserTransaction

 

ejbRemove

JNDI access to java:comp/env

JNDI access to java:comp/env

 

 

 

 

 

 

 

 

SessionContext methods: getEJBHome,

SessionContext methods: getEJBHome,

 

 

getCallerPrincipal, getRollback-

getCallerPrincipal, isCallerInRole,

 

 

Only, isCallerInRole, setRollback-

getEJBObject, getUserTransaction

 

business method

Only, getEJBObject

UserTransaction methods

 

JNDI access to java:comp/env

 

from remote interface

JNDI access to java:comp/env

 

 

Resource manager access

 

 

Resource manager access

 

 

Enterprise bean access

 

 

Enterprise bean access

 

 

 

 

 

 

 

Additional restrictions:

The getRollbackOnly and setRollbackOnly methods of the SessionContext interface should be used only in the session bean methods that execute in the context of a transaction. The Container must throw the java.lang.IllegalStateException if the methods are invoked while the instance is not associated with a transaction.

The reasons for disallowing operations in Table 3:

Invoking the getEJBObject method is disallowed in the session bean methods in which there is no session object identity associated with the instance.

Invoking the getCallerPrincipal and isCallerInRole methods is disallowed in the session bean methods for which the Container does not have a client security context.

Invoking the getRollbackOnly and setRollbackOnly methods is disallowed in the session bean methods for which the Container does not have a meaningful transaction context, and for all session beans with bean-managed transaction demarcation.

5/31/00

78