13.1.1 Transactions and Multi-DataSources
Given two domain classes such as:
class Movie {
String title
}
class Book {
String title
static mapping = {
datasource 'books'
}
}
You can supply the desired data source to @Transactional
or @ReadOnly
annotations.
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')
void save(String title) {
Book book = new Book(title: title)
book.save()
book
}
}
@CompileStatic
class MovieService {
@ReadOnly
List<Movie> findAll() {
Movie.where {}.findAll()
}
}