-
-
Notifications
You must be signed in to change notification settings - Fork 971
Fix @Where and DetachedCriteria query methods ignoring @Transactional(connection) #15418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jamesfredley
wants to merge
1
commit into
7.0.x
Choose a base branch
from
fix/where-connection-routing
base: 7.0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+403
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
187 changes: 187 additions & 0 deletions
187
...src/test/groovy/org/grails/orm/hibernate/connections/WhereQueryMultiDataSourceSpec.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| /* | ||
| * 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.Shared | ||
| import spock.lang.Specification | ||
|
|
||
| import grails.gorm.annotation.Entity | ||
| import grails.gorm.services.Service | ||
| import grails.gorm.services.Where | ||
| import grails.gorm.transactions.Transactional | ||
| import org.grails.datastore.gorm.GormEnhancer | ||
| import org.grails.datastore.gorm.GormEntity | ||
| import org.grails.datastore.mapping.core.DatastoreUtils | ||
| import org.grails.orm.hibernate.HibernateDatastore | ||
|
|
||
| @Issue("https://github.com/apache/grails-core/issues/15416") | ||
| class WhereQueryMultiDataSourceSpec extends Specification { | ||
|
|
||
| @Shared Map config = [ | ||
| 'dataSource.url':"jdbc:h2:mem:defaultDB;LOCK_TIMEOUT=10000", | ||
| 'dataSource.dbCreate': 'create-drop', | ||
| 'dataSource.dialect': H2Dialect.name, | ||
| 'dataSource.formatSql': 'true', | ||
| 'hibernate.flush.mode': 'COMMIT', | ||
| 'hibernate.cache.queries': 'true', | ||
| 'hibernate.hbm2ddl.auto': 'create-drop', | ||
| 'dataSources.secondary':[url:"jdbc:h2:mem:secondaryDB;LOCK_TIMEOUT=10000"], | ||
| ] | ||
|
|
||
| @Shared @AutoCleanup HibernateDatastore datastore = new HibernateDatastore( | ||
| DatastoreUtils.createPropertyResolver(config), Item | ||
| ) | ||
|
|
||
| @Shared ItemQueryService itemQueryService | ||
|
|
||
| void setupSpec() { | ||
| itemQueryService = datastore | ||
| .getDatastoreForConnection('secondary') | ||
| .getService(ItemQueryService) | ||
| } | ||
|
|
||
| void setup() { | ||
| def api = GormEnhancer.findStaticApi(Item, 'secondary') | ||
| api.withNewTransaction { | ||
| api.executeUpdate('delete from Item') | ||
| } | ||
| GormEnhancer.findStaticApi(Item).withNewTransaction { | ||
| GormEnhancer.findStaticApi(Item).executeUpdate('delete from Item') | ||
| } | ||
| } | ||
|
|
||
| void "@Where query routes to secondary datasource"() { | ||
| given: | ||
| saveToSecondary('Cheap', 10.0) | ||
| saveToSecondary('Expensive', 500.0) | ||
|
|
||
| when: | ||
| def results = itemQueryService.findByMinAmount(100.0) | ||
|
|
||
| then: | ||
| results.size() == 1 | ||
| results[0].name == 'Expensive' | ||
| } | ||
|
|
||
| void "@Where query does not return data from default datasource"() { | ||
| given: 'an item saved to secondary' | ||
| saveToSecondary('OnSecondary', 50.0) | ||
|
|
||
| and: 'a different item saved directly to default' | ||
| saveToDefault('OnDefault', 999.0) | ||
|
|
||
| when: 'querying via @Where for amount >= 500 on secondary-bound service' | ||
| def results = itemQueryService.findByMinAmount(500.0) | ||
|
|
||
| then: 'only secondary data is searched - default item is NOT found' | ||
| results.size() == 0 | ||
| } | ||
|
|
||
| void "count routes to secondary datasource"() { | ||
| given: | ||
| saveToSecondary('A', 1.0) | ||
| saveToSecondary('B', 2.0) | ||
|
|
||
| and: 'an item on default that should not be counted' | ||
| saveToDefault('C', 3.0) | ||
|
|
||
| expect: | ||
| itemQueryService.count() == 2 | ||
| } | ||
|
|
||
| void "list routes to secondary datasource"() { | ||
| given: | ||
| saveToSecondary('X', 10.0) | ||
| saveToSecondary('Y', 20.0) | ||
|
|
||
| and: 'an item on default that should not be listed' | ||
| saveToDefault('Z', 30.0) | ||
|
|
||
| when: | ||
| def all = itemQueryService.list() | ||
|
|
||
| then: | ||
| all.size() == 2 | ||
| } | ||
|
|
||
| void "findByName routes to secondary datasource"() { | ||
| given: | ||
| saveToSecondary('Unique', 77.0) | ||
|
|
||
| when: | ||
| def found = itemQueryService.findByName('Unique') | ||
|
|
||
| then: | ||
| found != null | ||
| found.name == 'Unique' | ||
| found.amount == 77.0 | ||
| } | ||
|
|
||
| private void saveToSecondary(String name, Double amount) { | ||
| def api = GormEnhancer.findStaticApi(Item, 'secondary') | ||
| api.withNewTransaction { | ||
| def instanceApi = GormEnhancer.findInstanceApi(Item, 'secondary') | ||
| def item = new Item(name: name, amount: amount) | ||
| instanceApi.save(item, [flush: true]) | ||
| } | ||
| } | ||
|
|
||
| private void saveToDefault(String name, Double amount) { | ||
| def api = GormEnhancer.findStaticApi(Item) | ||
| api.withNewTransaction { | ||
| def instanceApi = GormEnhancer.findInstanceApi(Item) | ||
| def item = new Item(name: name, amount: amount) | ||
| instanceApi.save(item, [flush: true]) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Entity | ||
| class Item implements GormEntity<Item> { | ||
| Long id | ||
| Long version | ||
| String name | ||
| Double amount | ||
|
|
||
| static mapping = { | ||
| datasource 'ALL' | ||
| } | ||
|
|
||
| static constraints = { | ||
| name blank: false | ||
| amount nullable: false | ||
| } | ||
| } | ||
|
|
||
| @Service(Item) | ||
| @Transactional(connection = 'secondary') | ||
| interface ItemQueryService { | ||
|
|
||
| Item findByName(String name) | ||
|
|
||
| Number count() | ||
|
|
||
| List<Item> list() | ||
|
|
||
| @Where({ amount >= minAmount }) | ||
| List<Item> findByMinAmount(Double minAmount) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,16 +96,16 @@ abstract class AbstractWhereImplementer extends AbstractReadOperationImplementer | |
| body.addStatement( | ||
| declS(queryVar, ctorX(getDetachedCriteriaType(domainClassNode), args(classX(domainClassNode.plainNodeReference)))) | ||
| ) | ||
| Expression connectionId = findConnectionId(newMethodNode) | ||
| body.addStatement( | ||
| assignS(queryVar, callX(queryVar, 'build', closureExpression)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes are in core, but there are additional tests in hibernate. That strongly tells me the test should be part of the TCK. |
||
| ) | ||
| Expression connectionId = findConnectionId(abstractMethodNode) | ||
|
|
||
| if (connectionId != null) { | ||
| body.addStatement( | ||
| assignS(queryVar, callX(queryVar, 'withConnection', connectionId)) | ||
| ) | ||
| } | ||
| body.addStatement( | ||
| assignS(queryVar, callX(queryVar, 'build', closureExpression)) | ||
| ) | ||
| Expression queryExpression = callX(queryVar, getQueryMethodToExecute(domainClassNode, newMethodNode), argsExpression != null ? argsExpression : AstUtils.ZERO_ARGUMENTS) | ||
| body.addStatement( | ||
| buildReturnStatement(domainClassNode, abstractMethodNode, newMethodNode, queryExpression) | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this from test pollution? Why isn't this in cleanup?