Tuesday, January 16, 2007

SCBCD - Exceptions

1)If a system exception is thrown by a bean instance method, which of the following responsibilities belong to neither the bean provider nor the container provider? [Select all correct answers]

1)Logging of errors.
2)Garbage collection.
3)Releasing of managed resources.
4)Monitoring the logs of non-application exceptions.
5)Discarding of bean instances.

2)Under which circumstances will an entity bean throw a javax.ejb.NoSuchEntityException? [Select all correct answers]

1)To indicate that the underlying entity has been removed from the database.
2)When a client of a stateless session bean removes the entity from the database.
3)The entity bean will typically throw the javax.ejb.NoSuchEntityException from within the ejbCreate() method.
4)The entity bean will typically throw the javax.ejb.NoSuchEntityException from within the ejbStore() method.
5)The javax.ejb.NoSuchEntityException does not exist.

3)Given the following scenario, which of the following actions are performed by the container? [Select all correct answers]

The bean method 
* Is defined on an enterprise bean with container-managed transaction.
* Is a business method.
* Runs in the context of a transaction that the container started.
* Throws an application exception.
* Does not mark the transaction for rollback.


1)The transaction is rolled back.
2)The transaction is committed.
3)The bean instance is discarded.
4)The application exception is re-thrown.
5)javax.ejb.EJBException is thrown.

4)What will the container do when a bean managed message driven bean throws a system exception? [Select all correct answers]

1)Log the system exception.
2)Mark the transaction for rollback.
3)The container will discard the message driven bean instance and will not invoke any business methods or container callbacks on the instance.
4)The container will commit the transaction unless the deployment descriptor specifies otherwise
5)The client will receive a RemoteException.

5)Which of the following statements are true when a session bean's client receives a java.rmi.RemoteException? [Select all correct answers]

1)The client will never receives a java.rmi.RemoteException.
2)The client calls the session bean from another JVM.
3)The client calls the session bean from within the same JVM.
4)The container always throws a java.rmi.RemoteException if the container performs a transaction rollback.
5)The throws clauses of all methods in the remote home and component interface must declare a java.rmi.RemoteException.

6)How will the EJB container handle exceptions thrown by a business method of a bean with container managed transaction demarcation? [Select all correct answers]

1)If the instance called setRollbackOnly(), the EJB container will rollback the transaction.
2)If the instance did not call setRollbackOnly(), the EJB container will commit the transaction.
3)The EJB container will throw a RemoteException.
4)If the bean represents an entity bean, it will remove the entity from the datasource.
5)If the bean represents a session bean, the bean will be removed from the pool.

7)Which of the following exceptions are considered to be application exceptions? [Select all correct answers]

1)javax.ejb.CreateException
2)javax.ejb.RemoveException
3)java.rmi.RemoteException
4)
5)

8)Which of the following statements regarding exception handling in EJBs are true? [Select all correct answers]

1)The bean provider is responsible for performing any clean up actions before throwing a non-application exception.
2)The container always discards an enterprise bean instance if one of its business methods throws a non-application exception.
3)A local client cannot continue a transaction if javax.ejb.TransactionRolledbackLocalException is received.
4)A transaction is automatically rolled back by the container if the client initiated the transaction and the bean's business method resulted in an application exception.
5)Message-driven bean methods cannot throw application exceptions to the client.

9)Which of the following exceptions are not RuntimeExceptions? [Select all correct answers]

1)javax.ejb.NoSuchEntityException
2)javax.ejb.EJBException
3)javax.ejb.NoSuchObjectLocalException
4)javax.ejb.FinderException
5)
javax.ejb.DuplicateKeyException

10)Which of the following statements regarding message-driven bean exception handling are true? [Select all correct answers]

1)Message-driven beans throw both application exceptions and system exceptions.
2)If a system exception is thrown by a method, the transaction is always rolled back by the container.
3)If a system exception is thrown by a method, the bean instance is always discarded.
4)If a system exception is thrown by a method, the client does not receive the exception.
5)If an application exception is thrown by a method, the client receives the exception.

Answers:

1)2,4

Answers 2 and 4 are correct. The Java virtual machine's garbage collector mechanism is responsible for garbage collection and the system administrator is responsible for monitoring the logs of the non-application exceptions and errors logged by the container.

Answers 1, 3, and 5 are incorrect because the container is responsible for these if a business method throws a system exception.



2)1,4

Answers 1 and 4 are correct.

