Transactions
Example code is available on github.com/vaadin-on-grails3/gorm-transactions.
Grails service layer is the proper place to put all the application logic. Services are transactional and also good place to make your applications code reusable.
In this article will show how to store user with a new account in a transaction. So, when account is saved but saving user fails the transaction is rolled back. That results in scenario that nothing is storred, user nor account.
Step 1
Create domain class that represents an account: grails create-domain-class com.myapp.Account
.
Step 2
Create domain class that represents an user that will have a reference to an accont: grails create-domain-class com.myapp.User
.
Step 3
In this step we create AccountService
that will be transactional, meaning that all methods in the service will be executed in a transaction. When an exception that extends RuntimeException is thrown, the transaction will be rolled back automatically.
It is also possible to define transactional behavior on method level.
Run grails create-service com.myapp.AccountService
to create the service.
Instead of defining
failOnError: true
everywhere, it is possible to enable failOnError globalygrails.gorm.failOnError = true
inConfig.groovy
.
Step 4
Create a view that will execute createUserWithAccount
with dummy values. After the method is executed, try to fetch values from database and print it into the console to see nothing is stored in database because we let the transaction fail.
Step 3
In order to test the behavior, we can use the account view in MyUi class.
When we run the application and a runtime exception is thrown in the service method, we should see no database items fetched from the database. Have a look the console output to see what has happend.
After we remove throwing a runtime exception exception:
We will see that rows has been inserted into the database. Have a look the console output to see what has happend.
Last updated
Was this helpful?