简体   繁体   English

Grails中的数据库迁移插件的Hibernate问题

[英]Hibernate issue with Database Migration Plugin in Grails

I have a migration with a "grailsChange" changeSet, which constantly generates a 我有一个带有“grailsChange”changeSet的迁移,它不断生成一个

org.hibernate.HibernateException: connnection proxy not usable after transaction completion org.hibernate.HibernateException:连接代理在事务完成后不可用

grailsChange {
   change {
      def list1 = [record1,record2]
      list1.each {
         DomainClass.withTransaction {
            new DomainClass(it).save(failOnError: true)
         }
      }
   }
}

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. 在我们的案例中的解决方案是,我们从Domain Class的beforeValidate调用了一个服务方法,并且该服务是事务性的。 so Adding static transactional = false to the service fixed our problem. 所以向服务添加静态transactional = false修复了我们的问题。

I ran into the same problem when writing a migration that used grailsChange . 在编写使用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. 在我的例子中,它将成功运行数百个事务,然后在最可能与grailsChange本身关联的最外层事务上失败。

Switching from withTransaction to withNewSession resolved the issue. withTransaction切换到withNewSession解决了这个问题。

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 . 如果你继续遇到问题,我建议在保存调用中添加validate:trueflush:true ,并使用GrailsChange.groovy中的error(String message)方法。

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.")
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM