admin管理员组文章数量:1025269
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?
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?
Share Improve this question asked Jun 8, 2014 at 21:29 Josh HabermanJosh Haberman 4,2301 gold badge27 silver badges47 bronze badges 3- 1 Hi Josh, it works on my machine as expected, sometimes the open and the delete takes 5-10 seconds but that is ok. I would remend you adding database.close() in the on success function of opening db. – Deni Spasovski Commented Jun 9, 2014 at 2:05
- Sadly deleting a database and index does not return a request on which you can register callbacks :( – buley Commented Jun 9, 2014 at 15:35
- I seem to have the same problem. – miracle2k Commented Dec 30, 2014 at 5:47
1 Answer
Reset to default 7This 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 pletes 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.
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?
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?
Share Improve this question asked Jun 8, 2014 at 21:29 Josh HabermanJosh Haberman 4,2301 gold badge27 silver badges47 bronze badges 3- 1 Hi Josh, it works on my machine as expected, sometimes the open and the delete takes 5-10 seconds but that is ok. I would remend you adding database.close() in the on success function of opening db. – Deni Spasovski Commented Jun 9, 2014 at 2:05
- Sadly deleting a database and index does not return a request on which you can register callbacks :( – buley Commented Jun 9, 2014 at 15:35
- I seem to have the same problem. – miracle2k Commented Dec 30, 2014 at 5:47
1 Answer
Reset to default 7This 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 pletes 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.
本文标签:
版权声明:本文标题:javascript - indexedDB doesn't reset version when you delete a database on Chrome -- bug or user error? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745619338a2159482.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论