【问题标题】:Hibernate issue with Database Migration Plugin in GrailsGrails 中数据库迁移插件的休眠问题
【发布时间】:2014-05-04 10:45:18
【问题描述】:

我有一个带有“grailsChange”changeSet 的迁移,它不断 生成一个

org.hibernate.HibernateException:连接代理在之后不可用 交易完成

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

【问题讨论】:

  • 我也注意到了这一点以及 withNewTransaction。但是 withSession 和 withNewSession 没有给出这个例外。此外,由于添加了更改,因此该异常并未完全发生在更改集中。它似乎是在抛出异常的变更集之外的最终事务
  • 我也试过withNewSession。它不会抛出任何异常,但也不会添加记录。不知道为什么,但如果 withNewSession 对你有用,那么应用程序的某个地方肯定有问题。
  • 如果在退出关闭之前添加 ctx.getBean('sessionFactory').currentSession.flush(),withNewSession 有效。

标签: hibernate grails grails-orm grails-2.0 grails-plugin


【解决方案1】:

我在编写使用 grailsChange 的迁移时遇到了同样的问题。在我的例子中,它会成功运行数百个事务,然后在最可能与 grailsChange 本身相关的最外层事务上失败。

withTransaction 切换到withNewSession 解决了这个问题。

should not need to manually flush the new session。如果您继续遇到问题,我建议将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.")
                }
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    我们遇到了同样的问题。在我们的案例中,解决方案是,我们从域类的 beforeValidate 中调用一个服务方法,并且该服务是事务性的。所以向服务添加 static transactional = false 解决了我们的问题。

    【讨论】:

      猜你喜欢
      • 2012-05-20
      • 2013-03-11
      • 2019-08-31
      • 2011-01-07
      • 2015-12-25
      • 1970-01-01
      • 2012-07-10
      • 2013-09-02
      相关资源
      最近更新 更多