-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close active connection before creating new one
- Loading branch information
1 parent
39ab118
commit d2f02c5
Showing
4 changed files
with
65 additions
and
28 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/io/tcds/orm/connection/ReconnectableConnection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!! | ||
} | ||
} |
21 changes: 0 additions & 21 deletions
21
src/main/kotlin/io/tcds/orm/connection/ReconnectableResilientConnection.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters