Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you keep an indexeddb transaction alive?

Tags:

indexeddb

Instead of opening several transactions (read a table, write to a table, write to another table, etc) is it possible to do this all from a single transaction as long as you are using an appropriate IDBTransaction?

Mozilla says: "The only way to keep the transaction active is to make a request on it. When the request is finished you'll get a DOM event and, assuming that the request succeeded, you'll have another opportunity to extend the transaction during that callback." which is a little vague. Does that mean if I provide an event handler for the DOM callback that I can use the transaction at any point in that callback without ever having to worry about the transaction being closed?

https://developer.mozilla.org/en/IndexedDB/Using_IndexedDB#Adding_data_to_the_database

like image 874
anonymous Avatar asked Apr 30 '12 14:04

anonymous


People also ask

Can I store object in IndexedDB?

It lets you store just about anything in the user's browser. In addition to the usual search, get, and put actions, IndexedDB also supports transactions. Here is the definition of IndexedDB on MDN: IndexedDB is a low-level API for client-side storage of significant amounts of structured data, including files/blobs.

Which command will you use to run a transaction for IndexedDB database?

IndexedDB events bubble: request → transaction → database .db. onerror = function(event) { let request = event. target; // the request that caused the error console.

What is IndexedDB transaction?

The IDBTransaction interface of the IndexedDB API provides a static, asynchronous transaction on a database using event handler attributes. All reading and writing of data is done within transactions.


1 Answers

Short answer: If you provide an event handler for a "success" or "error" event, you can place a new request inside that event handler and not have to worry about the transaction getting automatically closed.

Long answer: Transaction committing should generally be completely transparent. The only rule is that you can't hold a transaction open while doing non-database "stuff". I.e. you can't start a transaction, then hold it open while doing some XMLHttpRequests, or while waiting for the user to click a button.

As soon as you stop placing requests against a transaction and the last request callback finishes, the transaction is automatically closed.

However you can start a transaction, use that transaction to read some data and then write some results.

So make sure that you have all the data that you need before you start the transaction, then do all reads and writes that you want to do in the request callbacks. Once you are done the transaction will automatically finish.

like image 101
Jonas Sicking Avatar answered Sep 28 '22 18:09

Jonas Sicking