根据我的经验,在等待 AJAX 响应时事务往往会关闭,因此您必须在 AJAX 响应的成功处理程序中打开事务。
让多个重叠事务并行“运行”肯定没有错,但实际上 JavaScript 仍然是单线程的——你会发现即使是 AJAX 成功处理程序也是按顺序运行的,并且排序是相当可预测的(尽管不能保证),因此,如果您在每个交易中打开一个事务,然后使用一系列 put 处理数据,您可能实际看到的是:
openAjax1()
openAjax2()
ajax1Complete()
// in here, open transaction1 and call objectStore1.put() many times
ajax2Complete()
// in here, open transaction2 and call objectStore2.put() many times
put() // from ajax1
put() // from ajax1
put() // from ajax1
... // from ajax1
// transaction1 completes
put() // from ajax2
put() // from ajax2
put() // from ajax2
... // from ajax2
// transaction2 completes
理论上,真正使事情并行发生的唯一方法是使用工作线程,但即便如此,大多数实现仍会将工作集中到单个线程。实际上,后端在优化“批量”写入方面非常出色,因此您甚至不会从使用工作人员中获得太多性能改进。