Question

I'm looking at using the db.js wrapper for IndexedDB: https://github.com/aaronpowell/db.js

In the docs is says:

A connection is intended to be persisted and you can perform multiple operations while it's kept open. Check out the /tests/public/specs folder for more examples

However the /tests/public/specs folder doesn't exist.

Should I open a new connection each time I have a group of operations to perform, or should I open it once and try to cache it for as long as possible in the client?

cheers, Jordan.

Était-ce utile?

La solution

Connections are meant to be long-lived and reused.

Here's what the spec has to say about connections:

The act of opening a database creates a connection. There may be multiple connections to a given database at any given time. Each connection has a closePending flag which initially is set to false.

When a connection is initially created it is in opened state. The connection can be closed through several means. If the connection is GCed or execution context where the connection is created is destroyed (for example due to the user navigating away from that page), the connection is closed. The connection can also be closed explicitly using the steps for closing a database connection. When the connection is closed the closePending flag is always set to true if it hasn't already been.

The IDBDatabase interface represents a connection to a database.

In my experience, the only problem you'll run into with long-lived, re-used database connections is when you trigger a versionchange event by modifying schema:

As long as a "versionchange" transaction is running, attempts to open more connections to the same database are delayed, and any attempts to use the same connection to start additional transactions will result in an exception being thrown.

Because of this, personally I architected my library not to reuse connections. But under most circumstances re-use should be fine.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top