File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Exception handling in spring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Exception handling in spring" Watch "Exception handling in spring" New topic
Author

Exception handling in spring

chets patel
Ranch Hand

Joined: Apr 27, 2010
Posts: 72

Hi All,
I am using spring with jboss application server. As per my knowledge spring throws DataAccessException. But when I use it in JBoss, it gives me UnexpectedRollbackException.
I am using JTATransactionmanager for transaction management.

It works fine for JUNIT.

Does any one know the reason?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

DataAccessException is for data access exceptions. There is a separate tree in Spring for transaction exceptions. See org.springframework.transaction.TransactionException. The UnexpectedRollbackException falls under this category.


[How To Ask Questions][Read before you PM me]
chets patel
Ranch Hand

Joined: Apr 27, 2010
Posts: 72

Hi Bill Gorder
Thanks for the quick response.
I have not null check in database for one of the field. If I set for that field as null, then in this scenario, I should get DataAccessException. But still I am getting UnexpectedRollbackException.
But I want to check this as constraint violation exception which is coming under DataAccessException(Checked this in JUNIT using JPATransactionManager).
Can you please let me know how should I handle this.

Thanks in advance.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

Well I am sure there is a data access exception somewhere then. The problem is that triggers rollback behavior. The exception you are talking about is thrown from within the Spring transaction infrastructure. If you scroll way up in your logs I am sure you can find the root cause of the exception (you may have to scroll up quite a bit).

Most likely you have outer and inner transactions and you setting propagation rules in such a way that does not work. A lot of the time you can do away with setting any specific propagation at all and use the defaults this is often times sufficient. If you have need to do something else be sure that you understand what it is you are doing first.

This bit from Juergen Hoeller (The guy whose name you see all over the Spring Source code) says this about it in a JIRA which you can find HERE.

I guess we need to differentiate between 'logical' transaction scopes and 'physical' transactions here...

What PROPAGATION_REQUIRED creates is a logical transaction scope for each method that it gets applied to. Each such logical transaction scope can individually decide on rollback-only status, with an outer transaction scope being logically independent from the inner transaction scope. Of course, in case of standard PROPAGATION_REQUIRED behavior, they will be mapped to the same physical transaction. So a rollback-only marker set in the inner transaction scope does affect the outer transaction's chance to actually commit. However, since the outer transaction scope did not decide on a rollback itself, the rollback (silently triggered by the inner transaction scope) comes unexpected at that level - which is why an UnexpectedRollbackException gets thrown.

PROPAGATION_REQUIRES_NEW, in contrast, uses a completely independent transaction for each affected transaction scope. In that case, the underlying physical transactions will be different and hence can commit or rollback independently, with an outer transaction not affected by an inner transaction's rollback status.

PROPAGATION_NESTED is different again in that it uses a single physical transaction with multiple savepoints that it can roll back to. Such partial rollbacks allow an inner transaction scope to trigger a rollback for its scope, with the outer transaction being able to continue the physical transaction despite some operations having been rolled back. This is typically mapped onto JDBC savepoints, so will only work with JDBC resource transactions (Spring's DataSourceTransactionManager).

To complete the discussion: UnexpectedRollbackException may also be thrown without the application ever having set a rollback-only marker itself. Instead, the transaction infrastructure may have decided that the only possible outcome is a rollback, due to constraints in the current transaction state. This is particularly relevant with XA transactions.

As I suggested above, throwing an exception at the inner transaction scope, then catching that exception at the outer scope and translating it into a silent setRollbackOnly call there should work for your scenario. A caller of the outer transaction will never see an exception then. Since you only worry about such silent rollbacks because of special requirements imposed by a caller, I would even argue that the correct architectural solution is to use exceptions within the service layer, and to translate those exceptions into silent rollbacks at the service facade level (right before returning to that special caller).

Since your problem is possibly not only about rollback exceptions, but rather about any exceptions thrown from your service layer, you could even use standard exception-driven rollbacks all the way throughout you service layer, and then catch and log such exceptions once the transaction has already completed, in some adapting service facade that translates your service layer's exceptions into UI-specific error states.

Juergen

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception handling in spring
 
Similar Threads
Hibernate in JBoss
How to call the class in servlet container from the ejb
Jboss clustering
spring mvc validator
Reasons for Using Jboss Rather Than Tomcat Only