Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
798 views
in Technique[技术] by (71.8m points)

jquery - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...