On 4.0.4.GA (w/ EJB RC7), when I get a deadlock, I end up with the following exception hierarchy:
javax.ejb.EJBTransactionRolledbackException
javax.ejb.EJBException
javax.persistence.PersistenceException
org.hibernate.exception.LockAcquisitionException: could not insert: ...
java.sql.SQLException: Deadlock found when trying to get lock; try restarting transaction
Is there a cleaner way to detect this than simply walking down the exception list until an "SQLException" with the desired error code is found?
Secondly, I would like an automatic deadlock handler which retries TX's that end in deadlocks automatically while still using CMT (container managed transactions).
There seems to be a jboss extention for this: org.jboss.ejb.plugins.TxRetryExceptionHandler: http://wiki.jboss.org/wiki/Wiki.jsp?page=RetryingTransactions
However this does not allow me to easily keep track of any state regarding the retry (ie: retry count numbers, data for back off algorithms, etc.)
I can use the EJB3 Interceptor spec, but this ties in *after* the transaction management stuff has been setup. I really would want to intercept before the transaction information is setup, but this would require using the jboss aop stuff I think.
Any thoughts on the cleanest way to implement an automatic TX retry handler on deadlocks while still using CMTs?