Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad to open several database connections in indexedDB?

I have been working with IndexedDB for a bit now and I can successfully create a new database, create a store, and add a value during the "on upgrade needed". What I don't understand is, does the database remain "open" or do you have to re-open it inside of every function that needs access to read/write information in the database?

For example, here's my code (which works) to create a new database, and insert one record:

$(document).ready(function() {

var db;

function initDB() {
    console.log("opening db...");

    var req = indexedDB.open(dbName, version);
    req.onsuccess = function(event) {
      db = this.result;
      console.log("opening db is done");
    }

    req.onerror = function(event) {
      console.log("error opening db: ", event.target.errorCode);
    }

    req.onupgradeneeded = function(event) {
      db = event.target.result;
      var store = db.createObjectStore("creds", { autoincrement: true });
      store.add({ username:'none', password:'none'}, 1);
    }
  }

What is causing me confusion is, when I need to access the records in that database, or add more records, or delete records, I thought I could just create a new function and insert some values. This is what I have (which fails):

  function saveCreds() {
    usr = $("#username").val();
    psw = $("#password").val();

    $.getJSON(baseURL + "cred-check/?callback=?", { username:usr, password:psw }, function(data) {
      if (data.creds_good == 'true'); {
        // NEXT LINE FAILS
        var store = db.transaction(['creds'], 'readwrite').objectStore('creds');
        var request = store.get(1);
        request.onsuccess = function (event) {
          var data = request.result;
          data.username = usr;
          data.password = psw;

          var requestUpdate = store.put(data, 1);
          requestUpdate.onerror = function(event) {
            console.log("error putting value...");
          }
        }
      }
    });
  }

This saveCreds and the initDB function are inside the $(document).ready() function and the db variable is declared outside the initDB and saveCreds functions, but inside the $(document).ready() so I think my scope was OK.

The problem is, the db variable is undefined. I get the error: Cannot call method "transaction" of undefined.

So does this mean that for every function that needs access to the data in the database, I need to reopen it, and reassign it to a variable so I can manipulate/read data in the database?

like image 538
Garfonzo Avatar asked Jan 28 '14 23:01

Garfonzo


People also ask

How many connections can a DB handle?

Hi, By default, SQL Server allows a maximum of 32767 concurrent connections which is the maximum number of users that can simultaneously log in to the SQL server instance.

Should I keep a database connection open?

If you write/read every second I would definitely keep the connection open. If you only access the DB every hour I would probably close the connection. In most cases I would tend to keep the connection open as long as your application runs.

Is it safe to use IndexedDB?

The short answer is IndexedDB is vulnerable to malware and physical takeover attacks. It's better than many options because cryptography is done outside the browser execution environment, but it's not totally secure.

Is IndexedDB safer than localStorage?

If you want to store structured data on the client side, IndexedDB is the better choice, especially since localStorage isn't built to store sensitive information. But if you're storing a simple, small amount of key-value pair data, use localStorage.


1 Answers

My answer has to be: sometimes. No, you do not need to always open a new connection. You can use multiple transactions on a connection. A simple way to do this is to pass around the db variable to those functions. However, you must be aware of the asynchronous nature of javascript as well as indexedDB. There are times where you will need to open a new connection. For example, on the trigger of some other event.

So, if you are doing multiple transactions, use a large function something like the following:

function domultiplethings() {
  var db = ... 
  callfunction1(db);
  callfunction2(db);
}

function callfunction1(db) {
  if(!db) {
    // open a new connection then call ourself
  } else {
    // active connection, do read/write stuff
  }
}
like image 176
Josh Avatar answered Sep 28 '22 05:09

Josh