• Post Reply Bookmark Topic Watch Topic
  • New Topic

Catching a timeout exception when using CMT

 
Dean Pullen
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: I'm using CMT on JBoss 3.2.3

I'm trying to think of a way to somehow catch a timeout exception. It happens rarely and we can easily increase the timeout time to avoid it, but it has got me thinking of how to catch a timeout exception so that we can respond to the exception in a more constructive way (as opposed to just an exception in the log).

On the JBoss wiki this is suggested:

http://wiki.jboss.org/wiki/Wiki.jsp?page=WhatIsTheCorrectPatternForUserTransactions

However I think I'm correct in thinking that though the code at the bottom would help (via the finally block), this is purely for BMT.

Can anyone suggest anything?

Thanks.
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dean,

It works with CMT as well. Inside of any business method you need to catch the TransactionTimeoutException, call the EJBContext.setRollbackOnly() and finally rethrow an application exception. This way you can give the client the opportunity to retry the transaction.
By the way the only thing that I don't like about the code on the jboss wiki site, is that it catches all generic Exception(s). From a designing standpoint this might not be the brightest idea.
Regards.
 
Dean Pullen
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I thought the 'UserTransaction ut' was only available when dealing with BMT?
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dean,


But I thought the 'UserTransaction ut' was only available when dealing with BMT?

That�s correct. The only way one can rollback a transaction in CMT is to call the setRollbackOnly on the EJB context object. The code might look like this:
[code]
public void doSomeBusinessLogic() {
try {
// Data access logic here. The next methods are not allowed with CMT:
// 1. The commit, setAutoCommit, and rollback methods of java.sql.Connection
// 2. The getUserTransaction method of javax.ejb.EJBContext
// 3. Any method of javax.transaction.UserTransaction
...
} catch (TransactionTimeoutException ex) {
log.error(...);
context.setRollbackOnly(); // Allowed !!
throw new SomeApplicationException();
}
}
[code]
Regards.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!