diff --git a/build.gradle.kts b/build.gradle.kts index 153a734..ff61931 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -18,7 +18,7 @@ plugins { allprojects { group = "com.himadieiev" - version = "1.3.1" + version = "1.3.2" repositories { mavenCentral() diff --git a/redpulsar-lettuce/src/main/kotlin/com/himadieiev/redpulsar/lettuce/abstracts/AbstractLettucePooled.kt b/redpulsar-lettuce/src/main/kotlin/com/himadieiev/redpulsar/lettuce/abstracts/AbstractLettucePooled.kt index b66a396..7d37bc5 100644 --- a/redpulsar-lettuce/src/main/kotlin/com/himadieiev/redpulsar/lettuce/abstracts/AbstractLettucePooled.kt +++ b/redpulsar-lettuce/src/main/kotlin/com/himadieiev/redpulsar/lettuce/abstracts/AbstractLettucePooled.kt @@ -16,14 +16,16 @@ import org.apache.commons.pool2.impl.GenericObjectPool abstract class AbstractLettucePooled>( protected val connectionPool: GenericObjectPool, ) : AutoCloseable, LettuceUnified { + protected val logger = KotlinLogging.logger { } + init { - connectionPool.borrowObject().use { connection -> - if (connection !is StatefulRedisConnection<*, *> && connection !is StatefulRedisClusterConnection<*, *>) { - throw IllegalArgumentException( - "Connection pool must be of type StatefulRedisConnection or StatefulRedisClusterConnection.", - ) - } + val connection = connectionPool.borrowObject() + if (connection !is StatefulRedisConnection<*, *> && connection !is StatefulRedisClusterConnection<*, *>) { + throw IllegalArgumentException( + "Connection pool must be of type StatefulRedisConnection or StatefulRedisClusterConnection.", + ) } + connectionPool.returnObject(connection) } override fun close() { @@ -56,11 +58,14 @@ abstract class AbstractLettucePooled>( try { connection.sync().discard() } catch (e: Exception) { - val logger = KotlinLogging.logger { } - logger.error(e) { "Could not discard transaction." } + logger.error(e) { "Could not discard a transaction." } } } - connectionPool.returnObject(connection) + try { + connectionPool.returnObject(connection) + } catch (e: IllegalStateException) { + logger.info { "Failed to return connection to the pool. Skipping error: " + e.message } + } } } } diff --git a/redpulsar-lettuce/src/test/kotlin/com/himadieiev/redpulsar/lettuce/LettucePooledTest.kt b/redpulsar-lettuce/src/test/kotlin/com/himadieiev/redpulsar/lettuce/LettucePooledTest.kt index 133bbfb..a03104c 100644 --- a/redpulsar-lettuce/src/test/kotlin/com/himadieiev/redpulsar/lettuce/LettucePooledTest.kt +++ b/redpulsar-lettuce/src/test/kotlin/com/himadieiev/redpulsar/lettuce/LettucePooledTest.kt @@ -37,9 +37,9 @@ class LettucePooledTest { verify(exactly = 1) { connection.sync() - connectionPool.returnObject(eq(connection)) } verify(exactly = 2) { + connectionPool.returnObject(eq(connection)) connectionPool.borrowObject() } } @@ -54,10 +54,10 @@ class LettucePooledTest { verify(exactly = 1) { sync.discard() - connectionPool.returnObject(eq(connection)) } verify(exactly = 2) { connectionPool.borrowObject() + connectionPool.returnObject(eq(connection)) connection.sync() } } diff --git a/redpulsar-lettuce/src/test/kotlin/com/himadieiev/redpulsar/lettuce/LettucePubSubPooledTest.kt b/redpulsar-lettuce/src/test/kotlin/com/himadieiev/redpulsar/lettuce/LettucePubSubPooledTest.kt index d7296fe..eee5e8a 100644 --- a/redpulsar-lettuce/src/test/kotlin/com/himadieiev/redpulsar/lettuce/LettucePubSubPooledTest.kt +++ b/redpulsar-lettuce/src/test/kotlin/com/himadieiev/redpulsar/lettuce/LettucePubSubPooledTest.kt @@ -37,10 +37,10 @@ class LettucePubSubPooledTest { verify(exactly = 1) { connection.sync() - connectionPool.returnObject(eq(connection)) } verify(exactly = 2) { connectionPool.borrowObject() + connectionPool.returnObject(eq(connection)) } } @@ -54,11 +54,11 @@ class LettucePubSubPooledTest { verify(exactly = 1) { sync.discard() - connectionPool.returnObject(eq(connection)) } verify(exactly = 2) { connection.sync() connectionPool.borrowObject() + connectionPool.returnObject(eq(connection)) } }