IDBTransacation接口由IndexedDB API提供,异步transaction使用数据库中的事件对象属性。所有的读取和写入数据均在transactions中完成。由IDBDatabase
发起transaction,通过IDBTransaction
来设置transaction的模式(例如是否只读或读写),以及通过IDBObjectStore
来获得一个request。同时你也可以使用它来中止transactions。
方法
从EventTarget
继承
IDBTransaction.abort
- 放弃本次连接的transaction的所有修改,如果当前的transaction处于回滚完毕或完成状态,则会抛出一个错误事件。
IDBTransaction.objectStore
- Returns an
IDBObjectStore
object representing an object store that is part of the scope of this transaction.
属性
IDBTransaction.db
只读- The database connection with which this transaction is associated.
IDBTransaction.mode
只读- The mode for isolating access to data in the object stores that are in the scope of the transaction. For possible values, see the Constants section below. The default value is
readonly
. IDBTransaction.error
只读- The error returned in the event of an unsuccessful transaction. Null if the transaction is not finished, is finished and successfully committed, or was aborted with
IDBTransaction.abort
function. Returns the same DOMError as the request object which caused the transaction to be aborted due to a failed request, or a DOMError for the transaction failure not due to a failed request (such asQuotaExceededError
orUnknownError
).
Event handlers
IDBTransaction.onabort
只读- The event handler for the
abort
event, fired when the transaction is aborted. IDBTransaction.oncomplete
只读- The event handler for the
complete
event, thrown when the transaction completes successfully. IDBTransaction.onerror
只读- The event handler for the
error
event, thrown when the transaction fails to complete.
模式常量
已废弃 Gecko 25 (Firefox 25 / Thunderbird 25 / SeaMonkey 2.22)
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.
These constants are no longer available. You should use the direct string constants instead. (bug 888598)
Transactions 可使用以下三种模式中的一种:
常量 | 值 | 描述 |
---|---|---|
READ_ONLY |
"readonly" (0 in Chrome) |
允许读取数据,不改变。 |
READ_WRITE |
"readwrite" (1 in Chrome) |
允许读取和写入现有数据存储,数据被改变。
|
VERSION_CHANGE |
"versionchange" (2 in Chrome) |
允许执行任何操作,包括删除和创建对象存储和索引。 此模式是用于开始使用 IDBDatabase 的 这种模式的事务无法与其它事务并发运行。 |
即使目前这些常量已经被废弃,但如果你需要使用它,则需要提供向下兼容方案(in Chrome the change was made in version 21)。你应当防止出现对象不存在的情况:
var myIDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || { READ_WRITE: "readwrite" };
Example
In the following code snippet, we open a read/write transaction on our database and add some data to an object store. Note also the functions attached to transaction event handlers to report on the outcome of the transaction opening in the event of success or failure. For a full working example, see our To-do Notifications app (view example live.)
// Let us open our database var DBOpenRequest = window.indexedDB.open("toDoList", 4); DBOpenRequest.onsuccess = function(event) { note.innerHTML += '<li>Database initialised.</li>'; // store the result of opening the database in the db variable. This is used a lot below db = DBOpenRequest.result; // Run the addData() function to add the data to the database addData(); }; function addData() { // Create a new object ready for being inserted into the IDB var newItem = [ { taskTitle: "Walk dog", hours: 19, minutes: 30, day: 24, month: "December", year: 2013, notified: "no" } ]; // open a read/write db transaction, ready for adding the data var transaction = db.transaction(["toDoList"], "readwrite"); // report on the success of opening the transaction transaction.oncomplete = function(event) { note.innerHTML += '<li>Transaction completed: database modification finished.</li>'; }; transaction.onerror = function(event) { note.innerHTML += '<li>Transaction not opened due to error. Duplicate items not allowed.</li>'; }; // create an object store on the transaction var objectStore = transaction.objectStore("toDoList"); // add our newItem object to the object store var objectStoreRequest = objectStore.add(newItem[0]); objectStoreRequest.onsuccess = function(event) { // report the success of our new item going into the database note.innerHTML += '<li>New item added to database.</li>'; }; };
Specifications
Specification | Status | Comment |
---|---|---|
Indexed Database API IDBTransaction |
Recommendation |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 23webkit 24 |
10 moz 16.0 (16.0) |
10, partial | 15 | 7.1 |
Feature | Android | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 4.4 | 22.0 (22.0) | 1.0.1 | 10 | 22 | 未实现 |
Known browser issues
Older versions of Google Chrome serialise all transactions. So even if you have only read-only transactions and no read-write transaction, your transactions are executed one at a time. Any subsequent transactions are not executed until read-only transactions are completed. For the status, see bug 64076.
See also
- Using IndexedDB
- Starting transactions:
IDBDatabase
- Using transactions:
IDBTransaction
- Setting a range of keys:
IDBKeyRange
- Retrieving and making changes to your data:
IDBObjectStore
- Using cursors:
IDBCursor
- Reference example: To-do Notifications (view example live.)