JP
11/13/2020, 4:13 AMtransaction
? I want to take one in as a parameter in a function, and then execute the transaction. However, I've only ever seen it in the inline style of transaction {...}
and having it execute thereJoel
11/13/2020, 1:08 PMfun Transaction.doSomething() {
...
}
transaction {
doSomething() // this is Transaction
}
You could technically pass the Transaction
as an argument but I have not seen any code like that.JP
11/14/2020, 3:01 PMinterface Foo {
val doSomething: Transaction.() -> Unit
}
but when I try calling the transaction function, it doesn't like it
fun(f: Foo){
transaction{
f.doSomething() // Errors here
}
}
How come I can't do that, or is there a better way to do it? I essentially just want the interface to have a transaction, but not have it be executed until laterJoel
11/14/2020, 3:05 PMfun Transaction.doSomething()
instead of assigning a val
. Unsure if this is causing an issue.but not have it be executed until laterYou don't want transactions to be kept open for a while. This can cause inconsistencies in the database, tie up valuable pooled resources, etc. Exposed's transaction block model helps nudge you in the direction of concise statements.
JP
11/14/2020, 3:44 PMinterface Foo {
fun doSomething: Transaction.() -> Unit
fun execute() {
transaction {
doSomething()
}
}
}
Joel
11/14/2020, 3:47 PMinterface Foo {
fun Transaction.doSomething()
}
fun Foo.execute() = transaction { doSomething() }