3

I have a migration with a "grailsChange" changeSet, which constantly generates a

org.hibernate.HibernateException: connnection proxy not usable after transaction completion

grailsChange {
   change {
      def list1 = [record1,record2]
      list1.each {
         DomainClass.withTransaction {
            new DomainClass(it).save(failOnError: true)
         }
      }
   }
}
Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
user1298426
  • 3,467
  • 15
  • 50
  • 96
  • 1
    I noticed this too along with withNewTransaction. However withSession and withNewSession do not give this exception. Additionally the exception isn't exactly happening in the change set, as the changes are added. Its a final transaction outside the changeset that seems to be throwing the exception – bythe4mile Mar 28 '14 at 01:22
  • I also tried withNewSession. It does not throw any exception but doesn't add records too. Not sure why but if withNewSession is workign for you then there must problem in the application somewhere. – user1298426 Mar 28 '14 at 06:50
  • 1
    withNewSession works if you add a ctx.getBean('sessionFactory').currentSession.flush() before you exit the closure. – bythe4mile Mar 28 '14 at 17:24

2 Answers2

0

We had the same problem. The solution in our case was, that we called a service method from beforeValidate of the Domain Class and the service was transactional. so Adding static transactional = false to the service fixed our problem.

Alexander Kiefer
  • 546
  • 1
  • 13
  • 30
0

I ran into the same problem when writing a migration that used grailsChange. In my case, it would run hundreds of transactions successfully, and then fail on the outermost transaction that is most likely associated with the grailsChange itself.

Switching from withTransaction to withNewSession resolved the issue.

You should not need to manually flush the new session. If you continue to run into issues, I'd suggest adding validate:true and flush:true to the save call and making use of the error(String message) method in GrailsChange.groovy.

grailsChange {
    change {
        def list1 = [record1,record2]
        list1.each {
            DomainClass.withTransaction {
                def domain = new DomainClass(it)
                if (!domain.save(validate: true, flush: true)) {
                    error("There was a problem saving the domain.")
                }
            }
        }
    }
}
Community
  • 1
  • 1
jstricker
  • 2,132
  • 1
  • 30
  • 44