Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IndexedDB: When to close a connection

Tags:

html

indexeddb

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.

like image 594
NataliaT Avatar asked Jan 21 '16 04:01

NataliaT


2 Answers

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

like image 193
Josh Avatar answered Oct 22 '22 12:10

Josh


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); it it doesn't close its connection 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).

like image 34
Joshua Bell Avatar answered Oct 22 '22 11:10

Joshua Bell