Compare commits

...

2 Commits

Author SHA1 Message Date
darwincereska
5b70c6de89 feat: added database config 2025-12-17 09:40:34 -05:00
darwincereska
d957f2034d feat: added exceptions 2025-12-17 09:29:42 -05:00
5 changed files with 112 additions and 0 deletions

View File

@@ -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)
)
}
}

View File

@@ -0,0 +1,8 @@
package org.ccoin.exceptions
/** Base exception class for all CCoin-related exceptions */
open class CCoinException(
message: String,
cause: Throwable? = null,
val errorCode: String? = null
) : Exception(message, cause)

View File

@@ -0,0 +1,10 @@
package org.ccoin.exceptions
class InsufficientFundsException(
val address: String,
val requestedAmount: Double,
val availableBalance: Double
) : CCoinException(
message = "Insufficient funds in wallet $address. Requested: $requestedAmount, Available: $availableBalance",
errorCode = "INSUFFICIENT_FUNDS"
)

View File

@@ -0,0 +1,9 @@
package org.ccoin.exceptions
class InvalidTransactionException(
message: String,
val transactionHash: String? = null
) : CCoinException(
message = message,
errorCode = "INVALID_TRANSACTION"
)

View File

@@ -0,0 +1,8 @@
package org.ccoin.exceptions
class WalletNotFoundException(
val address: String
) : CCoinException(
message = "Wallet with address '$address' not found",
errorCode = "WALLET_NOT_FOUND"
)