13.1 Declarative Transactions 6.0.0

(Quick Reference) # 13.1 Declarative Transactions Version: 6.0.0 ## Table of Contents ## 13.1 Declarative Transactions ### Declarative Transactions Services are typically involved with coordinating logic between domain classes, and hence often involved with persistence that spans large operations. Given the nature of services, they frequently require transactional behaviour. You can use programmatic transactions with the withTransaction method, however this is repetitive and doesn’t fully leverage the power of Spring’s underlying transaction abstraction. Services enable transaction demarcation, which is a declarative way of defining which methods are to be made transactional. To enable transactions on a service use the `Transactional` transform:
    import grails.gorm.transactions.*

    @Transactional
    class CountryService {

    }
The result is that all methods are wrapped in a transaction and automatic rollback occurs if a method throws an exception (both Checked or Runtime exceptions) or an Error. The propagation level of the transaction is by default set to PROPAGATION_REQUIRED.
| Version Grails 3.2.0 was the first version to use GORM 6 by default. Checked exceptions did not roll back transactions before GORM 6. Only a method which threw a runtime exception (i.e. one that extends RuntimeException) rollbacked a transaction.
| Warning: dependency injection is the only way that declarative transactions work. You will not get a transactional service if you use the `new` operator such as `new BookService()`
#### The Transactional annotation vs the transactional property In versions of Grails prior to Grails 3.1, Grails created Spring proxies and used the `transactional` property to enable and disable proxy creation. These proxies are disabled by default in applications created with Grails 3.1 and above in favor of the `@Transactional` transformation. For versions of Grails 3.1.x and 3.2.x, if you wish to renable this feature (not recommended) then you must set `grails.spring.transactionManagement` to true or remove the configuration in `grails-app/conf/application.yml` or `grails-app/conf/application.groovy`. In Grails 3.3.x Spring proxies for transaction management has been dropped completely, and you must use Grails' AST transforms. In Grails 3.3.x, if you wish to continue to use Spring proxies for transaction management you will have to configure them manually, using the appropriate Spring configuration.
| In addition, prior to Grails 3.1 services were transactional by default, as of Grails 3.1 they are only transactional if the `@Transactional` transformation is applied.
### Custom Transaction Configuration Grails also provides `@Transactional` and `@NotTransactional` annotations for cases where you need more fine-grained control over transactions at a per-method level or need to specify an alternative propagation level. For example, the `@NotTransactional` annotation can be used to mark a particular method to be skipped when a class is annotated with `@Transactional`.
| Annotating a service method with `Transactional` disables the default Grails transactional behavior for that service (in the same way that adding `transactional=false` does) so if you use any annotations you must annotate all methods that require transactions.
In this example `listBooks` uses a read-only transaction, `updateBook` uses a default read-write transaction, and `deleteBook` is not transactional (probably not a good idea given its name).
    import grails.gorm.transactions.Transactional

    class BookService {

        @Transactional(readOnly = true)
        def listBooks() {
            Book.list()
        }

        @Transactional
        def updateBook() {
            // ...
        }

        def deleteBook() {
            // ...
        }
    }
You can also annotate the class to define the default transaction behavior for the whole service, and then override that default per-method:
    import grails.gorm.transactions.Transactional

    @Transactional
    class BookService {

        def listBooks() {
            Book.list()
        }

        def updateBook() {
            // ...
        }

        def deleteBook() {
            // ...
        }
    }
This version defaults to all methods being read-write transactional (due to the class-level annotation), but the `listBooks` method overrides this to use a read-only transaction:
    import grails.gorm.transactions.Transactional

    @Transactional
    class BookService {

        @Transactional(readOnly = true)
        def listBooks() {
            Book.list()
        }

        def updateBook() {
            // ...
        }

        def deleteBook() {
            // ...
        }
    }
Although `updateBook` and `deleteBook` aren’t annotated in this example, they inherit the configuration from the class-level annotation. For more information refer to the section of the Spring user guide on Using @Transactional. Unlike Spring you do not need any prior configuration to use `Transactional`; just specify the annotation as needed and Grails will detect them up automatically. #### Transaction status An instance of TransactionStatus is available by default in Grails transactional service methods. Example:
    import grails.gorm.transactions.Transactional

    @Transactional
    class BookService {

        def deleteBook() {
            transactionStatus.setRollbackOnly()
        }
    }
