grails - 具有3个或更多数据源的GORM集成测试

标签 grails groovy gorm spock grails-3.3

当您尝试与2个以上数据源进行交互时,我的集成测试MultipleDbsITSpec失败,并显示错误“org.hibernate.HibernateException:找不到当前线程的 session ” 。运行应用程序时不会发生此问题,请参阅BootStrap.groovy

enter image description here

进行此设置的正确方法是什么?

我想避免找到的解决方法,该解决方法需要使用带有NewTransaction 闭包的包装GORM查询。这将破坏代码的可读性。

Source Code

环境:

host:TestingMultDbs user$ ./grailsw -version
| Grails Version: 3.3.6
| Groovy Version: 2.4.7
| JVM Version: 1.8.0_181

数据库配置:
    development:
    dataSource:
        dbCreate: create-drop
        url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
        logSql: true
    dataSources:
        db2:
          dbCreate: create-drop
          url: jdbc:h2:mem:devDb2;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
        db3:
          dbCreate: create-drop
          url: jdbc:h2:mem:devDb3;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
test:
    dataSource:
        dbCreate: create-drop
        url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
        logSql: true
    dataSources:
        db2:
          dbCreate: create-drop
          url: jdbc:h2:mem:devDb2;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
        db3:
          dbCreate: create-drop
          url: jdbc:h2:mem:devDb3;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE

测试失败:
// Can only declare 1 @Transactional
//    @Transactional("db2") -- causes db3 to fail
//    @Transactional("db3") -- causes db2 to fail
//    @Transactional(["db2","db3"]) -- doesn't exist
@Transactional
void "Test interaction with all 3 databases"() {
    given:
    new DefaultDb(f1: "test").save()
    new Db2Example(f1: "test").save()
    new Db3Example(f1: "test").save()

    when:
    def value1 = DefaultDb.findByF1("test")
    def value2 = Db2Example.findByF1("test")
    def value3 = Db3Example.findByF1("test")

    then:
    value1
    value2
    value3
}

通过测试:
void "Test default db"() {
    given:
    new DefaultDb(f1: "test").save()

    when:
    def value = DefaultDb.findByF1("test")

    then:
    value
}

@Transactional("db2")
void "Test default db2"() {
    given:
    new Db2Example(f1: "test").save()

    when:
    def value = Db2Example.findByF1("test")

    then:
    value
}

@Transactional("db3")
void "Test default db3"() {
    given:
    new Db3Example(f1: "test").save()

    when:
    def value = Db3Example.findByF1("test")

    then:
    value
}

最佳答案

zyro23 在此处说明解决方案:
https://github.com/grails/grails-core/issues/10383

检查更改注册表。 ChainedTransactionManagerPostProcessor(自3.3.0起默认禁用):http://docs.grails.org/3.3.0/guide/upgrading.html,然后尝试通过 application.yml 重新启用。

grails:
  transaction:
    chainedTransactionManagerPostProcessor:
      enabled: true

使用该配置,忽略连接属性应该可以工作(或者不起作用)。

但请确保您了解链式交易处理的含义,即尽力而为的两阶段提交(“be2pc”)方法。

关于grails - 具有3个或更多数据源的GORM集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51540323/

相关文章:

hibernate - Grails引发非唯一对象异常

grails - 部署时如何为Grails应用程序设置版本

testing - Groovy 断言脚本在 SoapUI 中执行两次

grails - Spring 安全性无法正常工作

android - 如何更新Gradle 4.1的output.outputFile.name.replace?

java - Groovy 到 Java 代码

database - 找不到Grails jdbcsqlexception列

mongodb - 如何使用Gorm(Mongo)保存嵌入式对象的ID字段

hibernate - 自引用列表自定义可更新顺序

hibernate - 如何使用 Joda Time 查询 HQL (Hibernate) 中的日期?