jakearchibald / idb

IndexedDB, but with promises
https://www.npmjs.com/package/idb
ISC License
6.22k stars 348 forks source link

InvalidStateError: Failed to execute 'transaction' on 'IDBDatabase': The database connection is closing #229

Closed johannesjo closed 3 years ago

johannesjo commented 3 years ago

First of all: Thank you for this great module. It's making my life a lot easier.

Many of the users of my electron app encounter the error above. I personally was never able to reproduce it and I am not sure if this is caused by this module or just a general issue with chromium, but I am posting this here anyway in the hopes to find a new angle to tackle this problem, that has been bugging me for so long :)

https://github.com/johannesjo/super-productivity/issues/398 https://github.com/dfahlander/Dexie.js/issues/613

jakearchibald commented 3 years ago

I'm 99% sure it isn't this library. The library doesn't do auto-closing, and the error seems to happen in other libraries too. crbug.com seems like the right place for this.

piotr-cz commented 2 years ago

This may happen on Mobile Safari

Issue in similar library: https://github.com/jensarps/IDBWrapper/issues/80 Possible workaround: https://github.com/firebase/firebase-js-sdk/pull/4059

dfahlander commented 2 years ago

Dexie has a workaround for this in >=3.2.1. Idb however, is closer to the native IndexedDB API and does not execute transactions in a callback as dexie does, which makes it harder to workaround "in the background" without requiring code change by its callers. But one solution would be to offer a new helper function to be used as an alternative to db.transaction() that would return a Promise<IDBPTransaction> - it could call nativeDB.transaction() in a try...catch and in case this error occur, do the silent database repoening (reopen the same version as was already opened). Another approach would be to let Idb's version of IDBTransaction represent just a lazy placeholder and let the actual call to nativeDB.transaction() take part in the first call to an actual request - surrounding the call to nativeDB.transaction() with a try..catch and do the workaround if needed. The latter would not require change of application code, but could introduce subtle bugs. For example, the "fake" transaction would have to support executing complete events on noop transactions (if someone calls db.transaction().oncomplete = ()=>console.log('complete') without using the transaction).

But I assume the best thing would be if Chrome and Safari fixed the bug :)