Yehezkiel L
Yehezkiel L

Reputation: 441

Ktor Exposed java.lang.IllegalStateException: No transaction in context

I'm building ktor server using postgress, exposed, and hikari cp. I was following this https://ktor.io/docs/interactive-website-add-persistence.html. But I encountered error that need to be explain in this thread.

I have datasource to get all users like :

override suspend fun getAllUsers(): Query = dbQuery {
    UserDao.selectAll()
}

Using this code, it will return error as follow:

java.lang.IllegalStateException: No transaction in context.



But when I change the return value to List it's working fine. Can someone explain what is wrong with Query return value? Thank you
override suspend fun getAllUsers(): List<User> = dbQuery {
    UserDao.selectAll().map { 
        it.asUser()
    }
}

Upvotes: 1

Views: 1260

Answers (1)

Andrei Naumets
Andrei Naumets

Reputation: 514

We can't work with queries without transactions. So. The second example closes query and return user's list.

After obtaining the connection, all SQL statements should be placed inside a transaction: description here

Upvotes: 1

Related Questions