Skip to content

Commit

Permalink
[#1] Added a reactive transaction example
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Jun 1, 2022
1 parent 2142318 commit fe31678
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.jooq.demo.kotlin

import kotlinx.coroutines.reactive.awaitFirst
import kotlinx.coroutines.reactive.awaitFirstOrNull
import kotlinx.coroutines.reactor.mono
import kotlinx.coroutines.runBlocking
import org.jooq.Configuration
import org.jooq.Records.mapping
import org.jooq.demo.AbstractDemo
import org.jooq.demo.kotlin.db.tables.records.ActorRecord
Expand Down Expand Up @@ -81,9 +84,44 @@ class Demo13Reactive : AbstractDemo() {
return ctx
.selectFrom(ACTOR)
.where(ACTOR.ACTOR_ID.eq(id))

// Turn any reactive streams Publisher<T> into a suspension result using the
// kotlinx-coroutines-reactive extensions
.awaitFirstOrNull()
}

@Test
fun coroutinesWithTransactions() {
val actor: ActorRecord = runBlocking {
insertActorTransaction()
}

println(actor)
}

suspend fun insertActorTransaction(): ActorRecord {
return ctx.transactionPublisher { c ->

// Turn the suspension result into a Mono, which implements the reactive
// streams Publisher<T> SPI, which jOOQ expects as a result from a
// TransactionalPublishable
mono {
insertActor(c)
}
}

// Turn the Publisher<T> that is returned from transactionPublisher() back
// into a suspension result
.awaitFirst();
}

suspend fun insertActor(c: Configuration): ActorRecord = ctx.transactionPublisher { c -> c.dsl()
.insertInto(ACTOR)
.columns(ACTOR.ACTOR_ID, ACTOR.FIRST_NAME, ACTOR.LAST_NAME)
.values(201L, "A", "A")
.returning()
}.awaitFirst()

@After
override fun teardown() {
cleanup(ACTOR, ACTOR.ACTOR_ID)
Expand Down

0 comments on commit fe31678

Please sign in to comment.