How do I check if an indexedDB instance is open?
Asked Answered
V

3

8

Suppose I have an instance of an indexedDB object. Is there a simple way of detecting if the object is currently in the 'open' state?

I've tried database.closePending and looking at other properties but do not see a simple property that tells me the state of the database.

  • I am looking to do this synchronously.
  • Doing something like attempting open a transaction on a database and checking if an exception occurs is not a reasonable solution for me.
  • I don't want to maintain an extra variable associated with the database instance.

Perhaps I am missing some simple function in the api? Is there some observable feature of the instance variable that I can quickly and easily query to determine state?

Stated a different way, can you improve upon the following implementation?

function isOpen(db) {
  if(db && Object.prototype.toString.call(db) === '[object IDBDatabase]') {
    var names = db.objectStoreNames();
    if(names && names.length) {
      try {
        var transaction = db.transaction(names[0]);
        transaction.abort();
        return true;
      } catch(error) {
      }
    }
  }
}

Or this method?

var opened = false;
var db;
var request = indexedDB.open(...);
request.onsuccess = function() {
  db = request.result;
  opened = true;
};

function isOpen(db) {
  return opened;
}

db.close();
opened = false;

Or this method?

var db;
var request = indexedDB.open(...);
request.onsuccess = function() {
  db = request.result;
  db.onclose = function() {
    db._secret_did_close = true;
  };
};

function isOpen(db) {
  return db instanceof IDBDatabase && !db.hasOwnProperty('_secret_did_close');
}
Varion answered 29/10, 2017 at 3:32 Comment(1)
sorry for the time but i see the code and i prefer the middle because you don't need to check all, just using events and callbacks can got the errors. you can show it on the browser console and the way is to make some errors by test what is going on. The good way i guess is to implements all methods during the transaction where are major of errors stored. The connection can still running and you can catch this event by using blocked event ... that the reason why just implement and add a console.log there to be sure.Ananthous
E
3

There's nothing else in the API that tells you if a connection is closed. Your enumeration of possibilities is what is available.

Also note that there is no closePending property in the API. The specification text uses a close pending flag to represent internal state, but this is not exposed to script.

Doing something like attempting open a transaction on a database and checking if an exception occurs is not a reasonable solution for me.

Why? This is the most reliable approach. Maintaining extra state would not account for unexpected closure (e.g. the user has deleted browsing data, forcing the connection to close) although that's what the onclose handler would account for - you'd need to combine your 2nd and 3rd approaches. (close is not fired if close() is called by script)

Escaut answered 30/10, 2017 at 3:26 Comment(1)
Thanks Josh. Would love to see a feature like this though! My use case is a defensive development-phase assertion prior to starting a new transaction.Varion
E
2

Better late than never, but here you go:

function isOpen(db) {
    let open = true;
    try {
        db.transaction('');
    } catch(err) {
        if (err.name === "InvalidStateError") {
            open = false;
        }
    }
    return open;
}

Example:

indexedDB.open('test', 1).onsuccess = ({target: { result: db }}) => {
    console.log(isOpen(db)); // true
    db.close();
    console.log(isOpen(db)); // false
}
Evince answered 10/5 at 20:10 Comment(1)
The only right answer!Overbid
A
0

You should create a request by using indexedDB.open and if the connection is open you will jump onsuccess method.

request = indexedDB.open('html5',1);

request.onsuccess = function() {
    console.log('Database connected');
};

Example :

https://codepen.io/headmax/pen/BmaOMR?editors=1111

About how to close or how to known if the indexedDB is still open : I guess you need to implement all events on every transaction : for example to take the control you can take the events : transaction.onerror, transaction.onabort ... If you need some example explanation i guess you have to create a new post ;).

https://developer.mozilla.org/en-US/docs/Web/API/IDBTransaction

Ananthous answered 29/10, 2017 at 3:40 Comment(4)
Sorry if I wasn't clear, but this is not related to my question. I already have an open instance of the database using this method. Now I want to be able to inspect the instance and see if it is still open or not at a much later point in time.Varion
@Varion you can do it if you known the task inside a method can be closed by the hand using event.target.close(); and if you want to close the database just do a db.close(); if i understand the question?Ananthous
Again, sorry, but this is entirely unrelated to what I am asking. I've edited the question to try and be clearer.Varion
No problem Josh i understand.Ananthous

© 2022 - 2024 — McMap. All rights reserved.