NataliaT
NataliaT

Reputation: 179

IndexedDB: When to close a connection

I would like to know what the correct place to close a connection to the database is.

Let's say that I have the following piece of code:

function addItem(dbName, versionNumber, storeName, element, callback){

   var requestOpenDB = indexedDB.open(dbName, versionNumber); //IDBRequest
   requestOpenDB.onsuccess = function(event){

        //console.log ("requestOpenDB.onsuccess ");
        var db = event.target.result;

        var trans = db.transaction(storeName, "readwrite");
        var store = trans.objectStore(storeName);    
        var requestAdd = store.add(element);

        requestAdd.onsuccess = function(event) {

                    callback("Success");

        };
        requestAdd.onerror = function(event) {

                    callback("Error");       
        };      

    };
    requestOpenDB.onerror = function(event) { 

        console.log ("Error:" +  event.srcElement.error.message);/* handle error */ 
        callback("Error");
    };         
}

addItem basically adds a new element into the database. As per my understanding, when the requestAdd event is triggered that doesn't mean necessarily that the transaction has finished. Therefore I am wondering what the best place to call db.close() is. I was closing the connection inside of requestAdd.onsucess, but if an error happens and requestAdd.onerror is triggered instead, the connection might still be opened. I am thinking about adding trans.oncomplete just under request.onerror and close the db connection here which might be a better option. Any inputs will be more than welcome. Thank you.

Upvotes: 17

Views: 10723

Answers (4)

Joshua Bell
Joshua Bell

Reputation: 8337

You may wish to explicitly close a connection if you anticipate upgrading your database schema. Here's the scenario:

  1. A user opens your site in one tab (tab #1), and leaves it open.
  2. You push an update to your site, which includes code to upgrade the database schema, increasing the version number.
  3. The same user opens a second tab to your site (tab #2) and it attempts to connect to the database.

If the connection is held open by tab #1, the connection/upgrade attempt by tab #2 will be blocked. Tab #1 will see a "versionchange" event (so it could close on demand); if it doesn't close its connection then tab #2 will see a "blocked" event.

If the connection is not held open by tab #1, then tab #2 will be able to connect and upgrade. If tab #1 then tries (based on user action, etc) to open the database (with an explicit version number) it will fail since it will be using an old version number (since it still has the old code).

Upvotes: 16

oomer
oomer

Reputation: 169

You can call db.close() immediately after creating the transaction

var trans = db.transaction(storeName, "readwrite");
db.close();

and it will close the connection only after the transaction has completed. https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/close says The connection is not actually closed until all transactions created using this connection are complete. No new transactions can be created for this connection once this method is called.

Upvotes: 3

Johann
Johann

Reputation: 29867

If you want to run multiple versions of your app and both access the same database, you might think it's possible to keep connections open to both. This is not possible. You must close the database on one before opening it on another. But one problem is that there is currently no way to know when the database actually closes.

Upvotes: 1

Josh
Josh

Reputation: 18690

You generally never need to close a connection. You are not creating memory leaks or anything like that. Leaving the connection open does not result in a material performance hit.

I would suggest not worrying about it.

Also, whether you add trans.oncomplete before or after request.onerror is not important. I understand how it can be confusing, but the order in which you bind the listeners is irrelevant (qualified: from within the same function scope).

Upvotes: 10

Related Questions