aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Query about MDB throwing application exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Query about MDB throwing application exception" Watch "Query about MDB throwing application exception" New topic
Author

Query about MDB throwing application exception

Satya P
Greenhorn

Joined: Jul 01, 2008
Posts: 8
It is said as follows in Enthuware mock test description for a query:
------------------------
Message-driven bean�s message listener method must not throw the java.rmi.RemoteException. It can throw any other exception. If it throws an application exception, the transaction (if any) is rolled back, the exception is rethrown to the resource adapter, and the bean is NOT discarded. If it throws a system exception, the exception is logged, the transaction (if any) is rolledback, and the bean is discarded.
------------------------

Q) Why should the transaction be rolled back when application exception is thrown?
My understanding is that, By default the Transaction is not rolled back when an application exception is thrown.
nitin pai
Ranch Hand

Joined: May 30, 2006
Posts: 185
The description is wrong. The transaction should be rolled back only if the application exception has been explicitly marked for rollback either in the annotation or the bean method uses EJBContext.setRollbackOnly() before throwing the exception.



Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
If you happen to implement MessageListener interface, you cannot throw Application Exception. I tried this. I get
Exception MyException is not compatible with throws clause in MessageListener.onMessage(Message)

MyException is an application exception.

package mdb;

import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJBException;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import exceptions.MyException;

@MessageDriven( activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/testQueue") })
@TransactionManagement(TransactionManagementType.BEAN)
public class MyMdb implements MessageListener {

@Resource
private MessageDrivenContext msgContext;

public void onMessage(Message msg) throws MyException {
TextMessage tm = (TextMessage) msg;
System.out.println("Mdb: ");
// msgContext.setRollbackOnly();
//throw new EJBException("tt");
throw new MyException("Test");

}

}

Is'nt this strange?


SCJP 1.4, SCWCD 1.4, SCBCD 1.5
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
I think prior to EJB 3, MDB were not able to throw Application Exception. With EJB 3, they can throw Application Exception only if we use Annotations rather than implementing Message Listener.

But I don't understand the point of rethrowing Application Exception, as there is no client. These exceptions are only propoaged to resource adapters
J J Wright
Ranch Hand

Joined: Jul 02, 2008
Posts: 254
Remember that application exceptions can also be runtime exceptions, so you CAN implement the message listener interface and still throw an application exception.


SCJP, SCWCD, SCBCD, SCEA 5
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 385
Yeah, so that means, only those application exception that extend run time exception could be throws from MDBs.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query about MDB throwing application exception