////
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.
////

Given two domain classes such as:

[source, groovy]
----
class Movie {
    String title
}
----

[source, groovy]
----
class Book {
    String title

    static mapping = {
        datasource 'books'
    }
}
----

You can supply the desired data source to `@Transactional` or `@ReadOnly` annotations.

[source, groovy]
----
import grails.gorm.transactions.ReadOnly
import grails.gorm.transactions.Transactional
import groovy.transform.CompileStatic

@CompileStatic
class BookService {

    @ReadOnly('books')
    List<Book> findAll() {
        Book.where {}.findAll()
    }

    @Transactional('books')
    Book save(String title) {
        Book book = new Book(title: title)
        book.save()
        book
    }
}
----

TIP: GORM Data Services offer a simpler approach. Annotate the `@Service` abstract class with `@Transactional(connection = 'books')` and all auto-implemented methods - `get()`, `save()`, `delete()`, `findBy*()`, `countBy*()` - route to the correct datasource automatically. See the <<multipleDatasources,Data Services and Multiple Datasources>> section for the full pattern.

[source, groovy]
----
@CompileStatic
class MovieService {

    @ReadOnly
    List<Movie> findAll() {
        Movie.where {}.findAll()
    }
}
----

NOTE: If you are using GORM Data Services (the `@Service` annotation), the datasource is automatically inherited from the domain class's `mapping` block, so you do not need to repeat `@Transactional(connection = '...')` on the service. An explicit annotation on the service still takes precedence. See the "GORM Data Service Datasource Inheritance" section under "Multiple Datasources" in the Data Sources configuration guide for details.
