Why does calling indexedDB.deleteDatabase prevent me from making any further transactions?

If I go to my browser console (I use Chrome) right now, on this very page and type

indexedDB.open("MyDB").onsuccess = function(e) { console.log("success"); };

I immediately get the "success" error message in my console. I can do this as many times as I like, and it works great. But if I type

indexedDB.deleteDatabase("MyDB").onsuccess = function(e) { console.log("success"); };

I do not receive a success message. Not only that, but if I then try to call again .open, I also do not receive a success message. How can I cure this strange disease caused .deleteDatabase, and what exactly is happening?

(PS: As soon as I finished typing this answer, I think that the message about the successful completion of the call .deleteDatabasefinally passed two minutes after I made the call, but the question is).

+4
source share
1 answer

Each time you call indexedDB.open, a new database connection is established. When you call deleteDatabase, all these open connections will receive changechange events. They can each listen to this event and close their connections in response. This is your first option. If they do not, the query returned by indexed DB.deleteDatabase ("independently") will receive a blocked event. This is what happens in your case. The second option is to listen to the blocked event and close the connections there:

var request = indexedDB.deleteDatabase("MyDB");
request.onsuccess = function(e) { console.log("success"); };
request.onblocked = function(e) {
  console.log("blocked: " + e);
  // Close connections here
};
request.onerror = function(e) { console.log("error: " + e); };
+5
source

Source: https://habr.com/ru/post/1626898/


All Articles