Search code examples
grails

What is transactional in Grails?


What is transactional in a easy explanation or a example? I quite not understand it even reading the docs. If i put transactional = true, I can rollback if any error is caused, otherwise, if the transactional = false, means that I can't rollback?


Solution

  • Just to explain the concept of Transaction using an example.

    FYI: All services are transactional by default which means all the methods with in all services are also transactional by default. But if you add transaction = true/false your are breaking the convention so after that you have to mark each of your method transaction or not transactional as needed

    So going to the example assume you have a method that calls another five methods:

    void methodName(){
        task1()
        task2()
        task3()
        task4()
        task5()
    
    }
    

    I diagrammed the above method with this:

    enter image description here

    Now assume when you call the above method and the execution goes fine executing the first three tasks.

    Now the method is diagrammed as follows:
    enter image description here

    If the method is transactional and something wicked happened after executing the third task, all what is done will be rolledback:
    enter image description here

    But if the method is not transactional and something wicked happened after executing the third task, everything done will be committed:

    enter image description here

    Transactional in databases context from wiki:

    In an atomic transaction, a series of database operations either all occur, or nothing occurs.