Friday, March 02, 2007

JAVA 認證心得 -- SCBCD (8)

Session Bean Component Contract

  • The isIdentical(...) method of stateless session bean's component interface always returns true when used to compare session objects of the same stateless session bean within the same home.True if the stubs refer to two entities with the same primary key.
  • A stateful session bean is about to be passivated, these below can remain assign to non-transient fields of the session bean class
    1. null -> of course
    2. A reference to the javax.ejb.SessionContext object (even it is not serialize) -> the EJB container must ensure that the Session Context object will be serialize properly.
    3. A serialize object -> The Java Serialization protocol determines dynamically if an object is serializable or not based on its runtime type
  • The return type of ejbCreate() method of a session bean should be void. Can throw application exception or CreateException
  • A bean’s conversational state may contain primitive values, serializable objects and certain special types like SessionContext. The remote reference to other bean are also preserved by the container, if present in the conversational state.
  • The afterBegin method notifies a session Bean instance that a new transaction has started, and that the subsequest business methods on the instance will be invoked in the context of the transaction.
  • The state of a stateful session bean instance is maintained by the session bean's instance variables. The conversational state is maintained by the instance variables of the stateful session bean instance
  • The conversational state of a stateful session bean instance is not written to a persistent store
  • A stateful session bean instance can have open resources such as sockets and database connections as part of the conversational state of the session bean instance. These open resources must be closed when the stateful session bean instance is passivated and must be re-opened when the bean instance is activated
  • EJB supports distributed transactions
  • The container is responsible for preserving the following objects across passivation and activation:
    1. reference to javax.ejb.SessionContext
    2. reference to component and home interface
    3. reference to the environment naming context or any of its sub-context (i.e., javax.naming.Context)
    4. reference to javax.transaction.UserTransaction
    5. reference to resource connection factory and any other serializable object. (i.e., javax.sql.DataSource)
  • The getEJBLocalObject() method of javax.ejb.SessionContext is used to obtain the session bean's local component interface
  • The javax.ejb.SessionBean interface defines methods which can be called only by the container in which the session bean is deployed. The methods defined by the SessionBean interface are ejbCreate, ejbRemove, ejbActivate and ejbPassivate.
  • The container doesn't preserve the value of a transient variable across passivation and activation of a stateful session bean even if it references UserTransaction or SessionContext interface.
  • Here are the rules for a create method in the bean class.
    1. Its signature must be like:
    public void ejbCreate<method>(...);
    2. It can throw any exception except java.rmi.RemoteException. It may even choose not to throw any exception.
    3. It must not be final or static.
    Note that, in the home interface (Local or Remote), the create method signature MUST throw javax.ejb.CreateException but the bean class's corresponding ejbCreate() method signature may or may not declare CreateException in its throws clause depending on whether the method code throws CreateException or not.
  • An ejb acquires resources in ejbCreate() and ejbActivate() methods and it releases the same resources in ejbRemove() and ejbPassivate() method.
  • non-transient fields would already have been given the value that they had before passivation. So there is no need to initialize them
  • Since the transient fields are not preserved while serialization-deserialization process, you need initialize such fields to appropriate values in ejbActivate() method
  • Remember the following point about the ejbCreate methods:
    1. The method name must have ejbCreate as its prefix.
    2. The method must be declared as public.
    3. The method must not be declared as final or static.
    4. The return type must be void.
    5. The method arguments must be legal types for RMI/IIOP if there is a create<METHOD>(...) corresponding to the ejbCreate< METHOD>(...) method on the session bean's remote home interface.
    6. The throws clause may define arbitrary application exceptions, possibly including the javax.ejb.CreateException.
  • The session bean instance must not reset it's state in the beforeCompletion() method because the outcome of the transaction (commit or rollback) is not known at the time beforeCompletion() method is invoked.
  • the conversational state of a stateful session bean is not transactional i.e. it is not automatically reset to its initial state if the transaction in which it was manipulated is rolled back.
  • The session bean instance can reset it's state in the afterCompletion(boolean) method

Read more!

No comments: