Skip to content

Commit

Permalink
Use single threaded coroutine context for database transactions
Browse files Browse the repository at this point in the history
Fixes some concurrency issues (eg. Rollbacks failing to write to the
database during high load)
  • Loading branch information
DrexHD committed Jul 8, 2024
1 parent 019fb8b commit 292fc12
Showing 1 changed file with 3 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 292fc12

Please sign in to comment.