The NoSuchEntityException is a subclass of EJBException. It should be thrown by the entity bean class methods to indicate that the underlying entity has been removed from the database. An entity bean class typically throws this exception from the ejbLoad() and ejbStore() methods, and from the methods that implement the business methods defined in the component interface.



3)2,4

Answers 2 and 4 are correct. When the container starts a transaction, it is responsible for controlling the transaction when it receives an exception. If the bean method throws an application exception but does not mark the transaction for rollback, the container will commit the transaction before re-throwing the application exception to the client. If the bean method marks the transaction for rollback, the container will roll back the transaction before re-throwing the application exception to the client.

Answer 1 is incorrect because the bean instance did not mark the transaction for rollback.

Answer 3 is incorrect because the bean instance is only discarded by the container if it receives a non-application error (e.g. system exception).

Answer 5 is incorrect because the container always re-throws the application exception to the client.



4)1,2,3

Answers 1, 2 and 3 are correct.

Log the exception or error means that the Container logs the exception or error so that the System Administrator is alerted of the problem. Answer 4 is incorrect, the container will roll back the transaction, no matter what.

Answer 5 is incorrect, a MDB does not have a client.



5)2,5

Answers 2 and 5 are correct.

The Container catches a non-application exception; logs it (which can result in alerting the System Administrator); and, unless the bean is a message-driven bean, throws the java.rmi.RemoteException (or subclass thereof) to the client if the client is a remote client, or throws the javax.ejb.EJBException (or subclass thereof) to the client if the client is a local client.

Answer 4 is incorrect. The Container should not throw the java.rmi.RemoteException if the Container performs a transaction rollback because the instance has invoked the setRollbackOnly() method on its EJBContext object.



6)1,2
Answers 1 and 2 are correct.
If the instance called setRollbackOnly(), the EJB container will rollback the transaction and re-throw the application exception. If the instance did not call setRollbackOnly(), the EJB container will commit the transaction and re-throw the application exception.

7)1,2,4

Answers 1, 2 and 4 are correct. An application exception class must be a subclass (direct or indirect) of java.lang.Exception and must not be defined as a subclass of java.lang.RuntimeException or of java.rmi.RemoteException.

The javax.ejb.CreateException, javax.ejb.RemoveException, javax.ejb.FinderException, and subclasses thereof are considered to be application exceptions. These exceptions are used as standard application exceptions to report errors to the client from the create, remove, and finder methods.

Answer 3 is incorrect, java.rmi.RemoteException extends java.lang.RuntimeException. Answer 5 is incorrect, javax.ejb.ObjectNotFoundException does not extends java.lang.RuntimeException.



8)2,3,5

Answers 2, 3 and 5 are correct.

Answer 2 is correct. The container always discards an enterprise bean instance because the bean may end up in an inconsistent state as a result of the non-application exception thus becoming unsafe for further use. Discarding the bean prevents both the container and the client from further invoking methods on the bean. Answer 3 is correct because javax.ejb.TransactionRolledbackLocalException (subclass of javax.ejb.EJBException) indicates to the local client that the transaction has definitely been marked for rollback. Attempting to continue with the transaction is pointless because the transaction cannot be committed. Answer 5 is correct. Unlike business methods of a session or entity bean, message-driven bean methods do not throw application exceptions.

Answer 1 is incorrect because the container is responsible for performing any clean up actions before throwing a non-application exception. Answer 4 is incorrect because the container is not responsible for committing or rolling back transactions that have been initiated by the client.



9)4,5

Answers 4 and 5 are correct.

javax.ejb.NoSuchEntityException, javax.ejb.EJBException and javax.ejb.NoSuchObjectLocalException are all subclasses from java.lang.RuntimeException.



10)3,4

Answers 3 and 4 are correct. If a message-driven bean method throws a system exception, the bean may end up in an inconsistent state. Therefore, the container will discard the bean to prevent the bean from receiving further messages. If the bean throws a system exception, the container does not throw the exception back to the client. Instead, the container does not acknowledge receipt of the message, providing the sender with an opportunity to re-send the original message.

Answer 1 is incorrect. Unlike business methods of a session or entity bean, message-driven bean methods do not throw application exceptions.

Answer 2 is incorrect. The container is only responsible for rolling back a system exception for an message-driven bean with container-managed transaction demarcation and when the bean method runs in the context of a transaction started by the container. A transaction is not rolled backed by the container as a result of a system exception thrown when the transaction runs with an unspecified transaction context and also when a bean is declared with bean-managed transaction demaraction.

Answer 5 is incorrect since a message-driven bean method cannot throw application exceptions.


No comments: