Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
playground for spring transactions
fun PlatformTransactionManager.transactionTemplate(propagationBehavior:Int = TransactionDefinition.PROPAGATION_REQUIRED) = TransactionTemplate(this)
.apply {
this.propagationBehavior=propagationBehavior
}
fun <T>TransactionTemplate.tryExecute(block:(TransactionStatus)->T):Try<T> = Try { this.execute(block) as T }
@Component
class MyTx() {
@Transactional
fun <T>inTx(block:()->T):T {
return block()
}
@Transactional(propagation=Propagation.REQUIRES_NEW)
fun <T>inTxNew(block:()->T):T {
return block()
}
@Transactional(propagation=Propagation.NESTED)
fun <T>inTxNested(block:()->T):T {
return block()
}
}
@Component
class MyTx2(
private val myTx:MyTx
) {
fun <T>inTx(block:()->T):T = myTx.inTx(block)
fun <T>tryInTx(block:()->T):Try<T> = myTx.inTx { Try{ block() } }
fun <T>inTxNew(block:()->T):T = myTx.inTxNew(block)
fun <T>tryInTxNew(block:()->T):Try<T> = myTx.inTxNew { Try{ block() } }
fun <T>inTxNested(block:()->T):T= myTx.inTxNested(block)
fun <T>tryInTxNested(block:()->T):Try<T> = myTx.inTxNested { Try{ block() } }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment