jamesfredley commented on code in PR #15406: URL: https://github.com/apache/grails-core/pull/15406#discussion_r2849100042
########## grails-data-hibernate5/docs/src/docs/asciidoc/services/multipleDataSources.adoc: ########## @@ -0,0 +1,268 @@ +//// +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. +//// + +When using Data Services with <<multipleDataSources,multiple datasources>>, the service must declare which connection to use via the `connection` parameter of `@Transactional`. + +==== Routing to a Secondary Datasource + +Given a domain class mapped to a secondary datasource: + +[source,groovy] +---- +class Book { + + String title + String author + + static mapping = { + datasource 'books' + } +} +---- + +Define an interface for your data access methods and an abstract class that declares the connection: + +[source,groovy] +---- +import grails.gorm.services.Service + +interface BookDataService { + + Book get(Serializable id) + + Book save(Book book) + + void delete(Serializable id) + + List<Book> findAllByAuthor(String author) + + Long count() +} +---- + +[source,groovy] +---- +import grails.gorm.services.Service +import grails.gorm.transactions.Transactional +import groovy.transform.CompileStatic + +@CompileStatic +@Service(Book) +@Transactional(connection = 'books') +abstract class BookService implements BookDataService { + // All interface methods are auto-implemented by GORM + // and route to the 'books' datasource automatically. +} +---- + +The `@Transactional(connection = 'books')` annotation on the abstract class ensures that all auto-implemented methods (`get`, `save`, `delete`, `findBy*`, `countBy*`, etc.) route to the `books` datasource. Without this annotation, queries silently route to the default datasource. + +NOTE: The `@Service(Book)` annotation identifies the domain class but does not determine which datasource to use. Even if `Book` declares `datasource 'books'` in its mapping block, you must specify `@Transactional(connection = 'books')` on the abstract class to route operations to the correct datasource. Review Comment: https://github.com/apache/grails-core/pull/15433 -- 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]
