Last active
August 29, 2015 13:57
-
-
Save danvega/9808383 to your computer and use it in GitHub Desktop.
Grails Service Transaction Support Methods (Burt Beckwith Grails Book)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.transaction.interceptor.TransactionAspectSupport | |
import org.springframework.transaction.support.TransactionSynchronizationManager | |
class BootStrap { | |
def grailsApplication | |
def init = { servletContext -> | |
// transaction support for services | |
for( sc in grailsApplication.serviceClasses ){ | |
def metaClass = sc.clazz.metaClass | |
// proxy method support | |
metaClass.getMyProxy = { -> | |
grailsApplication.mainContext.getBean(sc.propertyName) | |
} | |
// returns TransactionStatus | |
metaClass.getCurrentTransactionStatus = { -> | |
if( !delegate.isTransactionActive() ) { | |
return null | |
} | |
TransactionAspectSupport.currentTransactionStatus() | |
} | |
// void, throws NoTransactionException | |
metaClass.setRollbackOnly = { -> | |
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly() | |
} | |
// returns boolean | |
metaClass.isRollbackOnly = { -> | |
if( !delegate.isTransactionActive() ){ | |
return false | |
} | |
delegate.getCurrentTransactionStatus().isRollbackOnly() | |
} | |
// return boolean | |
metaClass.isTransactionActive = { -> | |
TransactionSynchronizationManager.isSynchronizationActive() | |
} | |
} | |
} | |
def destroy = { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment