lisak
lisak

Reputation: 21961

Is it safe to open new IndexedDb transaction from oncomplete callback of the previous one?

The reason why I need it is for composing multiple (add,put) transactions in a flatMap style. Now I'm wondering if I can init a new transaction from an oncomplete callback of the previous one. It seems to be working, but I can never be sure what it does in production. For instance if you do similar thing from request onsuccess callback, it has a special meaning of transaction continuation.

The question might also be, is transaction#oncomplete method invocation a signal that transaction completed OR is it when transaction#oncomplete returns?

I'd have to do some load tests to figure that out myself, In simple test cases it works and it looks the transaction end is transaction#oncomplete method invocation, but if I started to use it heavily, there seemed to be some write lock starvations that made me think about this.

Upvotes: 1

Views: 196

Answers (1)

Joshua Bell
Joshua Bell

Reputation: 8337

Yes. Starting a second transaction in response to the "complete" event of a previous transaction is both correct and the easiest way to reason about the sequencing.

Per spec (section "steps for committing a transaction"):

[...] Only after the transaction has been successfully written is the "complete" event fired.

As you note, you may see "success" events from individual requests, but the overall transaction may abort (index consistency, quota, etc) so waiting for "complete" is the right thing to do if you need to reason about the success of the first transaction in subsequent ones.

Upvotes: 4

Related Questions