Grails rollback withTransaction on save -


i'm testing withtransaction got problem. why after saved object, cannot rollback it? example

domain1 obj1 = new domain(name: "obj1") domain1.withtransaction { status ->     if(obj1.save(flush:true)){ //save object, has no errors         domain2 obj2 = new domain(name: asdasd) // making error happen         if(!obj2.save(flush:true)){ // dont save, because not string             status.setrollbackonly() // rollback obj1         }     } else{     throw new persistenceexception("error", obj1.errors)     } } 

there's way roll save()?

when using transactions there important considerations must take account regards how underlying persistence session handled hibernate. when transaction rolled hibernate session used gorm cleared. means objects within session become detached , accessing uninitialized lazy-loaded collections lead lazyinitializationexceptions.

to understand why important hibernate session cleared. consider following example:

class author {     string name     integer age  static hasmany = [books: book] } 

if save 2 authors using consecutive transactions follows:

author.withtransaction { status ->     new author(name: "stephen king", age: 40).save()     status.setrollbackonly() }  author.withtransaction { status -> new author(name: "stephen king", age: 40).save() } 

only second author saved since first transaction rolls author save() clearing hibernate session. if hibernate session not cleared both author instances persisted , lead unexpected results.

it can, however, frustrating lazyinitializationexceptions due session being cleared.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -