, , . -, , : - , - .
fun <T : Any?> tx(codeBlock: DSLContext.() -> T): T {
return rootContext.txWithReturn(codeBlock)
}
fun <T : Any?> DSLContext.tx(codeBlock: DSLContext.() -> T): T {
var returnVal: T? = null
this.transaction { cfg ->
returnVal = DSL.using(cfg).codeBlock()
}
return returnVal as T
}
. Kotlin , .
fun foo() {
tx {
...
tx {
...
tx {
...
}
}
}
}
actionAbc actionXyz, .
fun DSLContext.actionAbc(parm1: String, parm2: Int) {
...
}
fun DSLContext.actionXyz(parm: Date) {
...
}
, . :
fun higherLevelAction(parm1: String, parm2: Date) {
tx {
actionAbc(parm1, 45)
actionXyz(parm2)
tx {
...
}
}
}
actionAbc actionXyz . , , , . , actionAbc:
fun DSLContext.actionAbc(parm1: String, parm2: Int) {
...
}
fun actionAbc(parm1: String, parm2: Int) {
tx { actionAbc(parm1, parm2) }
}
actionAbc , , , .
, , , .
:
, , , :
@Deprecated("Only call these without an existing transaction!",
level = DeprecationLevel.ERROR)
fun DSLContext.actionAbc(parm1: String, parm2: Int) {
throw IllegalStateException("Only call these without an existing transaction!")
}
fun actionAbc(parm1: String, parm2: Int) {
tx {
...
}
}
- @Deprecation , ERROR. WARNING, , @Suppress("DEPRECATION") .