Search code examples
hibernategrailshibernate-envers

Hibernate Envers in Grails


I would like to use Hibernate Envers in my Grails (v3.0.11) application. Since the grails envers plugins are dead, I tried to do it on my own.

What I did:

in build.gradle, I added the Hibernate Envers dependency compile "org.hibernate:hibernate-envers"

My domain looks like:

import org.hibernate.envers.Audited

@Audited
class Hotel {
    String name
}

in the scaffolded Controller, I only changed:

class HotelController {

    ...

    @Transactional
    def save(Hotel hotel) {
        ...
        Hotel.withTransaction {
            hotel.save flush:true    
        }
        ...
    }

    ...

}

I get the following exception when saving:

URI
    /hotel/save
Class
    org.hibernate.envers.exception.AuditException
Message
    null
Caused by
    Unable to create revision because of non-active transaction

    Line | Method
->> 1142 | runWorker in java.util.concurrent.ThreadPoolExecutor
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    617 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run . . . in java.lang.Thread

Caused by HibernateSystemException: Unable to create revision because of non-active transaction; nested exception is org.hibernate.envers.exception.AuditException: Unable to create revision because of non-active transaction
->>   32 | doCall    in HotelController.groovy
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     96 | doInTransaction in grails.transaction.GrailsTransactionTemplate$2
|     93 | execute . in grails.transaction.GrailsTransactionTemplate
|     31 | $tt__save in HotelController.groovy
|     96 | doInTransaction in grails.transaction.GrailsTransactionTemplate$2
|     93 | execute   in grails.transaction.GrailsTransactionTemplate
|     96 | doInTransaction in grails.transaction.GrailsTransactionTemplate$2
|     93 | execute   in grails.transaction.GrailsTransactionTemplate
|   1142 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    617 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run . . . in java.lang.Thread

Caused by AuditException: Unable to create revision because of non-active transaction
->>   32 | doCall    in HotelController.groovy
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     96 | doInTransaction in grails.transaction.GrailsTransactionTemplate$2
|     93 | execute . in grails.transaction.GrailsTransactionTemplate
|     31 | $tt__save in HotelController.groovy
|     96 | doInTransaction in grails.transaction.GrailsTransactionTemplate$2
|     93 | execute   in grails.transaction.GrailsTransactionTemplate
|     96 | doInTransaction in grails.transaction.GrailsTransactionTemplate$2
|     93 | execute   in grails.transaction.GrailsTransactionTemplate
|   1142 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    617 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run . . . in java.lang.Thread

What am I doing wrong? How to make the transaction "active" ?


Solution

  • try excluding the transitive dependencies of envers. worked for me. i guess pulling in hibernate-entitymanager does not do any good here. so:

    compile("org.hibernate:hibernate-envers") { transitive = false }