问题
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. If I delete the database and open it again with version=1
, shouldn't my onupgradeneeded
callback get called?
<html>
<head>
<script type="text/javascript">
var i = 1000;
var buggy = true;
function open() {
var version = buggy ? 1 : 1001 - i;
var request = indexedDB.open("test", version);
var upgraded = false;
request.onupgradeneeded = function() {
upgraded = true;
console.log("upgraded ok");
}
request.onsuccess = function() {
if (!upgraded) {
throw "Not upgraded";
}
console.log("open ok");
if (--i != 0) { obliterate(); }
}
request.onerror = function() {
throw "Error in open";
}
}
function obliterate() {
var request = indexedDB.deleteDatabase("test");
request.onsuccess = function() {
console.log("delete ok");
open();
}
request.onerror = function(event) {
throw "Error in obliterate.";
}
}
obliterate();
</script>
</head>
<body>
</body>
</html>
In Chrome, with buggy=true
, I get:
delete ok test.html:29
upgraded ok test.html:12
open ok test.html:18
delete ok test.html:29
Uncaught Not upgraded
In Firefox it works fine.
As a side note, in both Chrome and Firefox, this runs incredibly slowly -- like 5-10 seconds to create and delete the database a single time. Is this normal/expected? Am I doing something wrong?
回答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 throughopen
. - 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 tonull
.
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.
来源:https://stackoverflow.com/questions/24111035/indexeddb-doesnt-reset-version-when-you-delete-a-database-on-chrome-bug-or-u