jdaugherty commented on code in PR #16827:
URL: https://github.com/apache/grails-core/pull/16827#discussion_r4450182427
##########
grails-datamapping-core/src/test/groovy/grails/gorm/services/ServiceTransformSpec.groovy:
##########
@@ -181,6 +186,53 @@ return BookService
impl.getMethod("find", Serializable).getAnnotation(Implemented).by()
== FindOneImplementer
}
+ @Issue('https://github.com/apache/grails-core/issues/16801')
+ void 'the transaction manager of a data service is the one of the tenant
bound for its datastore while another datastore has a different tenant'() {
+ given:
+ var service = new GroovyClassLoader().parseClass('''
+import grails.gorm.services.Service
+import grails.gorm.annotation.Entity
+
+@Service(TenantBook)
+interface TenantBookService {
+
+ TenantBook find(Serializable id)
+}
+
+@Entity
+class TenantBook {
+
+ String title
+}
+''')
+ var instance =
service.classLoader.loadClass('$TenantBookServiceImplementation').getDeclaredConstructor().newInstance()
+ var serviceTransactionManager = Stub(PlatformTransactionManager)
+ var tenantTransactionManager = Stub(PlatformTransactionManager)
+ var serviceDatastore = Stub(TransactionCapableDatastore) {
+ getTransactionManager() >> serviceTransactionManager
+ }
+ var otherDatastore = Stub(Datastore)
+ GormRegistry.instance.initializeDatastore(serviceDatastore)
+ GormRegistry.instance.registerDatastore('ds1',
Stub(TransactionCapableDatastore) {
+ getTransactionManager() >> tenantTransactionManager
+ })
+
+ expect:
+ CurrentTenantHolder.withTenant(serviceDatastore, 'ds1') {
Review Comment:
Nothing sets the service's datastore here, so `getDatastore()` is null and
the generated getter takes the `findCurrentTenantTransactionManager(null)`
branch, which reads the tenant bound for the registry's default datastore. The
feature passes, but it exercises the same path as the `@Transactional` feature
in `TransactionalTransformSpec` rather than the service-scoped lookup its name
describes.
Could you register a separate default datastore, set `instance.datastore =
serviceDatastore`, and bind the tenant for `serviceDatastore` only? Then this
expectation holds only when the lookup reads the service's datastore, and the
`and:` block can bind the tenant for the default datastore instead to show that
one does not count.
##########
grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/connections/DatabaseMultiTenantCurrentTenantTransactionalSpec.groovy:
##########
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.grails.orm.hibernate.connections
+
+import org.hibernate.dialect.H2Dialect
+import spock.lang.AutoCleanup
+import spock.lang.Issue
+import spock.lang.Specification
+
+import grails.gorm.MultiTenant
+import grails.gorm.annotation.Entity
+import grails.gorm.multitenancy.CurrentTenant
+import grails.gorm.multitenancy.Tenants
+import grails.gorm.transactions.Transactional
+import org.grails.datastore.gorm.GormEntity
+import org.grails.datastore.mapping.core.DatastoreUtils
+import org.grails.datastore.mapping.core.connections.ConnectionSource
+import org.grails.datastore.mapping.multitenancy.MultiTenancySettings
+import org.grails.datastore.mapping.multitenancy.TenantResolver
+import
org.grails.datastore.mapping.multitenancy.exceptions.TenantNotFoundException
+import org.grails.orm.hibernate.HibernateDatastore
+
+/**
+ * A class that is not a data service, annotated with {@code @CurrentTenant}
and {@code @Transactional}, called
+ * inside {@code Tenants.withId} in DATABASE mode while the tenant resolver
resolves another tenant, as one that
+ * resolves the tenant from the web request does outside a request.
+ */
+@Issue('https://github.com/apache/grails-core/issues/16801')
+class DatabaseMultiTenantCurrentTenantTransactionalSpec extends Specification {
Review Comment:
This is the only test of the fix against a real `HibernateDatastore`, and it
lives in the Hibernate 5 module. The report came from 9.0.x, where Hibernate 7
is the usual path, and `grails-data-hibernate7/core` has no DATABASE-mode test
of `@CurrentTenant` together with `@Transactional`. Could you add the same spec
there? `GormApiAllocationSpec` in the same package of that module builds its
datastore the same way, so the file should port as is.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]