Wednesday, October 30, 2013

Transactions in Grails to roll back inconsistent data

The withTransaction function will participate in an existing transaction if one already has been started and would start a new one if not. The withNewTransaction method will always start a new transaction regardless of if one has already been started, isolating the code inside that block into it's own transaction (with it's own commit/rollback).

If you think the method you are developing should or could participate in some larger transaction with multiple separate db writes, then you should use withTransaction so that you can participate in a larger transaction if necessary. If you want your write here to be totally isolated from other db writes if another transaction is going on (and not potentially roll back that other transaction if this code fails), then use withNewTransaction.

In regards to your question two, these two will behave the same if they are the only calls being made in an action as they would both start up a new transaction.

def saveInstance = {
   /* Assume 'User' and 'Role' are two difference domain.
   Where Role is as a belongsTo of User.
   So need to first save Role and then User.
   But if Role saved and User not saved for any reason, then what happened???
   Role exists in database???
   For this we need to roll back data.
   Everything inside this block is run in a transaction as the name indicates. */
   User.withTransaction { status ->
      try {
          def role = new Role(rollName: "Role Name").save(flush: true);
          def user = new User(username: "pritom", name: "Pritom K Mondal", role: role).save(flush: true);
      } catch (Exception ex) {
          ex.printStackTrace();
          try {
              /* Rolling back data if any exception happens */
              status.setRollbackOnly();
          } catch (Exception ex2) {
              ex2.printStackTrace();
          }
      }
   }
}

No comments:

Post a Comment