Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

If I go into my browser's console (I'm using Chrome) right now, on this very page, and type

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

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

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

I get no "success" message back. Not only that, but if I then try and call .open again, I also get no "success" message back. How can I cure this strange illness caused by .deleteDatabase, and what exactly is happening?

(PS: Just as I finished typing this answer, I think the "success" message from the call to .deleteDatabase finally did come through, about two minutes after I made the call - but the question stands).

like image 976
Jack M Avatar asked Feb 01 '16 17:02

Jack M


People also ask

What is IndexedDB in chrome?

IndexedDB is a way for you to persistently store data inside a user's browser. Because it lets you create web applications with rich query abilities regardless of network availability, your applications can work both online and offline.


1 Answers

Every time you call indexedDB.open a new connection to the database is established. When you call deleteDatabase all those open connections will get versionchange events. They can each listen for that event and close their connections in response. That is your first option. If they do not, the request returned by indexedDB.deleteDatabase("whatever") will receive a blocked event. This is what is happening in your case. Your 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); };
like image 159
dgrogan Avatar answered Oct 08 '22 08:10

dgrogan