Indexddb twice open is suspended

problem description

I need to dynamically add the "table" (store), of indexddb to the project instead of initializing all the tables to be used at once. However, there is a problem when triggering the version update event, and the crux of the problem is as follows:

the code is as follows

const one = window.indexedDB.open("test");
one.onupgradeneeded = e => {
  console.log("db one upgrade success", e.target.readyState);
  updateDB();
}

function updateDB() {
  const tow = window.indexedDB.open("test", 2);
  tow.onupgradeneeded = e => {
    console.log("db tow upgrade success", e.target.readyState);
  }
  tow.onblocked = e => {
    console.log("blocked:", e)
  }

  setTimeout(() => {
    console.log(tow.readyState);
  }, 3000);
}

running results

questions

after creating the database, update the database version and expect some action in the upgradeneeded event, but the second operation is suspended and cannot trigger the upgradeneeded event. In fact, success and error are not triggered either. Could you tell me how to solve this situation?

Nov.29,2021

forgot to end the problem, and it was solved at that time, using the close () method to end the database connection, which was written in the comment on the question.
has been systematically studying indexedDB recently, and a lot of IndexedDB documents have been translated on MDN.
for details and principles of the above issues, see the MDN documentation.

to put it simply, when the same database under the same source is opened, the version upgrade is blocked, and the upgrade event will not continue until the close method is called to close the previous database connection.


A transaction has been opened after the visual indexedDB is opened. You cannot call open again in the upgradeneeded. This is a conflict between the two transactions.

Menu