Monday, March 24, 2014

org.hibernate.envers.exception.AuditException: Unable to create revision because of non-active transaction

The Problem : 

org.hibernate.envers.exception.AuditException: Unable to create revision because of non-active transaction

I was baffled for the most part of this evening trying to figure out what caused Envers to throw an AuditException while creating a new revision. 

Background to understand the exception:


Two frameworks that I used are 
  1.  Envers for database Auditing. BTW  Envers provides  easy auditing/versioning of persistent classes. All that you have to do is annotate your persistent class or some of its properties, that you want to audit, with @Audited. For each audited entity, a table will be created, which will hold the history of changes made to the entity.  
  2. Spring AOP Advisors - Like any other spring application, I have the transaction advisor offered by spring and I also coded additional custom advisors for the services classes. The following flow diagram from spring document explains perfectly my design.



I had declared my custom advisors as below and please notice the order of the advice. Spring would execute the advice interceptors as per the ordered defined in the application context. 


Both of the above intercepters perform some database operations which in turn triggers the Envers to create revision for database auditing. But Envers always failed to create audit records with the exception "org.hibernate.envers.exception.AuditException: Unable to create revision because of non-active transaction".

After digging deeper I found the root cause for the exception.

The Root Cause :  


The transaction advisors had lesser precedence and they were invoked after my custom advisors and it caused the transaction to be completed. When the custom advisors were called, the envers threw the audit exception.


The Solution : 

I set the order for the transaction advisor as below and it solved the problem.





3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I added to order param but still get the same error

    ReplyDelete
  3. so am I

    I read about this error on http://koenserneels.blogspot.com.au/2012/05/migrating-from-hibernate-3-to-4-with.html

    and tried that as well and got

    Could not introspect Hibernate JtaPlatform for SpringJtaSessionContext
    org.hibernate.engine.jndi.JndiException: Unable to lookup JNDI name [java:appserver/TransactionManager



    ReplyDelete

World facing challenges . What can you do?

The world is facing a number of challenges right now, including the war in Ukraine for the past 8 months, another war broke in the middle ea...