我在测试配置了多个数据源的 Grails 4 时遇到问题。
@GrailsCompileStatic
@EqualsAndHashCode(includes = 'authority')
@ToString(includes = 'authority', includeNames = true, includePackage = false)
class Role implements Serializable {
String authority
static constraints = {
authority nullable: false, blank: false, unique: true
}
static mapping = {
datasource ConnectionSource.ALL
}
}
class RoleSpec extends Specification implements DataTest {
void setupSpec() {
mockDomain Role
}
void "test nothing"() {
expect:
true
}
}
Invalid connection [ALL] configured for class [class us.cloudcard.api.Role]
org.grails.datastore.mapping.core.exceptions.ConfigurationException: Invalid connection [ALL] configured for class [class us.cloudcard.api.Role]
at org.grails.datastore.mapping.simple.SimpleMapDatastore$4.getDatastoreForQualifier(SimpleMapDatastore.java:256)
at org.grails.datastore.mapping.simple.SimpleMapDatastore$4.getStaticApi(SimpleMapDatastore.java:226)
at org.grails.datastore.gorm.GormEnhancer.registerEntity(GormEnhancer.groovy:154)
at org.grails.datastore.mapping.simple.SimpleMapDatastore$3.persistentEntityAdded(SimpleMapDatastore.java:218)
at org.grails.datastore.mapping.model.AbstractMappingContext.addPersistentEntities(AbstractMappingContext.java:288)
at grails.testing.gorm.DataTest$Trait$Helper.mockDomains(DataTest.groovy:80)
at grails.testing.gorm.DataTest$Trait$Helper.mockDomain(DataTest.groovy:65)
at grails.testing.gorm.DataTest$Trait$Helper.mockDomain(DataTest.groovy:64)
at us.cloudcard.api.RoleSpec.setupSpec(RoleSpec.groovy:10)
environments:
development:
dataSource:
pooled: true
driverClassName: com.mysql.jdbc.Driver
(rest of the dev datasource ...)
test:
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
dataSources:
readReplica:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
我不知道该去哪里,所以我可以使用你能提供的任何帮助。
谢谢!
我明确列出了每个数据源,这解决了问题。
static mapping = {
cache true
datasources([ConnectionSource.DEFAULT, 'readReplica'])
}
但我不太喜欢这个。我不明白为什么
ConnectionSource.ALL
不起作用,因为 docs 说它应该起作用。
SimpleMapDatastore 中似乎没有对 ConnectionSource.ALL 进行处理。 (理论上它应该回退到默认值)
作为一种解决方法,我们可以用以下方法欺骗系统:
test:
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
dataSources:
readReplica:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
ALL:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE