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
|
---
title: IDBTransaction
slug: Web/API/IDBTransaction
tags:
- API
- Database
- IDBTransaction
- IndexedDB
- Interface
- Reference
- Storage
translation_of: Web/API/IDBTransaction
---
<div>{{APIRef("IndexedDB")}}</div>
<p><strong><code>IDBTransaction</code></strong> は <a href="/ja/docs/IndexedDB">IndexedDB API</a> のインターフェイスで、イベントハンドラー属性を使用してデータベース上の静的で非同期のトランザクションを提供します。すべてのデータの読み書きはトランザクション内で行われます。 {{domxref("IDBDatabase")}} を使用してトランザクションを開始し、 {{domxref("IDBTransaction")}} を使用してトランザクションのモードを設定し (例 <code>readonly</code> または <code>readwrite</code>)、 {{domxref("IDBObjectStore")}} にアクセスしてリクエストを作成します。 <code>IDBTransaction</code> オブジェクトを使用してトランザクションを中止することもできます。</p>
<p>{{AvailableInWorkers}}</p>
<p>{{InheritanceDiagram}}</p>
<p>トランザクションは、最初のリクエストが発行された時ではなく、トランザクションが生成されたときに開始します。例えば、次のものを考えてください。</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>このコードが実行された後で、オブジェクトストアには "2" が含まれているはずであり、これは <code>trans2</code> が <code>trans1</code> の後に実行されるからです。</p>
<h2 id="Transaction_failures" name="Transaction_failures">トランザクションの失敗</h2>
<p>トランザクションは一定数の理由で失敗することがあり、 (ユーザーエージェントのクラッシュを除いて) すべての場合に abort コールバックを起動します。</p>
<ul>
<li>Abort due to bad requests, e.g. trying to <code>add()</code> the same key twice, or <code>put()</code> 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 <code>preventDefault()</code> on the error event on the request.</li>
<li>An explicit <code>abort()</code> call from script.</li>
<li>An uncaught exception in the request's <code>success</code>/<code>error</code> handler.</li>
<li>An I/O error (e.g. an actual failure to write to disk, or other OS/hardware failure).</li>
<li>Quota exceeded.</li>
<li>A user agent crash.</li>
</ul>
<h2 id="Firefox_durability_guarantees" name="Firefox_durability_guarantees">Firefox durability guarantees</h2>
<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>
<h2 id="Properties" name="Properties">プロパティ</h2>
<dl>
<dt>{{domxref("IDBTransaction.db")}} {{readonlyInline}}</dt>
<dd>The database connection with which this transaction is associated.</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 the{{domxref("IDBTransaction.abort()")}} function.</dd>
<dt>{{domxref("IDBTransaction.mode")}} {{readonlyInline}}</dt>
<dd>The mode for isolating access to data in the object stores that are in the scope of the transaction. The default value is <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 associated with the transaction.</dd>
</dl>
<h2 id="Methods" name="Methods">メソッド</h2>
<p>Inherits from: {{domxref("EventTarget")}}</p>
<dl>
<dt>{{domxref("IDBTransaction.abort()")}}</dt>
<dd>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.</dd>
<dt>{{domxref("IDBTransaction.objectStore()")}}</dt>
<dd>Returns an {{domxref("IDBObjectStore")}} object representing an <span class="internalDFN">object store</span> that is part of the <span class="internalDFN">scope</span> of this <span class="internalDFN">transaction</span>.</dd>
<dt>{{domxref("IDBTransaction.commit()")}}</dt>
<dd>For an active transaction, commits the transaction. Note that this doesn't normally <em>have</em> to be called — a transaction will automatically commit when all outstanding requests have been satisfied and no new requests have been made. <code>commit()</code> can be used to start the commit process without waiting for events from outstanding requests to be dispatched.</dd>
</dl>
<h2 id="Events" name="Events">イベント</h2>
<p>Listen to these events using <code>addEventListener()</code> or by assigning an event listener to the <code>on<em>eventname</em></code> property of this interface.</p>
<dl>
<dt><a href="/ja/docs/Web/API/IDBTransaction/abort_event"><code>abort</code></a></dt>
<dd>Fired when an <code>IndexedDB</code> transaction is aborted.<br>
Also available via the <code><a href="/ja/docs/Web/API/IDBTransaction/onabort">onabort</a></code> property.</dd>
<dt><a href="/ja/docs/Web/API/IDBTransaction/complete_event"><code>complete</code></a></dt>
<dd>Fired when a transaction successfully completes.<br>
Also available via the <code><a href="/ja/docs/Web/API/IDBTransaction/oncomplete">oncomplete</a></code> property.</dd>
<dt><a href="/ja/docs/Web/API/IDBTransaction/error_event"><code>error</code></a></dt>
<dd>Fired when a request returns an error and the event bubbles up to the transaction object.<br>
Also available via the <code><a href="/ja/docs/Web/API/IDBTransaction/onerror">onerror</a></code> property.</dd>
</dl>
<h2 id="Mode_constants" name="Mode_constants">Mode constants</h2>
<div>
<div>{{ deprecated_header(13) }}</div>
<div class="warning">
<p>These constants are no longer available — they were removed in Gecko 25. You should use the string constants directly instead. ({{ bug(888598) }})</p>
</div>
</div>
<p>Transactions can have one of three modes:</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>READ_ONLY</a></code></td>
<td>
<p>"readonly"</p>
<p>(0 in Chrome)</p>
</td>
<td>
<p>Allows data to be read but not changed.</p>
</td>
</tr>
<tr>
<td><code><a>READ_WRITE</a></code></td>
<td>
<p>"readwrite"</p>
<p>(1 in Chrome)</p>
</td>
<td>Allows reading and writing of data in existing data stores to be changed.</td>
</tr>
<tr>
<td><code><a>VERSION_CHANGE</a></code></td>
<td>
<p>"versionchange"</p>
<p>(2 in Chrome)</p>
</td>
<td>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 <a href="/ja/docs/IndexedDB/IDBDatabase#setVersion"><code>setVersion()</code></a> method of <a href="/ja/docs/IndexedDB/IDBDatabase">IDBDatabase</a> objects. Transactions of this mode cannot run concurrently with other transactions. Transactions in this mode are known as "upgrade transactions."</td>
</tr>
</tbody>
</table>
<p>Even if these constants are now deprecated, you can still use them to provide backward compatibility if required (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>). You should code defensively in case the object is not available anymore:</p>
<pre class="brush:js;">var myIDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || { READ_WRITE: "readwrite" };</pre>
<h2 id="Examples" name="Examples">例</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/">To-do Notifications</a> app (<a href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</p>
<pre class="brush: js">// 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>';
};
};</pre>
<h2 id="Specifications" name="Specifications">仕様書</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">仕様書</th>
<th scope="col">状態</th>
<th scope="col">備考</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('IndexedDB', '#transaction', 'IDBTransaction')}}</td>
<td>{{Spec2('IndexedDB')}}</td>
<td>初回定義</td>
</tr>
<tr>
<td>{{SpecName("IndexedDB 2", "#transaction", "IDBTransaction")}}</td>
<td>{{Spec2("IndexedDB 2")}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>
<div>
<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div>
<div>{{Compat("api.IDBTransaction")}}</div>
</div>
<h2 id="See_also" name="See_also">関連情報</h2>
<ul>
<li><a href="/ja/docs/Web/API/IndexedDB_API/Using_IndexedDB">IndexedDB の使用</a></li>
<li>トランザクションの開始: {{domxref("IDBDatabase")}}</li>
<li>トランザクションの仕様: {{domxref("IDBTransaction")}}</li>
<li>キーの範囲の設定: {{domxref("IDBKeyRange")}}</li>
<li>データの受け取りや変更: {{domxref("IDBObjectStore")}}</li>
<li>カーソルの使用: {{domxref("IDBCursor")}}</li>
<li>リファレンスの例: <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>
|