mirror of
https://github.com/InsanusMokrassar/PlaguBot.git
synced 2025-09-15 05:19:25 +00:00
add option waitForConnection in database config
This commit is contained in:
@@ -6,6 +6,7 @@ import org.jetbrains.exposed.sql.Database
|
||||
import org.jetbrains.exposed.sql.transactions.transactionManager
|
||||
import org.koin.core.scope.Scope
|
||||
import org.sqlite.JDBC
|
||||
import java.lang.Exception
|
||||
import java.sql.Connection
|
||||
|
||||
inline val Scope.database: Database?
|
||||
@@ -16,15 +17,30 @@ data class DatabaseConfig(
|
||||
val url: String = "jdbc:sqlite:file:test?mode=memory&cache=shared",
|
||||
val driver: String = JDBC::class.qualifiedName!!,
|
||||
val username: String = "",
|
||||
val password: String = ""
|
||||
val password: String = "",
|
||||
val waitForConnection: Boolean = true
|
||||
) {
|
||||
@Transient
|
||||
val database: Database = Database.connect(
|
||||
url,
|
||||
driver,
|
||||
username,
|
||||
password
|
||||
).also {
|
||||
it.transactionManager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE // Or Connection.TRANSACTION_READ_UNCOMMITTED
|
||||
val database: Database by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
|
||||
while (true) {
|
||||
return@lazy try {
|
||||
Database.connect(
|
||||
url,
|
||||
driver,
|
||||
username,
|
||||
password
|
||||
).also {
|
||||
it.transactionManager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE // Or Connection.TRANSACTION_READ_UNCOMMITTED
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
if (waitForConnection) {
|
||||
Thread.sleep(1000L)
|
||||
continue
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
error("Unable to get database by some reason")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user