From 292fc12ab394cbfd2c1ebc692cc4c60030683556 Mon Sep 17 00:00:00 2001 From: Drex Date: Mon, 8 Jul 2024 17:15:51 +0200 Subject: [PATCH] Use single threaded coroutine context for database transactions Fixes some concurrency issues (eg. Rollbacks failing to write to the database during high load) --- .../quiltservertools/ledger/database/DatabaseManager.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/github/quiltservertools/ledger/database/DatabaseManager.kt b/src/main/kotlin/com/github/quiltservertools/ledger/database/DatabaseManager.kt index c374e4b4..22dc477b 100644 --- a/src/main/kotlin/com/github/quiltservertools/ledger/database/DatabaseManager.kt +++ b/src/main/kotlin/com/github/quiltservertools/ledger/database/DatabaseManager.kt @@ -17,6 +17,7 @@ import com.github.quiltservertools.ledger.utility.PlayerResult import com.google.common.cache.Cache import com.mojang.authlib.GameProfile import kotlinx.coroutines.delay +import kotlinx.coroutines.newSingleThreadContext import net.minecraft.util.Identifier import net.minecraft.util.math.BlockPos import org.jetbrains.exposed.dao.Entity @@ -74,6 +75,7 @@ object DatabaseManager { get() = database.dialect.name private val cache = DatabaseCacheService + private val databaseCoroutine = newSingleThreadContext("Ledger") fun setup(dataSource: DataSource?) { val source = dataSource ?: getDefaultDatasource() @@ -369,7 +371,7 @@ object DatabaseManager { delay(timeMillis = 1000) } - return newSuspendedTransaction(db = database) { + return newSuspendedTransaction(context = databaseCoroutine, db = database) { repetitionAttempts = MAX_QUERY_RETRIES minRepetitionDelay = MIN_RETRY_DELAY maxRepetitionDelay = MAX_RETRY_DELAY