https://kotlinlang.org logo
#exposed
Title
# exposed
k

Kevin

01/20/2019, 2:42 PM
Hi all, I’m trying to return values as described in the wiki ( https://github.com/JetBrains/Exposed/wiki/Transactions#accessing-returned-values ), but I’m having trouble The following code successfully prints 10 rows:
Copy code
transaction(Connection.TRANSACTION_SERIALIZABLE, 1) {
    Message
        .slice(Message.text)
        .selectAll()
        .limit(10)
        .forEach { println(it) }
}
But when I try to return the rows and use them outside of the transaction, it doesn’t seem to work:
Copy code
val messages = transaction(Connection.TRANSACTION_SERIALIZABLE, 1) {
    Message
        .slice(Message.text)
        .selectAll()
        .limit(10)
        .toList()
}

messages.forEach { println(it) }
I’m getting
java.lang.IllegalStateException: No transaction in context
. When I set a breakpoint after the transaction,
messages
has length 10, but trying to access any elements throws an exception. I’m pretty new to this and wasn’t able to find anything by googling. Could someone help me figure out what I have wrong?
t

tapac

01/21/2019, 7:56 AM
You problem is into
text
(along with
blob
) field as whose kind of fields don't loaded eager (to not consume whole available memory) and that's why they are require transaction to be loaded. In your case it could be fixed by:
Copy code
Message
        .slice(Message.text)
        .selectAll()
        .limit(10)
        .map { it[Message.text] }
I'll update wiki
k

Kevin

01/21/2019, 8:09 AM
That works, thank you very much!
👌 1
2 Views