Created
February 21, 2020 10:51
-
-
Save bastman/c451c68cf6fd0836525e99611fdc3fb0 to your computer and use it in GitHub Desktop.
playground for spring transactions
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
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