## 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')
        Book save(String title) {
            Book book = new Book(title: title)
            book.save()
            book
        }
    }
    @CompileStatic
    class MovieService {

        @ReadOnly
        List<Movie> findAll() {
            Movie.where {}.findAll()
        }
    }
## 13.1.2 Transactions Rollback and the Session ### Understanding Transactions and the Hibernate Session When using transactions there are important considerations you must take into account with regards to how the underlying persistence session is handled by Hibernate. When a transaction is rolled back the Hibernate session used by GORM is cleared. This means any objects within the session become detached and accessing uninitialized lazy-loaded collections will lead to a `LazyInitializationException`. To understand why it is important that the Hibernate session is cleared. Consider the following example:
    class Author {
        String name
        Integer age

        static hasMany = [books: Book]
    }
If you were to save two authors using consecutive transactions as follows:
    Author.withTransaction { status ->
        new Author(name: "Stephen King", age: 40).save()
        status.setRollbackOnly()
    }

    Author.withTransaction { status ->
        new Author(name: "Stephen King", age: 40).save()
    }
Only the second author would be saved since the first transaction rolls back the author `save()` by clearing the Hibernate session. If the Hibernate session were not cleared then both author instances would be persisted and it would lead to very unexpected results. It can, however, be frustrating to get a `LazyInitializationException` due to the session being cleared. For example, consider the following example:
    class AuthorService {

        void updateAge(id, int age) {
            def author = Author.get(id)
            author.age = age
            if (author.isTooOld()) {
                throw new AuthorException("too old", author)
            }
        }
    }
    class AuthorController {

        def authorService

        def updateAge() {
            try {
                authorService.updateAge(params.id, params.int("age"))
            }
            catch(e) {
                render "Author books ${e.author.books}"
            }
        }
    }
In the above example the transaction will be rolled back if the age of the `Author` age exceeds the maximum value defined in the `isTooOld()` method by throwing an `AuthorException`. The `AuthorException` references the author but when the `books` association is accessed a `LazyInitializationException` will be thrown because the underlying Hibernate session has been cleared. To solve this problem you have a number of options. One is to ensure you query eagerly to get the data you will need:
    class AuthorService {
        ...
        void updateAge(id, int age) {
            def author = Author.findById(id, [fetch:[books:"eager"]])
            ...
In this example the `books` association will be queried when retrieving the `Author`.
| This is the optimal solution as it requires fewer queries than the following suggested solutions.
Another solution is to redirect the request after a transaction rollback:
    class AuthorController {

        AuthorService authorService

        def updateAge() {
            try {
                authorService.updateAge(params.id, params.int("age"))
            }
            catch(e) {
                flash.message = "Can't update age"
                redirect action:"show", id:params.id
            }
        }
    }
In this case a new request will deal with retrieving the `Author` again. And, finally a third solution is to retrieve the data for the `Author` again to make sure the session remains in the correct state:
    class AuthorController {

        def authorService

        def updateAge() {
            try {
                authorService.updateAge(params.id, params.int("age"))
            }
            catch(e) {
                def author = Author.read(params.id)
                render "Author books ${author.books}"
            }
        }
    }
### Validation Errors and Rollback A common use case is to rollback a transaction if there are validation errors. For example consider this service:
    import grails.validation.ValidationException

    class AuthorService {

        void updateAge(id, int age) {
            def author = Author.get(id)
            author.age = age
            if (!author.validate()) {
                throw new ValidationException("Author is not valid", author.errors)
            }
        }
    }
To re-render the same view that a transaction was rolled back in you can re-associate the errors with a refreshed instance before rendering:
    import grails.validation.ValidationException

    class AuthorController {

        def authorService

        def updateAge() {
            try {
                authorService.updateAge(params.id, params.int("age"))
            }
            catch (ValidationException e) {
                def author = Author.read(params.id)
                author.errors = e.errors
                render view: "edit", model: [author:author]
            }
        }
    }
| Quick Reference (hide)