File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query about MDB throwing application exception

 
Satya P
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Nikhil Jain
Ranch Hand
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 254
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic