Skip to content

Instantly share code, notes, and snippets.

@bastman
Created February 21, 2020 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bastman/c451c68cf6fd0836525e99611fdc3fb0 to your computer and use it in GitHub Desktop.
Save bastman/c451c68cf6fd0836525e99611fdc3fb0 to your computer and use it in GitHub Desktop.
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