Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EJB TransactionRolledback Exception  RSS feed

 
yc lim
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi experts,
Im working on some ticket booking system and had encounter something that i don't understand.
Whenever i add create a Entity Bean, and if there is a existing record with the same primary key, it will throw me a TransactionRolledbackException. Everything works well if i throw the exception to the web tier(Servlet) and handle it. But i wanted to store some value into the Exception so that my Servlet can get it, i try creating a Customoize Exception that extends TransactionRolledbackException like this :

public class TicketBookedException extends TransactionRolledbackException {
private String x;
//getter and setter
}

and throw this Exception to the web tier :

try {
//book ticket
} catch (TransactionRolledbackException e) {
TicketBookedException tbe = new TicketBookedException();
tbe.setX("xxx");
throw tbe;
}

but when i catch it in my web tier :

try {
bookingManager.book();
} catch (TransactionRolledbackException e) {
TicketBookedException tbe = (TicketBookedException)e;
//...
}

i keep getting a ClassCastException. im quite sure this is legal because i try it on a non ejb senerio by creating 3 simple java class and it works (throwing NullPointerException).

And the other thing im not sure is that in my EJB that use the entity bean. when i catch the TransactionRolledbackException , i must throw it to the web tier or i will get a error as if im not handling it. cant i just handle it silently at the ejb.

Thanks for reading. Hope someone will enlighten me
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!