【问题标题】:TransactionInactiveError when starting a Dexie transaction启动 Dexie 事务时的 TransactionInactiveError
【发布时间】:2021-01-04 05:57:39
【问题描述】:

我有一个将 jsonfile 同步到我的 Dexie IndexedDB 数据库的服务功能(使用 typescript 4.0.2+webpack 4.44.1+dexie 3.0.2)。但是一旦我开始我的数据库事务,我就会得到一个错误:

Unhandled rejection: TransactionInactiveError: Transaction has already completed or failed
./node_modules/dexie/dist/dexie.mjs/Transaction.prototype._promise@http://localhost:9000/dist/bundle.js:4876:30
./node_modules/dexie/dist/dexie.mjs/Dexie.prototype._transaction@http://localhost:9000/dist/bundle.js:6488:35
./node_modules/dexie/dist/dexie.mjs/Dexie.prototype.transaction@http://localhost:9000/dist/bundle.js:6438:34
updateEDSMSector/<@http://localhost:9000/dist/bundle.js:169098:26
...
...

错误出现在console.log('extracted json', json); 语句之后(但下一个console.log 永远不会发生)。我完全不知道我在这里做错了什么。我希望任何人都可以指点如何调试问题。

  static async updateEDSMSector(sector_number: number[], db: EDSMLayerDB) {
    const sector = await db.sectors.where('sector_number').equals(sector_number).first();
    console.log("Checking for EDSM updates for sector", sector);
    const updatedDate = sector.updatedDate;
    const [sx, sy, sz] = sector.sector_number;
    const jsonFilename = `edsm/${sx}/${sy}/${sx}_${sy}_${sz}.json.gz`;
    let res = await fetch(jsonFilename, {
      cache: 'no-cache',
      method: 'HEAD',
    });
    const lastModified = new Date(res.headers.get('Last-Modified'));
    console.log('updatedDate', updatedDate, 'lastModified', lastModified);
    if (!updatedDate || new Date(updatedDate) < lastModified) {
      res = await fetch(jsonFilename, {
        cache: 'no-cache',
        method: 'GET',
      });
      console.log('importing data');
      const json = JSON.parse(ungzip(new Uint8Array(await res.arrayBuffer()),
        {to: 'string'}));
      console.log('extracted json', json);
      await db.transaction('rw', db.sectors, db.systems, async () => {
        console.log('started transaction');
        await db.systems.where('sector').equals(sector.sector_number).delete();
        console.log('deleted old data');
        for (const system of json) {
          const {coords, ...rest} = system;
          const val = {...rest, ...coords};
          await db.systems.add(val);
        };
        console.log('Added systems');
        sector.updatedDate = new Date();
        await db.sectors.put(sector);
      });

      console.log('Finished adding system data');
    } else {
      console.log('Systems for sector', sector, 'already up-to-date.');
    }

  }

【问题讨论】:

    标签: typescript dexie


    【解决方案1】:

    看你的代码sn-p,好像是node程序? Dexie 主要用于浏览器,但可以在带有 IndexedDBShim 的节点中执行。是这样吗?然后你必须避免使用 async / await 关键字,因为 IndexedDBShim 会过早地触发它的事务。

    您可以通过将代码转换为 ES5 来解决这个问题,或者您可以避免使用 async/await。

    【讨论】:

    • 不,正如我所说的,我使用 webpack 运行它,所以在浏览器中。
    【解决方案2】:

    问题是我正在异步运行代码

    db.mytable.where(...).each((row) => { updateEDSMSector(row.sector_number, db) };
    

    这会触发一个查询,但对于每个查询结果,都会在 updateEDSMSector 内启动一个新事务。这些异步运行导致事务开始,而旧事务尚未正确关闭。我通过首先获取 db.mytable.where().toArray() 然后循环遍历这些来修复它。这仍然允许我的 updateEDSMSector 调用异步运行,但不会与关闭的事务冲突。

    【讨论】:

    • 很高兴您发现了问题。感谢分享!
    猜你喜欢
    • 2016-01-08
    • 2017-07-09
    • 2019-06-17
    • 2020-06-29
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多