How do I prevent exceptions from causing a transaction rollback under Grails? -


my grails service having issue swallowed exception unrelated transaction causing transaction rollback when unrelated persistance of domain object.

in service have along lines of

updatesomething(domainobj) {     def oldfilename = domainobj.filename     def newfilename = getnewfilename()      domainobj.filename = newfilename     domainobj.save(flush: true)      try {         cleanupoldfile(oldfilename)     } catch (cleanupexception) {         // oh well, log , swallow     } } 

what seeing when have exception when cleaning old file, log , swallow it, still causes transaction rollback, though done updating domain object.

how limit scope transaction complete before clean or there way clean exception not cause rollback?

just record using grails 2.1.1

you can use annotations more fine-grained transaction demarcation. default services transactional, , public methods transactional. if use @transactional annotations, grails doesn't make transactional - have complete control.

runtime exceptions automatically trigger rollbacks, checked exceptions don't. though groovy doesn't required catch checked exceptions, feature spring thing doesn't know groovy exception handling.

transactions implemented wrapping service class instance in proxy. if exception "escapes" proxy, whether it's caught or not, rollback have happened.

so have few options. annotate updatesomething @transactional don't annotate cleanupoldfile:

import org.springframework.transaction.annotation.transactional  @transactional def updatesomething(domainobj) { ... }  def cleanupoldfile(...) {    ... } 

you can annotate cleanupoldfile 1 or more unchecked exceptions shouldn't roll transaction (or in other use cases checked exceptions should), e.g.

@transactional(norollbackfor=[fooexception, barexception]) def cleanupoldfile(...) {    ... } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -