Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Query about MDB throwing application exception  RSS feed

 
Greenhorn
Posts: 8
  • 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.
 
Ranch Hand
Posts: 185
  • 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.
 
Ranch Hand
Posts: 393
  • 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: 393
  • 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
 
Ranch Hand
Posts: 254
  • 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: 393
  • 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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!