Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transaction rollback...and being user friendly

 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a transaction rolls back the client receives the equivalent of a runtime exception. This means that, like any runtime exception (uncaught), usually the stack trace will appear on the console, or the application will stop working. For instance, when I get a NullPointerException for my application (which is a runtime exception), I get the stack trace of what happened (like: something bad happened). This is fine for CMT, as it's the container which manages transactions (unless the bean's developer can force it to rollback), and if it decides to rollback a transaction it's probably because something bad happened; but BMT, it's be bean developer who has got complete control over transaction behaviour, and if a rollback is thrown, this doesn't mean necessarily that something bad as a runtime exception occurred. The bean developer may decide to rollback a transaction if he realizes for instance that there were no enough funds in a bank account: now, is this a reason enough to throw the equivalent of a system exception?
 
Nitin Mehhta
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In case of a BMT if there is a situation where you need to just rollback the transaction then you can call setRollBackOnly in the code and probably throw an application specific exception, which would be more useful in you application and you can set up the client to expect the exception and do the appropriate things instead of crashing down the bean by a system exception. I hope I answered your question.
 
Severin Stoeckli
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marc,

If I understood your question right you expect a "TransactionRolledBackException" in each case the transaction rolls back. That's not true. Even for CMP, the spec in 18.3.6 says:

However, the Container should not throw the java.rmi.RemoteException or
javax.ejb.EJBException if the Container performs a transaction rollback because the instance
has invoked the setRollbackOnly() method on its EJBContext object. In this case, the Container
must rollback the transaction and pass the business method result or the application exception
thrown by the business method to the client.


So it is possibel to rollback a transaction without exception. Or did I undestand you wrong?

Severin
 
alzamabar
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Severin St�ckli:
Hi Marc,

If I understood your question right you expect a "TransactionRolledBackException" in each case the transaction rolls back. That's not true. Even for CMP, the spec in 18.3.6 says:

However, the Container should not throw the java.rmi.RemoteException or
javax.ejb.EJBException if the Container performs a transaction rollback because the instance
has invoked the setRollbackOnly() method on its EJBContext object. In this case, the Container
must rollback the transaction and pass the business method result or the application exception
thrown by the business method to the client.


So it is possibel to rollback a transaction without exception. Or did I undestand you wrong?

Severin



Thank you Severin, this is exactly the answer that I was looking for.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic