• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Query about MDB throwing application exception

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, so that means, only those application exception that extend run time exception could be throws from MDBs.
 
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic