Tuesday, January 16, 2007

SCBCD Mock Questions - Session Bean Component Contract

11)Which one of the following statements are requirements for the session bean's remote home interface?

1)The interface must extend javax.ejb.EJBObject.
2)The return type for a create<Method>(...) method must be void.
3)A stateless session bean must define one or more create<METHOD>(...) method.
4)The throws clause of a create<Method>(...) method must include javax.ejb.CreateException.
5)The return type for a create<Method>(...) method must match the return type of the corresponding ejbCreate<METHOD> method that is defined in the session bean class.

2)Which one of the following responsibilities related to session beans belong to the bean provider?

1)Ensuring that only one thread can execute a bean instance at anyone time
2)Managing the session bean instances at runtime.
3)Supplying resource connections used by an enterprise bean.
4)Deciding whether a session bean should use either container-managed or bean-managed transaction demarcation.
5)Handling of a system exception thrown by a session bean's business method.

3)Which one of the following methods provides notification that the current transaction has completed?

1)javax.ejb.SessionBean.ejbPassivate()
2)javax.ejb.SessionBean.ejbRemove()
3)javax.ejb.SessionSynchronization.afterCompletion(boolean)
4)javax.ejb.SessionSynchronization.beforeCompletion(boolean)
5)javax.ejb.SessionSynchronization.transactionCompleted(boolean)

4)Which of the following responsibilities related to session bean's belong to the EJB container provider? [Select all correct answers]

1)Affecting transaction management of a session bean with container-managed transaction demarcation.
2)Handling of a system exception thrown by a session bean's business method.
3)Defining the session bean class.
4)Deciding whether a session bean should use either container-managed or bean-managed transaction demarcation.
5)Supplying resource connections used by an enterprise bean.

5)Which of the following responsibilities relating to a stateful session bean's ejbPassivate method belong to the container? [Select all correct answers]

1)Providing notification that a bean instance is about to be passivated.
2)Ensuring that the bean's instance fields meet the requirements for passivation.
3)Closing any open socket connections.
4)Saving a SessionContext interface object reference that is stored in the bean instance's state
5)Saving an UserTransaction interface object reference that is stored in the bean instance's state.

6)Which of the following statements regarding the create methods of a session bean are correct. [Select all correct answers]

1)Each session bean class must have at least one ejbCreate<METHOD>.
2)The return type of the ejbCreate method is the session bean's remote interface.
3)The container always calls ejbCreate() on the bean instance when a client invokes a create operation on the home interface of a stateless session object.
4)ejbCreate() is invoked by the container with an unspecified transaction context.
5)Invoking EJBContext.getRollbackOnly() is not permitted in ejbCreate() of a session bean instance with container-managed transaction demarcation.

7)Which of the following statements only apply to stateful session beans? [Select all correct answers]

1)Maintained by the container in a pool of available instances.
2)Ability to receive notification by the container of transaction boundaries.
3)Maintains conversational state across several business method calls.
4)A transaction started in a business method must be committed before the method completes.
5)Receives notification from the container when the bean instance is activated.

8)Which one of the following statements regarding a session bean's conversational state is true?

1)Stateless and stateful session beans both maintain conversational state.
2)An object reference to the java:comp/env JNDI context that is stored in session bean's conversational state is not perserved when the session bean is passivated.
3)The conversational state of a session bean with container-managed transaction demarcation is automatically rolled back to its initial state if a system exception is thrown by a business method.
4)A session bean's conversational state can hold an open socket connection.
5)Non-transient fields of a session bean's conversational state are lost when a session bean is passivated.

9)Which of the following statements are requirements for the session bean class? [Select all correct answers]

1)The session bean class must implement, directly or indirectly the javax.ejb.SessionBean interface.
2)Each method defined in the session bean class must have a matching method in the session bean's remote interface.
3)The class must be defined as public.
4)The class can be declared final.
5)A stateful session bean class does not have to define a no-argument public constructor.

10)Which of the following statements regarding the session bean interface are correct? [Select all correct answers]

1)All session beans must implement the SessionBean interface.
2)The container calls unsetSessionContext() to unset the associated session context.
3)The container notifies a stateless session bean by calling ejbPassivate() on the bean instance entering the "passive" state.
4)The container invokes ejbRemove() on a stateless bean instance when it no longer needs the instance and wishes to reduce the number of bean instances in the method-ready pool.
5)The ejbActivate method is a suitable place to open a database cursor that was previously closed in the ejbPassivate method of a stateful session bean.

Answers:

1)4

Answer 4 is correct.

Answer 1 is incorrect because the session bean's remote home interface must extend javax.ejb.EJBHome.

Answers 2 and 5 are incorrect. The return type for a create<Method>(...) method must be the session bean's remote component interface type and the corresponding ejbCreate<METHOD> method that is defined in the session bean class must be void.

Answer 3 is incorrect because a stateless session bean must define only one create() method with no arguments.



2)4

Answer 4 is correct. When designing an enterprise bean, the bean provider must decide, based on the transaction attributes in the deployment descriptor, whether a session bean uses container-managed or bean-managed transaction demarcation.

Answers 1, 2, 3, and 5 are incorrect as they are the responsibilities of the EJB container prov4ider.



