matrei commented on code in PR #16827:
URL: https://github.com/apache/grails-core/pull/16827#discussion_r4452824904
##########
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:
Good catch, though `getDatastore()` was not null here. For a data service
with a domain class, the generated `getDatastore()` returns
`GormRegistry.getDatastore(TenantBook)`, which falls back to the registry's
default datastore, and the feature registered `serviceDatastore` as the
default. So the service's datastore and the default datastore were the same,
and the feature could not tell the two lookups apart, as you say.
`instance.datastore = serviceDatastore` does not change that: that getter
ignores the field `setDatastore` sets for a service with a domain class, and
resolves through the registry. The feature now registers a separate default
datastore, maps `TenantBook` to `serviceDatastore` with
`GormRegistry.registerEntityDatastore`, and checks that `instance.datastore` is
`serviceDatastore`. The tenant of the default datastore is the other one in the
first expectation, and the only one in the `and:` block. It fails when the
generated getter calls `findCurrentTenantTransactionManager()` without the
service's datastore.
##########
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:
Added to `grails-data-hibernate7/core`. It ported as is, apart from the name
of the H2 database. Both features fail without the change there too, the first
with the `TenantException` from the issue.
--
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]