Kotlin协程支持
适用于 ✅ 开源版 ✅ 专业版 ✅ 企业版
在 Kotlin 中,协程和挂起函数是实现异步、流式逻辑的常用方法。从 jOOQ 3.17 开始,jooq-kotlin-coroutines
扩展模块允许桥接 响应式流 API 和协程 API。为此,只需添加以下依赖项
Maven
Gradle (Kotlin)
Gradle (Groovy)
<dependency> <!-- Use org.jooq for the Open Source Edition org.jooq.pro for commercial editions with Java 21 support, org.jooq.pro-java-17 for commercial editions with Java 17 support, org.jooq.pro-java-11 for commercial editions with Java 11 support, org.jooq.pro-java-8 for commercial editions with Java 8 support, org.jooq.trial for the free trial edition with Java 21 support, org.jooq.trial-java-17 for the free trial edition with Java 17 support, org.jooq.trial-java-11 for the free trial edition with Java 11 support, org.jooq.trial-java-8 for the free trial edition with Java 8 support Note: Only the Open Source Edition is hosted on Maven Central. Install the others locally using the provided scripts, or access them from here: https://repo.jooq.org See the JDK version support matrix here: https://jooq.org.cn/download/support-matrix-jdk --> <groupId>org.jooq</groupId> <artifactId>jooq-kotlin</artifactId> <version>3.20.5</version> </dependency> <dependency> <groupId>org.jooq</groupId> <artifactId>jooq-kotlin-coroutines</artifactId> <version>3.20.5</version> </dependency>
dependencies { // Use org.jooq for the Open Source Edition // org.jooq.pro for commercial editions with Java 21 support, // org.jooq.pro-java-17 for commercial editions with Java 17 support, // org.jooq.pro-java-11 for commercial editions with Java 11 support, // org.jooq.pro-java-8 for commercial editions with Java 8 support, // org.jooq.trial for the free trial edition with Java 21 support, // org.jooq.trial-java-17 for the free trial edition with Java 17 support, // org.jooq.trial-java-11 for the free trial edition with Java 11 support, // org.jooq.trial-java-8 for the free trial edition with Java 8 support // // Note: Only the Open Source Edition is hosted on Maven Central. // Install the others locally using the provided scripts, or access them from here: https://repo.jooq.org // See the JDK version support matrix here: https://jooq.org.cn/download/support-matrix-jdk implementation("org.jooq:jooq-kotlin:3.20.5") implementation("org.jooq:jooq-kotlin-coroutines:3.20.5") }
dependencies { // Use org.jooq for the Open Source Edition // org.jooq.pro for commercial editions with Java 21 support, // org.jooq.pro-java-17 for commercial editions with Java 17 support, // org.jooq.pro-java-11 for commercial editions with Java 11 support, // org.jooq.pro-java-8 for commercial editions with Java 8 support, // org.jooq.trial for the free trial edition with Java 21 support, // org.jooq.trial-java-17 for the free trial edition with Java 17 support, // org.jooq.trial-java-11 for the free trial edition with Java 11 support, // org.jooq.trial-java-8 for the free trial edition with Java 8 support // // Note: Only the Open Source Edition is hosted on Maven Central. // Install the others locally using the provided scripts, or access them from here: https://repo.jooq.org // See the JDK version support matrix here: https://jooq.org.cn/download/support-matrix-jdk implementation "org.jooq:jooq-kotlin:3.20.5" implementation "org.jooq:jooq-kotlin-coroutines:3.20.5" }
现在,您可以在 Kotlin 协程风格中使用 jOOQ
suspend fun findActor(id: Long): ActorRecord? { return create .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() }
并将您的事务代码包装在 org.jooq.kotlin.coroutines.transactionCoroutine()
扩展函数中
suspend fun insertActorTransaction(): ActorRecord { return ctx.transactionCoroutine(::insertActor) } suspend fun insertActor(c: Configuration): ActorRecord = c.dsl() .insertInto(ACTOR) .columns(ACTOR.ACTOR_ID, ACTOR.FIRST_NAME, ACTOR.LAST_NAME) .values(201L, "A", "A") .returning() .awaitFirst()
虽然 jOOQ 默认情况下在 JDBC 之上实现了 响应式流 API(以阻塞方式),但如果您希望您的协程真正实现非阻塞,我们建议您考虑切换到 R2DBC 驱动程序的使用。
反馈
您对此页面有任何反馈吗? 我们很乐意听取您的意见!