13.1.1 Transactions and Multi-DataSources

Given two domain classes such as:

  1. class Movie {
  2. String title
  3. }
  1. class Book {
  2. String title
  3. static mapping = {
  4. datasource 'books'
  5. }
  6. }

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

  1. import grails.gorm.transactions.ReadOnly
  2. import grails.gorm.transactions.Transactional
  3. import groovy.transform.CompileStatic
  4. @CompileStatic
  5. class BookService {
  6. @ReadOnly('books')
  7. List<Book> findAll() {
  8. Book.where {}.findAll()
  9. }
  10. @Transactional('books')
  11. void save(String title) {
  12. Book book = new Book(title: title)
  13. book.save()
  14. book
  15. }
  16. }
  1. @CompileStatic
  2. class MovieService {
  3. @ReadOnly
  4. List<Movie> findAll() {
  5. Movie.where {}.findAll()
  6. }
  7. }