在indexedDB中打开多个数据库连接是否不好? [英] Is it bad to open several database connections in indexedDB?

查看:235
本文介绍了在indexedDB中打开多个数据库连接是否不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用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...");
          }
        }
      }
    });
  }

saveCredsinitDB函数位于$(document).ready()函数内部,并且db变量在>和saveCreds函数的外部中声明,但 $(document).ready()里面,所以我认为我的范围还可以.

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.

问题是,db变量未定义.我收到错误:Cannot call method "transaction" of undefined.

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?

推荐答案

我的答案必须是:有时.不,您不需要始终打开新连接.您可以在一个连接上使用多个事务.一种简单的方法是将db变量传递给那些函数.但是,您必须了解javascript和indexedDB的异步特性.有时您需要打开新的连接.例如,在其他事件触发时.

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

这篇关于在indexedDB中打开多个数据库连接是否不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