1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
---
title: IDBTransaction
slug: Web/API/IDBTransaction
translation_of: Web/API/IDBTransaction
---
<p>{{APIRef("IndexedDB")}}</p>
<div>
<p><code>IDBTransacation</code>接口由<a href="/en-US/docs/IndexedDB">IndexedDB API</a>提供,异步事务使用数据库中的事件对象属性。所有的读取和写入数据均在事务中完成。由{{domxref("IDBDatabase")}}发起事务,通过{{domxref("IDBTransaction")}} 来设置事务的模式(例如:是否只读<code>readonly</code>或读写<code>readwrite</code>),以及通过{{domxref("IDBObjectStore")}}来发起一个请求。同时你也可以使用它来中止事务。</p>
</div>
<p>Note that as of Firefox 40, IndexedDB transactions have relaxed durability guarantees to increase performance (see {{Bug("1112702")}}.) Previously in a <code>readwrite</code> transaction {{domxref("IDBTransaction.oncomplete")}} was fired only when all data was guaranteed to have been flushed to disk. In Firefox 40+ the <code>complete</code> 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 <code>complete</code> 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.</p>
<p>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 <code>complete</code> event by creating a transaction using the experimental (non-standard) <code>readwriteflush</code> mode (see {{domxref("IDBDatabase.transaction")}}.</p>
<p>注意,事务在被创建的时候就已经开始,并非在发起第一个请求(<code>IDBRequest</code>)的时候。例如下面的例子:</p>
<pre class="brush: js" id="comment_text_0">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");
</pre>
<p>在代码执行后,object store 应该包含值 "2", 因为 <code>trans2</code> 应该在 <code>trans1</code> 之后执行。</p>
<p>Transactions can fail for a fixed number of reasons, all of which (except the user agent crash) will trigger an abort callback:</p>
<ul>
<li>Abort due to bad requests, e.g. trying to 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. Can be prevented by using preventDefault() on the error event on the request.</li>
<li>Explicit abort() call from script</li>
<li>Uncaught exception in request's success/error handler</li>
<li>I/O error (actual failure to write to disk, e.g. disk detached, or other OS/hardware failure)</li>
<li>Quota exceeded</li>
<li>User agent crash</li>
</ul>
<p> </p>
<p>{{AvailableInWorkers}}</p>
<p>{{InheritanceDiagram}}</p>
<h2 id="属性">属性</h2>
<dl>
<dt>{{domxref("IDBTransaction.db")}} {{readonlyInline}}</dt>
<dd>当前事务所属的数据库连接。</dd>
<dt>{{domxref("IDBTransaction.error")}} {{readonlyInline}}</dt>
<dd>Returns a {{domxref("DOMException")}} indicating the type of error that occured when there is an unsuccessful transaction. This property is <code>null</code> if the transaction is not finished, is finished and successfully committed, or was aborted with {{domxref("IDBTransaction.abort")}} function.</dd>
<dt>{{domxref("IDBTransaction.mode")}} {{readonlyInline}}</dt>
<dd>用于隔离事务作用域内的object store中数据访问的模式。下方的常量章节给出了所有可用的值。默认值是 <code><a href="#const_read_only">readonly</a></code>.</dd>
<dt>{{domxref("IDBTransaction.objectStoreNames")}} {{readonlyinline}}</dt>
<dd>Returns a {{domxref("DOMStringList")}} of the names of {{domxref("IDBObjectStore")}} objects.</dd>
</dl>
<h3 id="Event_handlers">Event handlers</h3>
<dl>
<dt>{{domxref("IDBTransaction.onabort")}} {{readonlyInline}}</dt>
<dd>The event handler for the <code>abort</code> event, fired when the transaction is aborted. This can happen due to:
<ul>
<li>bad requests, e.g. trying to add() the same key twice, or put() with the same index key with a uniqueness constraint and there is no error handler on the request to call preventDefault() on the event,</li>
<li>an explicit abort() call from script</li>
<li>uncaught exception in request's success/error handler,</li>
<li>an I/O error (actual failure to write to disk, e.g. disk detached, or other OS/hardware failure), or</li>
<li>quota exceeded.</li>
</ul>
</dd>
<dt>{{domxref("IDBTransaction.oncomplete")}} {{readonlyInline}}</dt>
<dd>The event handler for the <code>complete</code> event, thrown when the transaction completes successfully.</dd>
<dt>{{domxref("IDBTransaction.onerror")}} {{readonlyInline}}</dt>
<dd>The event handler for the <code>error</code> event, thrown when the transaction fails to complete.</dd>
</dl>
<h2 id="方法">方法</h2>
<p>从{{domxref("EventTarget")}}继承</p>
<dl>
<dt>{{domxref("IDBTransaction.abort")}}</dt>
<dd>放弃本次连接的transaction的所有修改,如果当前的transaction处于回滚完毕或完成状态,则会抛出一个错误事件。</dd>
<dt>{{domxref("IDBTransaction.objectStore")}}</dt>
<dd>返回表示作为此事务作用域一部分的object store的 {{domxref("IDBObjectStore")}} 对象。</dd>
</dl>
<h2 id="Constants" name="Constants">模式常量</h2>
<div>{{ obsolete_header(25) }}</div>
<div class="warning">
<p>这些常量将不再可用——它们在Gecko 25中被移除。 你应该直接使用字符串常量来作为替代。 ({{ bug(888598) }})</p>
</div>
<p>Transactions 可使用以下三种模式中的一种:</p>
<table class="standard-table">
<thead>
<tr>
<th scope="col">常量</th>
<th scope="col">值</th>
<th scope="col">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><a name="const_read_only">READ_ONLY</a></code></td>
<td>
<p>"readonly"</p>
<p>(0 in Chrome)</p>
</td>
<td>
<p>允许读取数据,不改变。</p>
</td>
</tr>
<tr>
<td><code><a name="const_read_write">READ_WRITE</a></code></td>
<td>
<p>"readwrite"</p>
<p>(1 in Chrome)</p>
</td>
<td>
<div>允许读取和写入现有数据存储,数据被改变。</div>
</td>
</tr>
<tr>
<td><code><a name="VERSION_CHANGE">VERSION_CHANGE</a></code></td>
<td>
<p>"versionchange"</p>
<p>(2 in Chrome)</p>
</td>
<td>允许执行任何操作,包括删除和创建对象存储和索引。<br>
此模式是用于开始使用<a href="/en-US/docs/IndexedDB/IDBDatabase">IDBDatabase</a> 的 <code><a href="/en-US/docs/IndexedDB/IDBDatabase#setVersion">setVersion()</a></code>方法更新版本号事务。 这种模式的事务无法与其它事务并发运行。<br>
这种模式下的事务被称为“升级事务”。</td>
</tr>
</tbody>
</table>
<p>即使目前这些常量已经被废弃,但如果你需要使用它,则需要提供向下兼容方案(in Chrome <a href="http://peter.sh/2012/05/tab-sizing-string-values-for-indexeddb-and-chrome-21/">the change was made in version 21</a>)。你应当防止出现对象不存在的情况:</p>
<pre class="brush:js;">var myIDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || { READ_WRITE: "readwrite" };</pre>
<h2 id="Example">Example</h2>
<p>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 <a href="https://github.com/mdn/to-do-notifications/" style="line-height: 1.5;">To-do Notifications</a><span style="line-height: 1.5;"> app (</span><a href="http://mdn.github.io/to-do-notifications/" style="line-height: 1.5;">view example live</a><span style="line-height: 1.5;">.)</span></p>
<pre class="brush: js">// Let us open our database
<span style="line-height: 1.5;">var DBOpenRequest = window.indexedDB.open("toDoList", 4);
</span><span style="line-height: 1.5;">DBOpenRequest.onsuccess = function(event) {</span>
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>';
};
};</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('IndexedDB', '#transaction', 'IDBTransaction')}}</td>
<td>{{Spec2('IndexedDB')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari (WebKit)</th>
</tr>
<tr>
<td>Basic support</td>
<td>23{{property_prefix("webkit")}}<br>
24</td>
<td>10 {{property_prefix("moz")}}<br>
{{CompatGeckoDesktop("16.0")}}</td>
<td>10, partial</td>
<td>15</td>
<td>7.1</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>Firefox OS</th>
<th>IE Phone</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>4.4</td>
<td>{{CompatGeckoMobile("22.0")}}</td>
<td>1.0.1</td>
<td>10</td>
<td>22</td>
<td>{{CompatNo}}</td>
</tr>
</tbody>
</table>
</div>
<h3 id="issues" name="issues">Known browser issues</h3>
<p>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 <a href="http://code.google.com/p/chromium/issues/detail?id=64076">bug 64076</a>.</p>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></li>
<li>Starting transactions: {{domxref("IDBDatabase")}}</li>
<li>Using transactions: {{domxref("IDBTransaction")}}</li>
<li>Setting a range of keys: {{domxref("IDBKeyRange")}}</li>
<li>Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}</li>
<li>Using cursors: {{domxref("IDBCursor")}}</li>
<li>Reference example: <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li>
</ul>
|