Wednesday, December 18, 2013

Grails withNewSession


Please follow the following scenario:
1. You are saving a domain object 'User'.
2. You are trying to save another domain object 'Activities', indicates that 
   who is saving 'User'.
3. First, 'User' created, so now time to save 'Activities'.
4. But activities failed, what would be the saved user? 'User' would be roll 
   back even if 'Activities' saving between try catch, because of hibernate 
   session?
5. In this scenario you must save your 'Activities' under try catch with 
   'new session', such:

def saveUser = [
    User user = new User(name: "Pritom").save();
    try {
        Activites.withNewSession { sesstion -> 
            new Activities(createdBy: "some name").save();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

No comments:

Post a Comment