Grails Transaction Handling Plugin

  • Authors : Daniel Henrique Alves Lima
0 vote
Dependency :
compile ":transaction-handling:0.1.3"

Documentation Source

Summary

Plugin for advanced management of transactions in Grails. Possibly a backport of http://jira.grails.org/browse/GRAILS-7093.

Installation

grails install-plugin transaction-handling

Description

transaction-handling extends the programmatic transaction management capabilities of Grails (DomainClass.withTransaction() and DomainClass.withNewTransaction ()), and allows the reconfiguration of the standards used by other types of transaction management: implicit in Services (transactional = true) or declaratively through annotations (@Transactional).

Initially proposed as a simple backport of GRAILS-7093, the plugin has evolved to allow the reconfiguration of important attributes of transactions, such as timeout and rollback rules, for example.

Installation

grails install-plugin transaction-handling

Usage

After installing the plugin, a new version of the method withTransaction and the new method withNewTransaction will be added to Grails domain classes. The method withTransaction only creates a new transaction if there is no transaction in progress (propagation behavior = required). On the other hand, the method withNewTransaction will always create a new transaction, regardless of the existence of a previous one (propagation behavior = requires new). Both methods support the redefinition of other characteristics of the transaction, such as isolation level and timeout. For the full list of configurable characteristics, see TransactionDefinition, TransactionAttribute and RuleBasedTransactionAttribute.

Examples of use:

User.withTransaction { }

User.withTransaction(isolation: 'readUncommitted') { }

User.withTransaction(readOnly: true, timeout: 'default') { }

User.withTransaction(propagationBehaviorName: 'PROPAGATION_MANDATORY', timeout: 765) { }

User.withTransaction(propagation: "mandatory") { }

User.withNewTransaction { }

User.withNewTransaction(isolationLevel: TransactionDefinition.ISOLATION_SERIALIZABLE) { }

User.withNewTransaction(propagation: 'supports', readOnly: true, timeout: 612) { }

User.withNewTransaction(isolationLevelName: 'ISOLATION_REPEATABLE_READ') { }

It is still possible to redefine the default values of relevant characteristics of the transaction. Among these characteristics, the most prominent are those that make up the rollback rules: They correspond to a set of exceptions (names or classes) for which the automatic rollback of the transaction can happen or not.

Default settings

grails {
    plugin {
        transactionHandling {

/* AbstractPlatformTransactionManager default settings. It affects any transaction started through a transaction manager. */ global { //timeout }

/* DomainClass.withTransaction/withNewTransaction default settings. */ programmatic { // isolation = // timeout = // readOnly = }

/* @Transactional default settings. */ declarative { // isolation = // timeout = // rollbackFor = // noRollbackFor = }

/* Service default settings. */ implicit { // isolation = // timeout = // readOnly = // rollbackFor = // noRollbackFor = } } } }

Examples of settings

grails {
    plugin {
        transactionHandling {
            programmatic {
                isolation = 'readUncommitted'
                readOnly = true
            }
        }
    }
}

import org.springframework.transaction.TransactionDefinition

grails { plugin { transactionHandling { declarative { isolationLevel = TransactionDefinition.ISOLATION_SERIALIZABLE timeout = 60 } } } }

grails.plugin.transactionHandling.implicit.rollbackFor = [RuntimeException]
grails.plugin.transactionHandling.implicit.noRollbackFor = ['MyBusinessException']

Additional information