已开启
fix:attach 成功后没调 success 回调 #37
linsanjiu创建于 29 天前
fix:attach 成功后没调 success 回调 #37
已开启
共 1 个文件变更+13-22
| @@ -347,29 +347,20 @@ export class SQLitePluginTurboModule extends AnyThreadTurboModule implements TM. | |||
| 347 | 347 | ||
| 348 | const rdbStore = this.rdbMap.get(dbname) as relationalStore.RdbStore; | 348 | const rdbStore = this.rdbMap.get(dbname) as relationalStore.RdbStore; |
| 349 | 349 | ||
| 350 | - if (rdbStore != undefined) { | 350 | + if (rdbStore == undefined) { |
| 351 | - try { | 351 | + error({ "code": -1, "message": `database ${dbname} is not open` }); |
| 352 | - let filePathToAttached = this.context.uiAbilityContext.databaseDir + "/rdb/" + dbNameToAttach; | 352 | + return; |
| 353 | - | 353 | + } |
| 354 | - try { | ||
| 355 | - | ||
| 356 | - await rdbStore.attach(filePathToAttached, dbAlias) | ||
| 357 | - | ||
| 358 | - Logger.debug(CommonConstants.TAG, 'test--SQLitePlugin=attachDatabase>>>>>>complete') | ||
| 359 | - | ||
| 360 | - } catch (e) { | ||
| 361 | - Logger.debug(CommonConstants.TAG, 'Get RdbStore execute fail'); | ||
| 362 | - const err = { "code": e.code, "message": e.message } | ||
| 363 | - | ||
| 364 | - error(err); | ||
| 365 | - } | ||
| 366 | - | ||
| 367 | - } catch (e) { | ||
| 368 | - const err = { "code": e.code, "message": e.message } | ||
| 369 | - | ||
| 370 | - error(err); | ||
| 371 | - } | ||
| 372 | 354 | ||
| 355 | + try { | ||
| 356 | + let filePathToAttached = this.context.uiAbilityContext.databaseDir + "/rdb/" + dbNameToAttach; | ||
| 357 | + await rdbStore.attach(filePathToAttached, dbAlias) | ||
| 358 | + Logger.debug(CommonConstants.TAG, 'test--SQLitePlugin=attachDatabase>>>>>>complete') | ||
| 359 | + success({ "rowsAffected": 0 }, { "rowsAffected": 0 }); | ||
| 360 | + } catch (e) { | ||
| 361 | + Logger.debug(CommonConstants.TAG, 'Get RdbStore execute fail'); | ||
| 362 | + const err = { "code": e.code, "message": e.message } | ||
| 363 | + error(err); | ||
| 373 | } | 364 | } |
| 374 | } | 365 | } |
| 375 | 366 | ||
【AI-Review】【一般】【基础代码问题】【代码逻辑错误】executeSqlBatchDatabase 同样存在 rdbStore 为空时回调缺失导致事务队列挂起
● 问题: 本 PR 在 attachDatabase 第350-352行为 rdbStore == undefined 补充了 error 回调,修复了回调不触发导致 JS 侧挂起的问题。但同文件 executeSqlBatchDatabase(第218-222行)仍保留相同缺陷:rdbStore == undefined 时仅 Logger.debug 后 return,既不调用 success 也不调用 error。触发路径:JS 侧 SQLitePluginTransaction.run(lib/sqlite.core.js:662)通过 plugin.exec("backgroundExecuteSqlBatch", ..., mysuccess, myerror) 调用本方法,事务完成依赖 mysuccess(由 success(callvalue) 触发)将 waiting 计数归零并置 txLocks[dbname].inProgress=false;若原生侧 success 与 error 均未调用,waiting 永不归零,inProgress 永久为 true,该 dbname 后续所有事务被阻塞。
● 影响: 一般。在 deleteDatabase(第190行 relationalStore.deleteRdbStore().then 中执行 rdbMap.delete)与已派发但未完成的 backgroundExecuteSqlBatch 产生时序竞争,或 JS openDBs 与原生 rdbMap 状态不一致时,会触发该数据库事务队列冻屏,症状与本次 attach 修复的挂起问题同源。
● 建议: 参照 attachDatabase 的修复方式,在第218行分支补充 error 回调后返回: if (rdbStore == undefined) { error({ "code": -1, "message":
database ${dbname} is not open}); return; }