Compare commits
2 Commits
38c42b5217
...
5b70c6de89
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5b70c6de89 | ||
|
|
d957f2034d |
@@ -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)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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"
|
||||||
|
)
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package org.ccoin.exceptions
|
||||||
|
|
||||||
|
class InvalidTransactionException(
|
||||||
|
message: String,
|
||||||
|
val transactionHash: String? = null
|
||||||
|
) : CCoinException(
|
||||||
|
message = message,
|
||||||
|
errorCode = "INVALID_TRANSACTION"
|
||||||
|
)
|
||||||
|
|||||||
@@ -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"
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user