Skip to content

Commit

Permalink
Close active connection before creating new one
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagocordeiro committed Sep 3, 2024
1 parent 39ab118 commit d2f02c5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 28 deletions.
27 changes: 27 additions & 0 deletions src/main/kotlin/io/tcds/orm/connection/ReconnectableConnection.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.tcds.orm.connection

import org.slf4j.Logger
import java.sql.Connection

class ReconnectableConnection(
private val logger: Logger? = null,
private val factory: () -> Connection,
) : ResilientConnection {
private var connection: Connection? = null

override fun instance(): Connection {
val status = listOf(
connection == null,
connection?.isClosed,
connection?.isValid(5) == false,
)
val requiresCreation = status.any { it == true }

if (requiresCreation) {
connection?.close().apply { logger?.info("Closed active connection") }
connection = factory().apply { logger?.info("Created new connection") }
}

return connection!!
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.tcds.orm.connection

import org.slf4j.Logger
import java.sql.Connection
import java.sql.Connection as JdbcConnection

interface ResilientConnection {
fun instance(): JdbcConnection

companion object {
fun reconnectable(factory: () -> Connection) = ReconnectableResilientConnection(factory)
fun reconnectable(logger: Logger? = null, factory: () -> Connection) = ReconnectableConnection(logger, factory)
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package fixtures.connection

import io.mockk.every
import io.mockk.mockk
import io.mockk.*
import io.tcds.orm.connection.ResilientConnection
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.slf4j.Logger
import java.sql.Connection as JdbcConnection

class ReconnectableResilientConnectionTest {
class ReconnectableConnectionTest {
private val logger = mockk<Logger>()
private val connection = mockk<JdbcConnection>()
private var calls = 0

init {
every { logger.info(any()) } just runs
}

@Test
fun `when instance is null then create from the factory`() {
val connection = ResilientConnection.reconnectable {
val connection = ResilientConnection.reconnectable(logger) {
calls++
connection
}
Expand All @@ -22,13 +27,15 @@ class ReconnectableResilientConnectionTest {

Assertions.assertEquals(1, calls)
Assertions.assertInstanceOf(JdbcConnection::class.java, instance)
verify(exactly = 1) { logger.info("Created new connection") }
}

@Test
fun `when instance is closed then create from the factory`() {
every { connection.isClosed } returns true
every { connection.isValid(any()) } returns true
val connection = ResilientConnection.reconnectable {
every { connection.close() } just runs
val connection = ResilientConnection.reconnectable(logger) {
calls++
connection
}
Expand All @@ -39,13 +46,25 @@ class ReconnectableResilientConnectionTest {

Assertions.assertEquals(3, calls)
Assertions.assertInstanceOf(JdbcConnection::class.java, instance)
verifySequence {
// first
logger.info("Closed active connection")
logger.info("Created new connection")
// second
logger.info("Closed active connection")
logger.info("Created new connection")
// third
logger.info("Closed active connection")
logger.info("Created new connection")
}
}

@Test
fun `when instance is not valid then create from the factory`() {
every { connection.isClosed } returns false
every { connection.isValid(any()) } returns false
val connection = ResilientConnection.reconnectable {
every { connection.close() } just runs
val connection = ResilientConnection.reconnectable(logger) {
calls++
connection
}
Expand All @@ -56,5 +75,16 @@ class ReconnectableResilientConnectionTest {

Assertions.assertEquals(3, calls)
Assertions.assertInstanceOf(JdbcConnection::class.java, instance)
verifySequence {
// first
logger.info("Closed active connection")
logger.info("Created new connection")
// second
logger.info("Closed active connection")
logger.info("Created new connection")
// third
logger.info("Closed active connection")
logger.info("Created new connection")
}
}
}

0 comments on commit d2f02c5

Please sign in to comment.