Identify correct and incorrect statements or examples about the lifecycle of a Stateful Session Bean including the @PrePassivate and @PostActivate lifecycle callback methods and @Remove methods.

[Note]

Lifecycle Callback Interceptor Methods

PostConstruct, PreDestroy, PrePassivate, and PostActivate lifecycle callback interceptor methods may be defined for session beans. If PrePassivate or PostActivate lifecycle callbacks are defined for STATELESS session beans, they are IGNORED.

The PrePassivate and PostActivate lifecycle callback interceptor methods are only called on STATEFUL session bean instances.

The PrePassivate callback notification signals the intent of the container to passivate the instance. The PostActivate notification signals the instance it has just been reactivated. Because containers automatically maintain the conversational state of a stateful session bean instance when it is passivated, these notifications are not needed for most session beans. Their purpose is to allow stateful session beans to maintain those open resources that need to be closed prior to an instance's passivation and then reopened during an instance's activation.

For example, the Bean Provider must close all JDBC connections in the PrePassivate method and assign the instance's fields storing the connections to null.

@Stateful
public class SessionCalculator implements Calculator  {

	@PrePassivate
	public void serialize () {
		// close JDBC connection here
	}
	
	@PostActivate
	private void activate () { // 'private' Ok too
		// re-open JDBC connection here
	}
	...
}	
					

The PrePassivate and PostActivate lifecycle callback interceptor methods execute in an UNSPECIFIED transaction and security context.

If the session bean implements the SessionBean interface, the PreDestroy annotation can only be applied to the ejbRemove method; the PostActivate annotation can only be applied to the ejbActivate method; the PrePassivate annotation can only be applied to the ejbPassivate method. Similar requirements apply to use of deployment descriptor metadata as an alternative to the use of annotations.

@Remove method

A client may remove a STATEFUL session bean by invoking a method of its business interface designated as a Remove method.

@Stateful
public class SessionCalculator implements Calculator {

	@Remove
	public void stopSession() {
		// Call to this method signals the container
		// to remove this bean instance and terminates
		// the session
		...
	}
	...
}
					

The lifecycle of a STATELESS session bean does not require that it be removed by the client. Removal of a stateless session bean instance is performed by the CONTAINER, transparently to the client.

Remove Annotation for Stateful Session Beans

The Remove annotation is used to denote a remove method of a STATEFUL session bean. Completion of this method causes the container to destroy the stateful session bean, first invoking the bean's PreDestroy method, if any. The retainIfException element allows the removal to be prevented if the method terminates abnormally with an APPLICATION exception.

@Target(METHOD) @Retention(RUNTIME)
public @interface Remove{
	boolean retainIfException() default false;
}
					

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine