@@ -3,7 +3,6 @@ package org.grails.gorm.graphql.fetcher
3
3
import grails.gorm.DetachedCriteria
4
4
import grails.gorm.multitenancy.Tenants
5
5
import grails.gorm.transactions.GrailsTransactionTemplate
6
- import grails.gorm.transactions.TransactionService
7
6
import graphql.schema.DataFetcher
8
7
import graphql.schema.DataFetchingEnvironment
9
8
import groovy.transform.CompileStatic
@@ -17,11 +16,10 @@ import org.grails.datastore.mapping.model.PersistentProperty
17
16
import org.grails.datastore.mapping.model.types.Association
18
17
import org.grails.datastore.mapping.multitenancy.MultiTenantCapableDatastore
19
18
import org.grails.datastore.mapping.transactions.CustomizableRollbackTransactionAttribute
19
+ import org.grails.datastore.mapping.transactions.TransactionCapableDatastore
20
20
import org.grails.gorm.graphql.entity.EntityFetchOptions
21
21
import org.springframework.transaction.PlatformTransactionManager
22
22
23
- import java.lang.reflect.Method
24
-
25
23
/**
26
24
* A generic class to assist with querying entities with GraphQL
27
25
*
@@ -111,18 +109,38 @@ abstract class DefaultGormDataFetcher<T> implements DataFetcher<T> {
111
109
protected Object withTransaction (boolean readOnly , Closure closure ) {
112
110
Datastore datastore
113
111
if (entity. multiTenant && this . datastore instanceof MultiTenantCapableDatastore ) {
114
- MultiTenantCapableDatastore multiTenantCapableDatastore = (MultiTenantCapableDatastore )this . datastore
112
+ MultiTenantCapableDatastore multiTenantCapableDatastore = (MultiTenantCapableDatastore ) this . datastore
115
113
Serializable currentTenantId = Tenants . currentId(multiTenantCapableDatastore)
116
114
datastore = multiTenantCapableDatastore. getDatastoreForTenantId(currentTenantId)
117
- }
118
- else {
115
+ } else {
119
116
datastore = this . datastore
120
117
}
121
118
122
- TransactionService txService = datastore. getService(TransactionService )
119
+ // To support older versions of GORM
120
+ try {
121
+ PlatformTransactionManager transactionManager = getTransactionManager(datastore)
122
+ CustomizableRollbackTransactionAttribute transactionAttribute = new CustomizableRollbackTransactionAttribute ()
123
+ transactionAttribute. setReadOnly(readOnly)
124
+ new GrailsTransactionTemplate (transactionManager, transactionAttribute). execute(closure)
125
+ } catch (NoSuchMethodException | SecurityException e) {
126
+ log. error(' Unable to find a transaction manager for datastore {}' , datastore. class. name)
127
+ null
128
+ }
129
+
130
+ // Supports 6.1.x+ only
131
+ /*
132
+ TransactionService txService = (TransactionService)datastore.getService((Class<?>)TransactionService)
123
133
CustomizableRollbackTransactionAttribute transactionAttribute = new CustomizableRollbackTransactionAttribute()
124
134
transactionAttribute.setReadOnly(readOnly)
125
135
txService.withTransaction(transactionAttribute, closure)
136
+ */
137
+ }
138
+
139
+ private static PlatformTransactionManager getTransactionManager (Datastore datastore ) {
140
+ if (! datastore instanceof TransactionCapableDatastore ) {
141
+ throw new IllegalArgumentException (" Domain mapped DataStore % should be transactional" )
142
+ }
143
+ return ((TransactionCapableDatastore ) datastore). getTransactionManager()
126
144
}
127
145
128
146
abstract T get (DataFetchingEnvironment environment )
0 commit comments