Showing posts with label transactional. Show all posts
Showing posts with label transactional. Show all posts

Sunday, June 17, 2018

Grails on Groovy > Transactions > Transaction Block > Read Only Transactions > With New Transaction > Transactions With Read Only Stage

We all know that Grails services are transactional by default. When you create a service you see that there is a annotation @Transactional exists over class name. Transactional means all transactions will persist on database or no one will persist.
We can set transactions read only mode if we wish in a transactional block if we need. Below is a code snippet:
package com.pkm

import grails.transaction.Transactional
import org.springframework.transaction.TransactionStatus

@Transactional
class HomeService {
    TransactionStatus transactionStatus

    void callMe() {
        Table1 table1 = Table1.last()
        new Table2(table1: table1, subject: "S3", score: 3D).save()
        transactionStatus.setRollbackOnly()

        Table2.withNewTransaction { TransactionStatus tx ->
            table1 = Table1.last()
            new Table2(table1: table1, subject: "S4", score: 3D).save()
            tx.setRollbackOnly()
        }
    }
}
In above code block, we inject a bean named transactionStatus which actually maintain if a service definition is read only or not. We can set it to read only mode using transactionStatus.setRollbackOnly(). We can do the same job for when we do anything in withNewTransaction block. You can see there is another transactionStatus occur for that new region. So you can also set it to read only mode.

Saturday, April 21, 2018

Grails on Groovy > @Transactional does not rollback on checked exceptions

We’re using the Spring Framework in most of our grails applications to manage database transaction.
One of the big advantages is the the declarative transaction handling using the @Transactional attribute.
import org.springframework.transaction.Transactional;
 
@Transactional
public class MyService {
  List exec () {
    
  }
}
That simple annoation on class managed by a Spring ApplicationContext causes all method calls onto that service to be bound to a transaction. The transaction is committed after the method call has left the service again and it’s rollbacked for the case an exception is thrown
But be careful: Only unchecked exceptions (that is, subclasses of java.lang.RuntimeException) are rollbacked by default. For the case, a checked exception is thrown, the transaction will be committed!
And that customization can be done very easily by just adding the parameter rollBackFor to the @Transactional attribute:
import org.springframework.transaction.Transactional;
 
@Transactional(rollbackFor = Exception.class)
public class MyService {
  List exec () {
    
  }
}

Wednesday, May 25, 2016

Grails create read only service

Full read only service


package com.pkm.services.common

import org.springframework.transaction.annotation.Transactional

@Transactional(readOnly = true)
class DataReadService {
    def a() {

    }

    def b() {

    }
}

Some methods (method a) are read only, others are transactional


package com.pkm.services.common

import org.springframework.transaction.annotation.Transactional

@Transactional
class DataReadService {
    @Transactional(readOnly = true)
    def a() {

    }

    def b() {

    }
}