You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

KMP迁移后Room随机抛出SQLException连接超时问题求助

KMP+Compose迁移后Room数据库连接超时随机崩溃

原Android项目(非KMP)运行正常,迁移到KMP+Compose Multiplatform后应用随机崩溃。修改某个Flow值时会触发其他Room Flow发射新值,推测是多Flow同时发射导致数据库连接池耗尽。DAO中所有SELECT语句均返回Flow并被持续收集。

依赖版本

agp = "8.5.0"
androidx-room = "2.7.0-alpha04"
ksp = "2.0.0-1.0.21"
sqlite = "2.5.0-alpha04"
kotlin = "2.0.0"
kotlinSerialization = "2.0.0"
koin = "3.6.0-Beta4"
koinComposeMultiplatform = "1.2.0-Beta4"

Room初始化代码(Koin模块)

actual fun platformModule(): Module = module {
    single<MyDatabase> { getDatabase(get()) }
}

fun getDatabase(
    context: Context
): MyDatabase {
    val dbFile = context.getDatabasePath(dbFileName)
    return Room.databaseBuilder<MyDatabase>(
        context = context,
        name = dbFile.absolutePath
    )
        .fallbackToDestructiveMigration(true)
        .setDriver(BundledSQLiteDriver())
        .setQueryCoroutineContext(Dispatchers.IO)
        .build()
}

崩溃错误日志

FATAL EXCEPTION: main
Process: com.example.example, PID: 19964
android.database.SQLException: Error code: 5, message: Timed out attempting to acquire a connection
    at androidx.sqlite.SQLiteKt.throwSQLiteException(SQLite.kt:42)
    at androidx.room.coroutines.ConnectionPoolImpl.useConnection(ConnectionPoolImpl.kt:138)
    at androidx.room.coroutines.ConnectionPoolImpl$useConnection$1.invokeSuspend(Unknown Source:16)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.internal.ScopeCoroutine.afterResume(Scopes.kt:28)
    at kotlinx.coroutines.AbstractCoroutine.resumeWith(AbstractCoroutine.kt:99)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:102)
    at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:111)
    at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:99)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:811)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:715)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:702)
    Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [androidx.compose.ui.platform.MotionDurationScaleImpl@cd3755f, androidx.compose.runtime.BroadcastFrameClock@a461bac, StandaloneCoroutine{Cancelling}@f506075, AndroidUiDispatcher@21ae80a]

解决建议

  • 调整连接池大小:在Room数据库Builder中添加.setConnectionPoolSize(4)(可根据并发需求调整,默认池容量可能无法满足多Flow并发查询)
  • 合并Flow减少并发请求:将关联的Flow用combine或zip合并,避免同时发起过多独立查询占用连接
  • 规范Flow收集生命周期:在Compose中使用collectAsStateWithLifecycle,确保Flow在组件活跃时才收集,避免闲置Flow长期占用连接
  • 升级Room版本:当前使用的是alpha测试版,尝试升级到稳定版或更高版本的alpha包,修复已知的连接池相关bug
  • 避免主线程阻塞:确保Flow下游的处理逻辑在Dispatchers.IO执行,不要在主线程做耗时操作延长连接占用时间

内容的提问来源于stack exchange,提问作者Álvaro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 07:23:20