3)3

Answer 3 is correct. javax.ejb.SessionSynchronization.afterCompletion(boolean) informs a stateful session bean that implements the SessionSychronization interface when the current transaction has completed. A completion status is passed to the method; true for a committed transaction and false if a rollback has occurred. If a rollback has occurred, afterCompletion() is a good place to reset any cached instance variables to the values prior to beginning the transaction.

Answers 1 and 2 are incorrect because the methods do not provide any notifications about transactions. Answers 4 and 5 are incorrect because no such methods exists.



4)1,2,5

Answers 1 ,2 and 5 are correct. Answers 1 and 5 are correct because the EJB container provider intercepts every client method invocation on a session bean object via the bean's home and component interface and requests for connections to resource managers to affect the enterprise bean’s transaction management. Answer 2 is correct. When a system exception is thrown by a session bean's business method and a transaction context exists, the container logs the exception, marks or rolls back the transaction, discards the bean instance and then throws an exception to the client.

Answers 3 and 4 are incorrect as they are the responsibilities of the bean provider.



5)1,4,5

Answers 1, 4, and 5 are correct. The purpose of the ejbPassivate method in a stateful session bean is to allow the container to notify the bean provider of its intent to passivate the bean instance. If references to an javax.ejb.SessionContext interface or javax.transaction.UserTransaction interface are stored in the instance's state it is the container's responsibility to save and restore the objects references after invoking ejbPassivate(). The container can replace the original object with a different but functionally equivalent objective during activation.

Answers 2 and 3 are incorrect because they are the responsibility of the bean provider.



6)1,4,5

Answers 1, 4 and 5 are correct. ejbCreate() is invoked by the container with an unspecified transaction context. The transactional behaviour of executing ejbCreate() is undefined by the EJB specification and left to the container implementation. Since ejbCreate() is invoked by the container with an unspecified transaction context, calling EJBContext.getRollbackOnly() in ejbCreate() is not permitted and results in java.lang.IllegalException being thrown.

Answer 2 is incorrect because the return type of the ejbCreate method must be void. This should not be confused with the return type of a create<METHOD>(...) method on the remote home interface which returns the session bean's remote interface. Answer 3 is incorrect. There is no relationship to a client's invocation of a create operation on a stateless session bean with the container calling ejbCreate(). The container invokes ejbCreate() when it wishes to increase the number of instances in the method-ready pool to cope with an increase client work load.



7)2,3,5

Answers 2, 3, and 5 are correct. Answer 2 is correct because only stateful session beans can implement the SessionSynchronization interface. A session bean that implements the SessionSynchronization interface is provided with transaction synchronization notifications from the container. Answer 3 is correct because only stateful session beans maintain conversational state, whereas stateless session bean don't. Answer 5 is correct. Only stateful session beans are passivated and activated by the container because they have conversational state that require preservation when the container decides to conserve resources.

Answers 1 and 4 are incorrect because they only apply to stateless session beans.



8)4

Answer 4 is correct. The conversational state of a stateful session bean may hold resources such as open socket connections and open database cursors. However, the container cannot retain the open resources when the bean instance is passivated. The bean provider must therefore ensure that the open resources are closed in the ejbPassivate method and reopened in the ejbActivate method.

Answer 1 is incorrect because only stateful session beans can maintain a conversational state whereas stateless session beans cannot.

Answer 2 is incorrect. If the session bean instance stores an object reference to the java:comp/env JNDI context or subcontext in its conversational state, the container saves and restores the reference across the instance's passivation.

Answer 3 is incorrect. A session bean's conversational state is not transactional and is not automatically reset to its initial state if a transaction is rolled back. The afterCompletion notification may be used to manually reset the bean instance's conversational state in the event of a transaction roll back.

Answer 5 is incorrect. Non-transient fields of a session bean's conversational state are maintained across the instance's passivation.



9)1,3

Answers 1 and 3 are correct.

Answer 2 is incorrect. The session bean class can define other methods such as helper methods for internal uses that are not made available to remote clients. However, each method defined in a session bean's remote interface must be defined in the session bean class.

Answer 3 is incorrect. If the session bean class is declared final, the container will be unable to extend the class should it wish to mix in some container-specific code with the session bean class.

Answer 5 is incorrect because the container uses the no-argument constructor to create instances of the session bean class.



10)1,4,5

Answers 1, 4 and 5 are correct. Answer 1 is correct because all session beans must implement the SessionBean interface. Answer 4 is correct. There is no relationship between a client's invocation of a remove operation with the container calling ejbRemove(). The container invokes ejbRemove() when it no longer needs the instances and wishes to reduce the number of instances in the method-ready pool. Answer 5 is correct the container cannot retain open resources when a bean instance is passivated. The bean provider must therefore ensure that any open resources are closed in the ejbPassivate method and reopened in the ejbActivate method.

Answer 2 is incorrect. Unlike the EntityBean interface which has unsetEntityContext() to correspond with setEntityContext(), there is no unsetSesssionContext method defined by the SessionBean interface. If the developer provides an unsetSessionContext method in a session component interface, the container will never call it.

Answer 3 is incorrect because the container never passivates stateless session beans since they do not maintain conversational state.



No comments: