feat: added database config
This commit is contained in:
@@ -0,0 +1,77 @@
|
|||||||
|
package org.ccoin.config
|
||||||
|
|
||||||
|
import com.zaxxer.hikari.HikariConfig
|
||||||
|
import com.zaxxer.hikari.HikariDataSource
|
||||||
|
import org.ccoin.database.Tables
|
||||||
|
import org.jetbrains.exposed.sql.Database
|
||||||
|
import org.jetbrains.exposed.sql.SchemaUtils
|
||||||
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
import org.slf4j.LoggerFactory
|
||||||
|
|
||||||
|
object DatabaseConfig {
|
||||||
|
private val logger = LoggerFactory.getLogger(DatabaseConfig::class.java)
|
||||||
|
|
||||||
|
fun init() {
|
||||||
|
logger.info("Initializing database connection...")
|
||||||
|
|
||||||
|
val config = HikariConfig().apply {
|
||||||
|
driverClassName = "org.postgresql.Driver"
|
||||||
|
jdbcUrl = System.getenv("DATABASE_URL") ?: "jbdc:postgresql://localhost:5432/ccoin"
|
||||||
|
username = System.getenv("DATABASE_USER") ?: "ccoin"
|
||||||
|
password = System.getenv("DATABASE_PASSWORD") ?: "ccoin"
|
||||||
|
|
||||||
|
// Connection pool settings
|
||||||
|
maximumPoolSize = (System.getenv("DATABASE_POOL_SIZE")?.toIntOrNull() ?: 20)
|
||||||
|
minimumIdle = 5
|
||||||
|
connectionTimeout = 30000
|
||||||
|
idleTimeout = 600000
|
||||||
|
maxLifetime = 1800000
|
||||||
|
|
||||||
|
// Performance settings
|
||||||
|
isAutoCommit = false
|
||||||
|
transactionIsolation = "TRANSACTION_REPEATABLE_READ"
|
||||||
|
|
||||||
|
// Connection validation
|
||||||
|
connectionTestQuery = "SELECT 1"
|
||||||
|
validationTimeout = 5000
|
||||||
|
|
||||||
|
// Pool name for monitoring
|
||||||
|
poolName = "CCoinPool"
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
val dataSource = HikariDataSource(config)
|
||||||
|
Database.connect(dataSource)
|
||||||
|
|
||||||
|
logger.info("Database connection established successfully")
|
||||||
|
|
||||||
|
// Create tables if they don't exist
|
||||||
|
createTables()
|
||||||
|
} catch(e: Exception) {
|
||||||
|
logger.error("Failed to initialize database connection", e)
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createTables() {
|
||||||
|
logger.info("Creating database tables if they don't exist...")
|
||||||
|
|
||||||
|
transaction {
|
||||||
|
SchemaUtils.create(
|
||||||
|
Tables.Wallets,
|
||||||
|
Tables.Transactions,
|
||||||
|
Tables.Blocks
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("Database tables created/verified successfully")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getConnectionInfo(): Map<String, Any> {
|
||||||
|
return mapOf(
|
||||||
|
"url" to (System.getenv("DATABASE_URL") ?: "jdbc:postgresql://localhost:5432/ccoin"),
|
||||||
|
"user" to (System.getenv("DATABASE_USER") ?: "ccoin_user"),
|
||||||
|
"poolSize" to (System.getenv("DATABASE_POOL_SIZE")?.toIntOrNull() ?: 20)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user