Skip to content
This repository was archived by the owner on Mar 10, 2025. It is now read-only.

Commit de5bba4

Browse files
committed
ISSUES-31: Use TransactionCapableDatastore to get PlatformTransactionManager
instead of using reflection
1 parent ad4f51e commit de5bba4

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

core/src/main/groovy/org/grails/gorm/graphql/fetcher/DefaultGormDataFetcher.groovy

+25-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package org.grails.gorm.graphql.fetcher
33
import grails.gorm.DetachedCriteria
44
import grails.gorm.multitenancy.Tenants
55
import grails.gorm.transactions.GrailsTransactionTemplate
6-
import grails.gorm.transactions.TransactionService
76
import graphql.schema.DataFetcher
87
import graphql.schema.DataFetchingEnvironment
98
import groovy.transform.CompileStatic
@@ -17,11 +16,10 @@ import org.grails.datastore.mapping.model.PersistentProperty
1716
import org.grails.datastore.mapping.model.types.Association
1817
import org.grails.datastore.mapping.multitenancy.MultiTenantCapableDatastore
1918
import org.grails.datastore.mapping.transactions.CustomizableRollbackTransactionAttribute
19+
import org.grails.datastore.mapping.transactions.TransactionCapableDatastore
2020
import org.grails.gorm.graphql.entity.EntityFetchOptions
2121
import org.springframework.transaction.PlatformTransactionManager
2222

23-
import java.lang.reflect.Method
24-
2523
/**
2624
* A generic class to assist with querying entities with GraphQL
2725
*
@@ -111,18 +109,38 @@ abstract class DefaultGormDataFetcher<T> implements DataFetcher<T> {
111109
protected Object withTransaction(boolean readOnly, Closure closure) {
112110
Datastore datastore
113111
if (entity.multiTenant && this.datastore instanceof MultiTenantCapableDatastore) {
114-
MultiTenantCapableDatastore multiTenantCapableDatastore = (MultiTenantCapableDatastore)this.datastore
112+
MultiTenantCapableDatastore multiTenantCapableDatastore = (MultiTenantCapableDatastore) this.datastore
115113
Serializable currentTenantId = Tenants.currentId(multiTenantCapableDatastore)
116114
datastore = multiTenantCapableDatastore.getDatastoreForTenantId(currentTenantId)
117-
}
118-
else {
115+
} else {
119116
datastore = this.datastore
120117
}
121118

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)
123133
CustomizableRollbackTransactionAttribute transactionAttribute = new CustomizableRollbackTransactionAttribute()
124134
transactionAttribute.setReadOnly(readOnly)
125135
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()
126144
}
127145

128146
abstract T get(DataFetchingEnvironment environment)

0 commit comments

Comments
 (0)