This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I was appearing for Mock test for "Exceptions" on www.ejbcertificate.com site. I came across following question, where correction is required IMO:
Question: Which of the following statements are true when a session bean's client receives a java.rmi.RemoteException? [Check 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 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.
My Answer:You have answered: 2 5 - This is incorrect
Correct Answer:Answer 2, 4 and 5 are correct.
Explanation: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.
My Explanation:Answer 4 is NOT correct. I am aware that there are scenarios when Container performs transaction rollback and throws a java.rmi.RemoteException.However, we can NOT make a generic statement as The container throws a java.rmi.RemoteException if the container performs a transaction rollback.
Container can throw RemoteException and need not rollback transaction (if bean method runs in unspecified transaction context). So one can not say that whenever container throws a java.rmi.RemoteException, container performs transaction rollback.Please refer to 2nd row of Table 15 on page no. 376 of EJB 2.0 Specification.
Container can rollback transaction in case EJBContext.setRollbackOnly() is called before throwing an Application exception by a bean method. Container throws Application Exception to client as it is after it performs transaction rollback. So one also can NOT say that if Container rollbacks transaction, it will always throw a java.rmi.RemoteException.
Please correct me if I am wrong.
Regards,<br />Sandesh<br />(SCJCP, SCWCD, SCBCD - 99%, OCP-1)<br /> <br />Either find a way or create one.
Container can throw RemoteException and need not rollback transaction (if bean method runs in unspecified transaction context)
You are right but is not against what ejbcertificate explanations say at all. It's pure logic and its's very common logic mistake!!!. Think about it: ejbcertificate says: if the container perfoms a transaction rollback, then throws a RemoteException. So if A, then B your answer: not true because if RemoteException (B) then not automatically transaction rollback (A) . So if B, then Not A.
But by saying that, you don't make the ejbcertificate's assemption false. The only way to prove that if A then B is false is to pick up a case where Not B => A (and not if B => Not A).
You say also:
Container can rollback transaction in case EJBContext.setRollbackOnly() is called before throwing an Application exception by a bean method. Container throws Application Exception to client as it is after it performs transaction rollback. So one also can NOT say that if Container rollbacks transaction, it will always throw a java.rmi.RemoteException
Here you are!! That's a logical contradiction with the ejbcertificate assomption: A (container performs transaction roolback) => not B (not RemoteException but ApplicationException)
But, because there's a but, we can always argue that in this case, it's not completely tied to the container to rollback the transaction because you have to do a SetRollBackOnly() in the bean's instance code...
So, to my mind, it's not so easy to say who is right and wrong...
SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
Actually, isn't statement 4 is true regardless of weather a client recieves a RemoteException?, thus it is true in the case where the client recieves a remote exception.
The item about this particular question i have difficulties with is item 2. A session bean client may very well be another session bean in the same ejb jar file under the same jvm. The client (session bean 2, or perhaps mdb) may just be using the remote component interface of the session bean, which forces the container to throw java.rmi.RemoteException. Thus, if client recieves java.rmi.RemoteException, "The client calls the session bean from another JVM" isn't always true. In fact, an ejb client could be a web application that was developed with the intent or ability of being in a separate jvm, but the application assembler could package them into one ear that is deployed to one jvm.
Also, item 5 has nothing at all to do with the statement/condition of the question. The statement in item 5 is true regardless of weather or not the client actually recieves a RemoteException. this is another argument that item 4 should be a valid answer.