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

夙愿已清 提交于 2019-12-22 06:00:35

问题


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


回答1:


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); };


来源:https://stackoverflow.com/questions/35137234/why-does-calling-indexeddb-deletedatabase-prevent-me-from-making-any-further-tra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!