<@U034NPZJXBR> using 1.0.0-rc-1 within Ktor is giv...
# exposed
j
@Oleg Babichev using 1.0.0-rc-1 within Ktor is giving me the classic
No transaction in context.
error when I don't think it should be 🧵
âž• 4
All I have is this main file
Copy code
fun main() {
  runBlocking {
    val database = createDatabase()
    val ktorServer = createKtorServer(database)
    ktorServer.start()
    Thread.sleep(Long.MAX_VALUE)
  }
}

private fun createDatabase() =
  R2dbcDatabase.connect(
    url = "r2dbc:<postgresql://localhost:5432/kairo_sample>",
    user = "highbeam",
    password = "highbeam",
  )

private fun createKtorServer(database: R2dbcDatabase) =
  embeddedServer(
    factory = Netty,
    environment = applicationEnvironment(),
    configure = {
      connector {
        host = "0.0.0.0"
        port = 8080
      }
    },
    module = {
      install(CallLogging)
      routing {
        get("/library-books") {
          suspendTransaction(db = database) {
            LibraryBookTable.selectAll().toList()
          }
          call.respond("It worked!")
        }
      }
    },
  )
and my table
Copy code
object LibraryBookTable : Table("library.library_book") {
  val id: Column<String> =
    text("id")

  override val primaryKey: PrimaryKey = PrimaryKey(id)

  val createdAt: Column<Instant> =
    timestamp("created_at")
      .defaultExpression(CurrentTimestamp)

  val version: Column<Long> =
    long("version")
      .default(0)

  val updatedAt: Column<Instant> =
    timestamp("updated_at")
      .defaultExpression(CurrentTimestamp)

  val title: Column<String?> =
    text("title").nullable()

  val authors: Column<List<String>> =
    array<String>("authors")

  val isbn: Column<String> =
    text("isbn")
      .uniqueIndex("uq__library_book__isbn")
}
I've pushed a minimum repro to https://github.com/hudson155/kairo-sample/tree/feature/min-repro. It's just the 2 files. Running it and hitting with
curl -v <http://localhost:8080/library-books>
causes a 500 with
No transaction in context.
a
I'm also experiencing the same issue. 😢
p
@Oleg Babichev, it seems I had the same issue, but it’s reproducible with a smaller setup 🙂 I bet it’s related to the coroutine context switches and transaction context elements. https://youtrack.jetbrains.com/issue/EXPOSED-869
o
Thank you for mentioning the problem, we also have several issues in YouTrack related to that problem >_< Right now I'm working on the fix. The problem is related to the thread local variables that store the current transaction and loose it in the case of switching thread during coroutine execution. The expected solution is to align the state of these thread local variables to the transaction coroutine context.
🔥 5
j
Thanks Oleg! We appreciate your work on this, and excited to use Exposed 1.0 once this is fixed!
Hey Oleg, any update on when the R2DBC fix might be ready?
Thank you! I see it’s resolved