From 5c759d87bf5dfc6f93b265b058c644411ba357e6 Mon Sep 17 00:00:00 2001 From: Sergey Prytkov Date: Wed, 5 Jun 2024 18:31:10 +0500 Subject: [PATCH] Eva pimp loosen query executer param types (#132) * EVA-PIMP loosen query executer param types * gradle * explain errors clearer --- buildSrc/src/main/kotlin/CI.kt | 2 +- .../persistence/jdbc/JdbcTransactionManager.kt | 2 +- .../jdbc/executor/JdbcQueryExecutor.kt | 5 +++-- .../jdbc/JdbcTransactionManagerSpec.kt | 4 ++-- .../jdbc/executor/JdbcQueryExecutorSpec.kt | 4 ++-- .../vertx/executor/VertxQueryExecutor.kt | 4 ++-- .../vertx/VertxTransactionManagerSpec.kt | 4 ++-- .../vertx/executor/VertxQueryExecutorSpec.kt | 4 ++-- .../razz/eva/persistence/TransactionManager.kt | 16 ++++++---------- 9 files changed, 21 insertions(+), 24 deletions(-) diff --git a/buildSrc/src/main/kotlin/CI.kt b/buildSrc/src/main/kotlin/CI.kt index 74daf64..4303bca 100644 --- a/buildSrc/src/main/kotlin/CI.kt +++ b/buildSrc/src/main/kotlin/CI.kt @@ -3,7 +3,7 @@ import java.lang.Boolean.parseBoolean object Ci { private const val SNAPSHOT_BASE = "0.7.0" - private const val RELEASE_VERSION = "0.6.9" + private const val RELEASE_VERSION = "0.6.10" private val githubSha = System.getenv("GITHUB_SHA") ?: "latest" val publishRelease = System.getProperty("release", "true").let(::parseBoolean) diff --git a/eva-persistence-jdbc/src/main/kotlin/com/razz/eva/persistence/jdbc/JdbcTransactionManager.kt b/eva-persistence-jdbc/src/main/kotlin/com/razz/eva/persistence/jdbc/JdbcTransactionManager.kt index 1fc36d8..b34194a 100644 --- a/eva-persistence-jdbc/src/main/kotlin/com/razz/eva/persistence/jdbc/JdbcTransactionManager.kt +++ b/eva-persistence-jdbc/src/main/kotlin/com/razz/eva/persistence/jdbc/JdbcTransactionManager.kt @@ -13,7 +13,7 @@ import kotlin.coroutines.coroutineContext class JdbcTransactionManager( primaryProvider: ConnectionProvider, replicaProvider: ConnectionProvider, - private val blockingJdbcContext: CoroutineDispatcher = Dispatchers.IO + private val blockingJdbcContext: CoroutineDispatcher = Dispatchers.IO, ) : TransactionManager(primaryProvider, replicaProvider) { override suspend fun withConnection(block: suspend (Connection) -> R): R { diff --git a/eva-persistence-jdbc/src/main/kotlin/com/razz/eva/persistence/jdbc/executor/JdbcQueryExecutor.kt b/eva-persistence-jdbc/src/main/kotlin/com/razz/eva/persistence/jdbc/executor/JdbcQueryExecutor.kt index cb1a5bb..b82f370 100644 --- a/eva-persistence-jdbc/src/main/kotlin/com/razz/eva/persistence/jdbc/executor/JdbcQueryExecutor.kt +++ b/eva-persistence-jdbc/src/main/kotlin/com/razz/eva/persistence/jdbc/executor/JdbcQueryExecutor.kt @@ -1,8 +1,8 @@ package com.razz.eva.persistence.jdbc.executor import com.razz.eva.persistence.ConnectionMode.REQUIRE_EXISTING +import com.razz.eva.persistence.TransactionManager import com.razz.eva.persistence.executor.QueryExecutor -import com.razz.eva.persistence.jdbc.JdbcTransactionManager import org.jooq.DMLQuery import org.jooq.DSLContext import org.jooq.Param @@ -14,9 +14,10 @@ import org.jooq.Table import org.jooq.exception.DataAccessException import org.jooq.impl.DSL import org.postgresql.util.PSQLException +import java.sql.Connection class JdbcQueryExecutor( - private val transactionManager: JdbcTransactionManager, + private val transactionManager: TransactionManager, ) : QueryExecutor { override suspend fun executeSelect( diff --git a/eva-persistence-jdbc/src/test/kotlin/com/razz/eva/persistence/jdbc/JdbcTransactionManagerSpec.kt b/eva-persistence-jdbc/src/test/kotlin/com/razz/eva/persistence/jdbc/JdbcTransactionManagerSpec.kt index e354050..8128c1c 100644 --- a/eva-persistence-jdbc/src/test/kotlin/com/razz/eva/persistence/jdbc/JdbcTransactionManagerSpec.kt +++ b/eva-persistence-jdbc/src/test/kotlin/com/razz/eva/persistence/jdbc/JdbcTransactionManagerSpec.kt @@ -50,7 +50,7 @@ class JdbcTransactionManagerSpec : BehaviorSpec({ Then("Exception thrown") { val ex = shouldThrow { call() } - ex.message shouldBe "Required existing connection" + ex.message shouldBe "Required existing connection but no existing connection was found" } And("Pools were not called") { @@ -86,7 +86,7 @@ class JdbcTransactionManagerSpec : BehaviorSpec({ connection.autoCommit = true connection.close() } - ex.message shouldBe "Required new connection" + ex.message shouldBe "Required new connection but existing connection was found" } And("Pool connections was acquired and returned, connection was rolled back") { diff --git a/eva-persistence-jdbc/src/test/kotlin/com/razz/eva/persistence/jdbc/executor/JdbcQueryExecutorSpec.kt b/eva-persistence-jdbc/src/test/kotlin/com/razz/eva/persistence/jdbc/executor/JdbcQueryExecutorSpec.kt index a934386..91e618e 100644 --- a/eva-persistence-jdbc/src/test/kotlin/com/razz/eva/persistence/jdbc/executor/JdbcQueryExecutorSpec.kt +++ b/eva-persistence-jdbc/src/test/kotlin/com/razz/eva/persistence/jdbc/executor/JdbcQueryExecutorSpec.kt @@ -70,7 +70,7 @@ class JdbcQueryExecutorSpec : BehaviorSpec({ Then("Exception thrown saying there is context missing") { val ex = shouldThrow { storeRun() } - ex.message shouldBe "Required existing connection" + ex.message shouldBe "Required existing connection but no existing connection was found" } And("Connection was not acquired and was not released on delegate provider") { coVerify(exactly = 0) { @@ -98,7 +98,7 @@ class JdbcQueryExecutorSpec : BehaviorSpec({ Then("Exception thrown saying there is context missing") { val ex = shouldThrow { storeRun() } - ex.message shouldBe "Required existing connection" + ex.message shouldBe "Required existing connection but no existing connection was found" } And("Connection was not acquired and was not released on delegate provider") { coVerify(exactly = 0) { diff --git a/eva-persistence-vertx/src/main/kotlin/com/razz/eva/persistence/vertx/executor/VertxQueryExecutor.kt b/eva-persistence-vertx/src/main/kotlin/com/razz/eva/persistence/vertx/executor/VertxQueryExecutor.kt index 4aded59..b0b9071 100644 --- a/eva-persistence-vertx/src/main/kotlin/com/razz/eva/persistence/vertx/executor/VertxQueryExecutor.kt +++ b/eva-persistence-vertx/src/main/kotlin/com/razz/eva/persistence/vertx/executor/VertxQueryExecutor.kt @@ -1,8 +1,8 @@ package com.razz.eva.persistence.vertx.executor import com.razz.eva.persistence.ConnectionMode.REQUIRE_EXISTING +import com.razz.eva.persistence.TransactionManager import com.razz.eva.persistence.executor.QueryExecutor -import com.razz.eva.persistence.vertx.VertxTransactionManager import io.vertx.core.json.Json import io.vertx.kotlin.coroutines.coAwait import io.vertx.pgclient.PgConnection @@ -29,7 +29,7 @@ import java.time.LocalDateTime import java.time.ZoneOffset.UTC class VertxQueryExecutor( - private val transactionManager: VertxTransactionManager + private val transactionManager: TransactionManager, ) : QueryExecutor { override suspend fun executeSelect( diff --git a/eva-persistence-vertx/src/test/kotlin/com/razz/eva/persistence/vertx/VertxTransactionManagerSpec.kt b/eva-persistence-vertx/src/test/kotlin/com/razz/eva/persistence/vertx/VertxTransactionManagerSpec.kt index 7d96f98..40419b1 100644 --- a/eva-persistence-vertx/src/test/kotlin/com/razz/eva/persistence/vertx/VertxTransactionManagerSpec.kt +++ b/eva-persistence-vertx/src/test/kotlin/com/razz/eva/persistence/vertx/VertxTransactionManagerSpec.kt @@ -48,7 +48,7 @@ class VertxTransactionManagerSpec : BehaviorSpec({ Then("Exception thrown") { val ex = shouldThrow { call() } - ex.message shouldBe "Required existing connection" + ex.message shouldBe "Required existing connection but no existing connection was found" } And("Pools were not called") { @@ -84,7 +84,7 @@ class VertxTransactionManagerSpec : BehaviorSpec({ txn.rollback() connection.close() } - ex.message shouldBe "Required new connection" + ex.message shouldBe "Required new connection but existing connection was found" } And("Pool connections was acquired and returned, txn was rolled back") { diff --git a/eva-persistence-vertx/src/test/kotlin/com/razz/eva/persistence/vertx/executor/VertxQueryExecutorSpec.kt b/eva-persistence-vertx/src/test/kotlin/com/razz/eva/persistence/vertx/executor/VertxQueryExecutorSpec.kt index 4fec5c3..7bd49f4 100644 --- a/eva-persistence-vertx/src/test/kotlin/com/razz/eva/persistence/vertx/executor/VertxQueryExecutorSpec.kt +++ b/eva-persistence-vertx/src/test/kotlin/com/razz/eva/persistence/vertx/executor/VertxQueryExecutorSpec.kt @@ -108,7 +108,7 @@ class VertxQueryExecutorSpec : BehaviorSpec({ Then("Exception thrown saying there is context missing") { val ex = shouldThrow { storeRun() } - ex.message shouldBe "Required existing connection" + ex.message shouldBe "Required existing connection but no existing connection was found" } And("Connection was not acquired and was not released on delegate provider") { coVerify(exactly = 0) { @@ -136,7 +136,7 @@ class VertxQueryExecutorSpec : BehaviorSpec({ Then("Exception thrown saying there is context missing") { val ex = shouldThrow { storeRun() } - ex.message shouldBe "Required existing connection" + ex.message shouldBe "Required existing connection but no existing connection was found" } And("Connection was not acquired and was not released on delegate provider") { coVerify(exactly = 0) { diff --git a/eva-persistence/src/main/kotlin/com/razz/eva/persistence/TransactionManager.kt b/eva-persistence/src/main/kotlin/com/razz/eva/persistence/TransactionManager.kt index c0a99d5..5e1ab70 100644 --- a/eva-persistence/src/main/kotlin/com/razz/eva/persistence/TransactionManager.kt +++ b/eva-persistence/src/main/kotlin/com/razz/eva/persistence/TransactionManager.kt @@ -8,7 +8,7 @@ import kotlin.coroutines.coroutineContext abstract class TransactionManager( private val primaryProvider: ConnectionProvider, - private val replicaProvider: ConnectionProvider + private val replicaProvider: ConnectionProvider, ) { open suspend fun withConnection(block: suspend (C) -> R): R { @@ -37,9 +37,9 @@ abstract class TransactionManager( mode: ConnectionMode, block: suspend (C) -> R ): R { - checkCtxConnectionMode(mode) return when (val existingConn = ctxConnection()) { null -> { + check(mode == REQUIRE_NEW) { "Required existing connection but no existing connection was found" } var newConn: C? = null try { newConn = primaryProvider.acquire() @@ -63,7 +63,10 @@ abstract class TransactionManager( // because we are in recursive call to inTransaction // and this connection was created upwards the callstack during first call to inTransaction // and will be handled there - else -> block(existingConn) + else -> { + check(mode == REQUIRE_EXISTING) { "Required new connection but existing connection was found" } + block(existingConn) + } } } @@ -74,13 +77,6 @@ abstract class TransactionManager( replicaProvider } - private suspend fun checkCtxConnectionMode(mode: ConnectionMode) { - when (mode) { - REQUIRE_NEW -> check(ctxConnection() == null) { "Required new connection" } - REQUIRE_EXISTING -> check(ctxConnection() != null) { "Required existing connection" } - } - } - abstract fun supportsPipelining(): Boolean protected abstract fun wrapConnection(newConn: C): ConnectionWrapper