permaculture playing cards
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

Query about MDB throwing application exception

Satya P

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: 389
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") })
public class MyMdb implements MessageListener {

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, TOGAF 9, Comptia Cloud+
Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 389
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.

Nikhil Jain
Ranch Hand

Joined: May 15, 2005
Posts: 389
Yeah, so that means, only those application exception that extend run time exception could be throws from MDBs.
I agree. Here's the link:
subject: Query about MDB throwing application exception
It's not a secret anymore!