Tuesday, January 16, 2007

SCBCD Mock Questions - CMP Entity Bean Life Cycle

1)Which of the following methods can the container execute and will not move the entity bean's instance from the pooled state to the ready state? [Select all correct answers]

1)Execute the entity bean's finder methods.
2)Execute the entity bean's home methods.
3)Execute the entity bean's create methods.
4)Execute the entity bean's activate/passivate methods.
5)Execute any business methods.

2)Which of the following methods from the EntityContext interface are allowed to be used within your CMP entity bean's ejbPassivate() method? [Select all correct answers]

1)getEJBObject()
2)getPrimaryKey()
3)getEJBHome()
4)getUserTransaction()
5)getRollbackOnly()

3)Which of the following methods must the entity bean provider implement in the abstract entity bean class? [Select all correct answers]

1)The entity bean provider must provide a public constructor without arguments.
2)The entity bean provider must provide at least one implementation for the ejbCreate() method.
3)The entity bean provider must provide the implementation of the ejbFind() method(s).
4)The entity bean provider must provide the implementation of the ejbHome() method(s).
5)The entity bean provider must provide at least one ejbSelect() method.

4)Which of the following methods from the EntityContext interface are allowed to be used within your CMP entity bean's ejbCreate() method? [Select all correct answers]

1)getEJBLocalHome()
2)getEJBHome()
3)getEJBObject()
4)getCallerPrincipal()
5)getUserTransaction()

5)Which of the following statements are correct about the responsibilities of the EJB container? [Select all correct answers]

1)When the container invokes the ejbRemove() method the container removes immediately the entity from the database.
2)The container synchronizes the instance's state before it invokes the ejbRemove() method and uses the ejbLoad() method.
3)The container is responsible for providing the implementation of the ejbFind() methods
4)After the ejbHome() method completes, the instance remains in the ready state.
5)he container is responsible for calling the ejbCreate() method.

6)Identify all correct requirements for a CMP entity bean class? [Select all correct answers]

1)The class must be defined as public.
2)The class must define a public constructor that takes no arguments.
3)The class must not be defined as abstract.
4)The class must define the finalize() method.
5)The class must implement, directly or indirectly, the javax.ejb.EntityBean interface.

7)Which of the following are valid operations in the setEntityContext method, after the CMP entity bean has been instantiated? [Select all correct answers]

1)Get a reference to your home interface.
2)Get a reference to your EJBObject
3)Get a reference to your JNDI environment.
4)Get a reference to the resource manager.
5)Get security related information about the calling client application.

8)Which of the following methods can be called to change the state of the CMP entity bean from pooled to the ready state? [Select all correct answers]

1)ejbStore()
2)ejbCreate()
3)ejbActivate()
4)ejbFind()
5)setEntityContext()

9)Which of the following are valid ejbCreate() declarations in a CMP entity bean? [Select all correct answers]

1)public String ejbCreate(String s) throws CreateException
2)public static String ejbCreate(String s) throws CreateException
3)public final String ejbCreate(String s) throws CreateException
4)public Integer ejbCreateAllCompanies() throws CreateException
5)public void ejbCreateAllCompanies() throws CreateException

10)The ______ is called by the EJB container when the entity bean is about to return to the pool of entity bean's, after a transaction has completed. [Fill in the missing method name without brackets]

Answers:

1)1,2

Answers 1 and 2 are correct. After the container has created the entity bean, with newInstance() and the entityContext has been set the instance becomes part of the pool. While the instance is in the pooled state, the container may use the instance to execute any of the entity bean's finder methods or any of the entity bean's home methods.

The instance does not move to the ready state during the execution of a finder or a home method. An ejbSelect() method may be called by an entity bean's home method while the instance is in the pooled state.

Answer 3 is incorrect, executing the ejbCreate method will move the entity bean from the pooled to the ready state.

Answer 4 is incorrect, executing the ejbActivate method will move the entity bean from the pooled to the ready state and the ejbPassivate method moves the entity bean from the ready state to the pooled state.

Answer 5 is incorrect, business methods will be executed on entity bean's that are in the ready state.



2)1,2,3

Answers 1, 2 and 3 are correct.

At the moment the container passivate the bean is no longer associated with a particular client or transaction. The container invokes this method on an entity bean instance at passivation time (i.e. when the instance is being disassociated from an entity object identity and moved into the pool). The container must ensure that the identity of the associated entity object is still available to the instance if the instance invokes the getPrimaryKey(), getEJBLocalObject(), or getEJBObject() method on its entity context.

