最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - indexedDB doesn't reset version when you delete a database on Chrome -- bug or user error? - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 7

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 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论