IDBTransaction
接口在
IndexedDB API
provides a static, asynchronous transaction on a database using event handler attributes. All reading and writing of data is done within transactions. You use
IDBDatabase
to start transactions,
IDBTransaction
to set the mode of the transaction (e.g. is it
readonly
or
readwrite
), and you access an
IDBObjectStore
to make a request. You can also use an
IDBTransaction
object to abort transactions.
<div id="interfaceDiagram" style="display: inline-block; position: relative; width: 100%; padding-bottom: 11.666666666666666%; vertical-align: middle; overflow: hidden;"><svg style="display: inline-block; position: absolute; top: 0; left: 0;" viewbox="-50 0 600 70" preserveAspectRatio="xMinYMin meet"><a xlink:href="../API/EventTarget.html" target="_top"><rect x="1" y="1" width="110" height="50" fill="#fff" stroke="#D4DDE4" stroke-width="2px" /><text x="56" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">EventTarget</text></a><polyline points="111,25 121,20 121,30 111,25" stroke="#D4DDE4" fill="none"/><line x1="121" y1="25" x2="151" y2="25" stroke="#D4DDE4"/><a xlink:href="../API/IDBTransaction" target="_top"><rect x="151" y="1" width="140" height="50" fill="#F4F7F8" stroke="#D4DDE4" stroke-width="2px" /><text x="221" y="30" font-size="12px" font-family="Consolas,Monaco,Andale Mono,monospace" fill="#4D4E53" text-anchor="middle" alignment-baseline="middle">IDBTransaction</text></a></svg></div>
a:hover text { fill: #0095DD; pointer-events: all;}
Transactions are started when the transaction is created, not when the first request is placed; for example consider this:
var trans1 = db.transaction("foo", "readwrite");
var trans2 = db.transaction("foo", "readwrite");
var objectStore2 = trans2.objectStore("foo")
var objectStore1 = trans1.objectStore("foo")
objectStore2.put("2", "key");
objectStore1.put("1", "key");
After the code is executed the object store should contain the value "2", since
trans2
should run after
trans1
.
Transactions can fail for a fixed number of reasons, all of which (except the user agent crash) will trigger an abort callback:
add()
the same key twice, or
put()
with the same index key with a uniqueness constraint. This causes an error on the request, which can bubble up to an error on the transaction, which aborts the transaction. This can be prevented by using
preventDefault()
on the error event on the request.
abort()
call from script.
success
/
error
handler.
Note that as of Firefox 40, IndexedDB transactions have relaxed durability guarantees to increase performance (see
bug 1112702
.) Previously in a
readwrite
transaction
IDBTransaction.oncomplete
was fired only when all data was guaranteed to have been flushed to disk. In Firefox 40+ the
complete
event is fired after the OS has been told to write the data but potentially before that data has actually been flushed to disk. The
complete
event may thus be delivered quicker than before, however, there exists a small chance that the entire transaction will be lost if the OS crashes or there is a loss of system power before the data is flushed to disk. Since such catastrophic events are rare, most consumers should not need to concern themselves further.
If you must ensure durability for some reason (e.g. you're storing critical data that cannot be recomputed later) you can force a transaction to flush to disk before delivering the
complete
event by creating a transaction using the experimental (non-standard)
readwriteflush
mode (see
IDBDatabase.transaction
.
IDBTransaction.db
只读
The database connection with which this transaction is associated.
IDBTransaction.error
只读
DOMException
indicating the type of error that occured when there is an unsuccessful transaction. This property is
null
if the transaction is not finished, is finished and successfully committed, or was aborted with the
IDBTransaction.abort()
函数。
IDBTransaction.mode
只读
readonly
.
IDBTransaction.objectStoreNames
只读
DOMStringList
of the names of
IDBObjectStore
objects associated with the transaction.
继承自:
EventTarget
IDBTransaction.abort()
Rolls back all the changes to objects in the database associated with this transaction. If this transaction has been aborted or completed, this method fires an error event.
IDBTransaction.objectStore()
IDBObjectStore
object representing an
object store
that is part of the
scope
of this
transaction
.
IDBTransaction.commit()
commit()
can be used to start the commit process without waiting for events from outstanding requests to be dispatched.
监听这些事件使用
addEventListener()
或通过把事件监听器赋值给
on
eventname
特性为此接口。
abort
IndexedDB
transaction is aborted.
onabort
特性。
complete
oncomplete
特性。
error
onerror
特性。
Deprecated since Gecko 13 (Firefox 13 / Thunderbird 13 / SeaMonkey 2.10)
This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the
兼容性表格
at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
These constants are no longer available — they were removed in Gecko 25. You should use the string constants directly instead. ( bug 888598 )
Transactions can have one of three modes:
| 常量 | 值 | 描述 |
|---|---|---|
READ_ONLY
|
"readonly"
(0 in Chrome) |
Allows data to be read but not changed. |
READ_WRITE
|
"readwrite"
(1 in Chrome) |
Allows reading and writing of data in existing data stores to be changed. |
VERSION_CHANGE
|
"versionchange"
(2 in Chrome) |
Allows any operation to be performed, including ones that delete and create object stores and indexes. This mode is for updating the version number of transactions that were started using the
setVersion()
方法为
IDBDatabase
objects. Transactions of this mode cannot run concurrently with other transactions. Transactions in this mode are known as "upgrade transactions."
|
Even if these constants are now deprecated, you can still use them to provide backward compatibility if required (in Chrome the change was made in version 21 ). You should code defensively in case the object is not available anymore:
var myIDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || { READ_WRITE: "readwrite" };
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 待办通知 app ( 实时查看范例 )。
// 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;
// Add the data to the database
addData();
};
function addData() {
// Create a new object to insert 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 to add 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 the request (this does not mean the item
// has been stored successfully in the DB - for that you need transaction.oncomplete)
note.innerHTML += '<li>Request successful.</li>';
};
};
| 规范 | 状态 | 注释 |
|---|---|---|
|
索引数据库 API 2.0
The definition of 'IDBTransaction' in that specification. |
推荐 | 初始定义 |
|
索引数据库 API 草案
The definition of 'IDBTransaction' in that specification. |
推荐 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
IDBTransaction
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
abort
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
abort
event
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
commit
|
Chrome 76 | Edge 79 | Firefox 74 | IE 不支持 No | Opera 63 | Safari 不支持 No | WebView Android 76 | Chrome Android 76 | Firefox Android 不支持 No | Opera Android 54 | Safari iOS 不支持 No | Samsung Internet Android 12.0 |
complete
event
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
db
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
error
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
error
event
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
mode
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
objectStore
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
objectStoreNames
|
Chrome 48 | Edge ≤79 | Firefox Yes | IE ? | Opera 35 | Safari Yes | WebView Android 48 | Chrome Android 48 | Firefox Android Yes | Opera Android 35 | Safari iOS Yes | Samsung Internet Android 5.0 |
onabort
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
oncomplete
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
onerror
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 | WebView Android Yes | Chrome Android Yes | Firefox Android 22 | Opera Android 14 | Safari iOS 8 | Samsung Internet Android Yes |
| Available in workers | Chrome Yes | Edge ≤79 | Firefox 37 | IE ? | Opera Yes | Safari ? | WebView Android Yes | Chrome Android Yes | Firefox Android 37 | Opera Android Yes | Safari iOS ? | Samsung Internet Android Yes |
完整支持
部分支持
不支持
兼容性未知
要求使用供应商前缀或不同名称。
IDBDatabase
IDBTransaction
IDBKeyRange
IDBObjectStore
IDBCursor
IDBTransaction
IDBCursor
IDBCursorSync
IDBCursorWithValue
IDBDatabase
IDBDatabaseException
IDBDatabaseSync
IDBEnvironment
IDBEnvironmentSync
IDBFactory
IDBFactorySync
IDBIndex
IDBIndexSync
IDBKeyRange
IDBObjectStore
IDBObjectStoreSync
IDBOpenDBRequest
IDBRequest
IDBTransactionSync
IDBVersionChangeEvent
IDBVersionChangeRequest