The container invokes this method with an unspecified transaction context.



3)1,4

Answers 1 and 4 are correct.

Answer 2 is incorrect, there are zero or more ejbCreate() methods, whose signatures match the signatures of the create() methods of the entity bean's home interface.

Answer 3 is incorrect, the bean provider of an entity bean with container-managed persistence does not write the finder methods. The finder methods are generated at the entity bean deployment time using the Container Provider's tools.

Answer 5 is incorrect, the Bean Provider may provide zero or more select methods. A select method is a query method that is not directly exposed to the client in the home or component interface. The bean provider typically calls a select method within a business method. The bean provider defines the select methods as abstract methods.



4)1,2,4

Answers 1, 2 and 4 are correct. The following methods can be used from within the ejbCreate() method:

  • getEJBLocalHome
  • getEJBHome
  • getCallerPrincipal
  • getRollbackOnly
  • isCallerInRole
  • setRollbackOnly

Within the ejbCreate() method you can access the JNDI environment and you have access to the resource manager.

CMP entity bean instances must not call the getUserTransaction(). If the entity bean tries to access methods that are not allowed then the container will throw IllegalStateException.



5)2,3,5

Answer 1 is incorrect, the container may delete the representation of the entity in the database immediately, or it can defer it to a later time, depending on the caching strategy that it uses.

Answer 2 is correct, the persistent state of the instance at the beginning of the ejbRemove() method is the same as it would be at the beginning of a business method. The container must invoke ejbLoad before it invokes ejbRemove().

Answer 3 is correct, the container invokes the ejbFind() method on an instance when a client invokes a matching find() method on the entity bean's home interface.

Answer 4 is incorrect, the ejbHome() method is executed on an entity bean in the pooled state.

Answer 5 is correct.



6)1,2,5

Answers 1, 2 and 5 are correct.

The following are the requirements for a CMP entity bean class:

  • The class must implement, directly or indirectly, the javax.ejb.EntityBean interface.
  • The class must be defined as public and must be abstract.
  • The class must define a public constructor that takes no arguments.
  • The class must not define the finalize() method.
  • The entity bean class must implement the business methods, and the ejbCreate() and ejbPostCreate() methods.
  • The entity bean class must implement the ejbHome() methods that correspond to the home business methods specified in the bean's home interface.
  • The entity bean class must implement the get and set accessor methods of the bean's abstract persistence schema as abstract methods.
  • The entity bean class does not implement the finder methods. The implementations of the finder methods are provided by the container.
  • The entity bean class must implement any ejbSelect() methods as abstract methods.


7)1,3

Answers 1 and 3 are correct.

An entity bean instance's life starts when the container creates the instance using newInstance(). The container then invokes the setEntityContext() method to pass the instance a reference to the EntityContext interface. The EntityContext interface allows the instance to invoke services provided by the container and to obtain the information about the caller of a client-invoked method.

At this moment the only valid operations within the setEntityContext() method are:

  1. getting a reference to your home with getEJBHome() or getEJBLocalHome().
  2. having access to your JNDI environment.

It is not possible to get a reference to your EJBObject or to get security information about the client at this moment. If the entity bean tries to access methods that are not allowed then the container will throw an IllegalStateException.1,2,5



8)2,3

Answers 2 and 3 are correct.

An instance transitions from the pooled state to the ready state when the container selects that instance to service a client call to an entity object. There are two possible transitions from the pooled to the ready state: through the ejbCreate() and ejbPostCreate() methods, or through the ejbActivate() method.

The ejbActivate() invokes this method on the instance when the container picks the instance from the pool and assigns it to a specific entity object identity.



9)1,4

Answers 1 and 4 are correct.

The following rules need to be followed to declare a correct ejbCreate() method:

  • The method name must have ejbCreate as its prefix.
  • The method must be declared as public.
  • The method must not be declared as final or static.
  • The return type must be the entity bean's primary key type.
  • The return type must be serializable.


10)

ejbPassivate is correct.

The container can choose to passivate an entity bean instance within a transaction. To passivate an instance, the container first invokes the ejbStore method to allow the instance to prepare itself for the synchronization of the database state with the instance's state, and then the container invokes the ejbPassivate method to return the instance to the pooled state.


No comments: