Reputation: 441
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.
override suspend fun getAllUsers(): List<User> = dbQuery {
UserDao.selectAll().map {
it.asUser()
}
}
Upvotes: 1
Views: 1260
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