Skip to content Skip to sidebar Skip to footer

Indexeddb Doesn't Reset Version When You Delete A Database On Chrome -- Bug Or User Error?

The following code throws an error in Chrome 35 (but not Firefox 29) if I set buggy to true. Since I'm pretty new in indexedDB I wanted to ask whether this ought to work or not. I

Solution 1:

This is a user error.

Deletions in IndexedDB are very fast in both Firefox and Chrome. I haven't done any measurement. But to put things into perspective: In the test suite for SyncedDB I delete and create a database between every single test. There are currently 67 tests and they execute in less than a second. And the database deletions and creations are definitely not the most time consuming part of the tests.

What you've stumbled upon is one of the tricky parts of how IndexedDB works. IndexedDB has a notion of a database connection. The relevant parts with regards to your problem is:

  • You establish a connection by opening a database with the open function. The connection is represented by the IDBDatabase you acquire through open.
  • A database can not be deleted as long as their is open connections to it.
  • When an attempt is made to delete a database a 'versionchange' event is fired against all open IDBDatabase objects connected to the database. The event will have the newVersion property set to null.

The problem is that you call your obliterate function inside your open requests onsuccess event handler. It this point you are trying to delete a database at which you have an open connection. This is problematic and that is why your example code does not work in Chrome (it appears that Firefox times out the connection and completes the deletion, albeit with a huge delay).

The fix is to attach a listener for the versionchange event in your onsuccess event handler. Add this after the line if (--i != 0) { obliterate(); }:

request.result.onversionchange = function(e) {
    if (e.newVersion === null) { // An attempt is made to delete the db
        e.target.close(); // Manually close our connection to the db
    }
};

Inserting this you will see that both Firefox and Chrome will no very rapidly create and delete the tests database.

Post a Comment for "Indexeddb Doesn't Reset Version When You Delete A Database On Chrome -- Bug Or User Error?"