Skip to content

Commit 74b6e2a

Browse files
git squash commit for manualcommit.
461db42e966892b49eb9f689c20d1e4fcde0f618 Initial sketch of manual commit fix/add 'new in this edition' hints version history invert flag check don't let tx go inactive post-rebase cleanup 6fbe9e5f5221c00700c1b2e2a0707249177f4414 bangbang
1 parent af5536b commit 74b6e2a

File tree

1 file changed

+125
-17
lines changed

1 file changed

+125
-17
lines changed

index.bs

Lines changed: 125 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,8 @@ following:
948948
instead is created automatically when an
949949
{{IDBOpenDBRequest/upgradeneeded!!event}} event is fired.
950950

951+
A [=/transaction=] has an <dfn>manual commit flag</dfn>, which is initially false. By default, transactions commit automatically when all outstanding requests have been processed. This can be behavior can be modified by options set when creating the transaction.
952+
951953
A [=/transaction=] has a <dfn>durability hint</dfn>. This is a hint to the user agent of whether to prioritize performance or durability when committing the transaction. The [=transaction/durability hint=] is one of the following:
952954

953955
: "{{IDBTransactionDurability/strict}}"
@@ -1055,10 +1057,11 @@ The <dfn>lifetime</dfn> of a
10551057
</aside>
10561058

10571059
1. When each [=/request=] associated with a transaction is [=request/processed=],
1058-
a {{IDBRequest/success!!event}} or {{IDBRequest/error!!event}} [=event=] will be
1059-
fired. While the event is being [=dispatched=], the transaction
1060+
a {{IDBRequest/success!!event}} or {{IDBRequest/error!!event}} [=event=] will be fired.
1061+
If the transaction's [=transaction/manual commit flag=] is false (the default), then
1062+
while the event is being [=dispatched=], the transaction
10601063
[=transaction/state=] is set to [=transaction/active=], allowing
1061-
additional requests to be made against the transaction. Once the
1064+
additional requests to be made against the transaction; once the
10621065
event dispatch is complete, the transaction's
10631066
[=transaction/state=] is set to [=transaction/inactive=] again.
10641067

@@ -1077,11 +1080,12 @@ The <dfn>lifetime</dfn> of a
10771080
[=/object stores=] as well as additions and removals of [=/object
10781081
stores=] and [=/indexes=].
10791082

1080-
1. The implementation must attempt to <dfn lt="commit|committed">commit</dfn>
1081-
a transaction when all [=/requests=] placed against the
1083+
1. If a transaction's [=transaction/manual commit flag=] is false, then
1084+
the implementation must attempt to <dfn lt="commit|committed">commit</dfn>
1085+
it when all [=/requests=] placed against the
10821086
transaction have completed and their returned results handled, no
10831087
new requests have been placed against the transaction, and the
1084-
transaction has not been [=transaction/aborted=]
1088+
transaction has not been [=transaction/aborted=].
10851089

10861090
An explicit call to {{IDBTransaction/commit()}} will initiate a
10871091
[=transaction/commit=] without waiting for request results to be
@@ -1118,7 +1122,7 @@ They will return true if any transactions were cleaned up, or false otherwise.
11181122
1. For each [=/transaction=] |transaction| with [=transaction/cleanup event loop=]
11191123
matching the current [=/event loop=]:
11201124

1121-
1. Set |transaction|'s [=transaction/state=] to [=transaction/inactive=].
1125+
1. If |transaction|'s [=transaction/manual commit flag=] is false, then set |transaction|'s [=transaction/state=] to [=transaction/inactive=].
11221126

11231127
1. Clear |transaction|'s [=transaction/cleanup event loop=].
11241128

@@ -1139,6 +1143,78 @@ a [=/transaction=] that has successfully [=transaction/committed=].
11391143
An event with type <dfn event for=IDBTransaction>`abort`</dfn> is fired at
11401144
a [=/transaction=] that has [=transaction/aborted=].
11411145

1146+
1147+
<aside class=example id=example-transaction-autocommit>
1148+
1149+
The following example uses requests within a transaction to increment a counter value.
1150+
Event handlers log various state changes during the request and transaction lifecycles.
1151+
1152+
```js
1153+
const transaction = connection.transaction('my_data', 'readwrite');
1154+
const store = transaction.objectStore('my_data');
1155+
const get_request = store.get('counter');
1156+
get_request.onerror = e => {
1157+
console.warn(`get failed: ${get_request.error}`);
1158+
};
1159+
get_request.onsuccess = e => {
1160+
console.log(`get succeeded`);
1161+
const old_value = get_request.result;
1162+
const put_request = store.put(old_value + 1, 'counter');
1163+
put_request.onerror = e => {
1164+
console.warn(`get failed: ${put_request.error}`);
1165+
};
1166+
put_request.onsuccess = {
1167+
console.log(`put succeeded`);
1168+
// No more requests are made, so the transaction will autocommit.
1169+
};
1170+
};
1171+
1172+
transaction.onabort = e => {
1173+
console.warn(`transaction aborted: ${transaction.error}`);
1174+
};
1175+
1176+
transaction.oncomplete = e => {
1177+
console.log(`transaction committed`);
1178+
};
1179+
```
1180+
</aside>
1181+
1182+
<aside class=example id=example-transaction-manualcommit>
1183+
1184+
The following example uses requests within a transaction to atomically
1185+
update a record's value using an asynchronous network fetch. A manually
1186+
committing transaction is necessary.
1187+
1188+
```js
1189+
const transaction = connection.transaction(
1190+
'my_records, 'readwrite', {manualCommit: true});
1191+
1192+
const store = transaction.objectStore('my_records);
1193+
store.get('key').onsuccess = async e => {
1194+
try {
1195+
const record = e.target.result;
1196+
1197+
// Make an asynchronous network request. If the transaction
1198+
// was not set to manual commit, it would autocommit here
1199+
// because there were no outstanding requests.
1200+
const response = await fetch(record.url);
1201+
1202+
// Update the record.
1203+
record.status = response.status;
1204+
store.put(record, 'key');
1205+
1206+
// Commit the transaction, once the request has completed.
1207+
// If the request fails, the transaction will abort instead.
1208+
transaction.commit();
1209+
} catch (ex) {
1210+
// If the fetch() fails, abort the transaction.
1211+
transaction.abort();
1212+
}
1213+
});
1214+
```
1215+
</aside>
1216+
1217+
11421218
<!-- ============================================================ -->
11431219
### Transaction scheduling ### {#transaction-scheduling}
11441220
<!-- ============================================================ -->
@@ -2448,6 +2524,7 @@ interface IDBDatabase : EventTarget {
24482524
enum IDBTransactionDurability { "default", "strict", "relaxed" };
24492525

24502526
dictionary IDBTransactionOptions {
2527+
boolean manualCommit = false;
24512528
IDBTransactionDurability durability = "default";
24522529
};
24532530

@@ -2641,9 +2718,13 @@ instance on which it was called.
26412718
Returns a new [=/transaction=] with the given
26422719
|scope| (which can be a single [=/object store=] [=object-store/name=] or an array of [=object-store/names=]),
26432720
|mode| ("{{IDBTransactionMode/readonly}}" or "{{IDBTransactionMode/readwrite}}"),
2644-
and additional |options| including {{IDBTransactionOptions/durability}} ("{{IDBTransactionDurability/default}}", "{{IDBTransactionDurability/strict}}" or "{{IDBTransactionDurability/relaxed}}").
2721+
and additional |options| including
2722+
{{IDBTransactionOptions/manualCommit}} (true or false), and
2723+
{{IDBTransactionOptions/durability}} ("{{IDBTransactionDurability/default}}", "{{IDBTransactionDurability/strict}}" or "{{IDBTransactionDurability/relaxed}}").
26452724

2646-
The default |mode| is "{{IDBTransactionMode/readonly}}" and the default {{IDBTransactionOptions/durability}} is "{{IDBTransactionDurability/default}}".
2725+
The default |mode| is "{{IDBTransactionMode/readonly}}".
2726+
The default {{IDBTransactionOptions/manualCommit}} is false.
2727+
The default {{IDBTransactionOptions/durability}} is "{{IDBTransactionDurability/default}}".
26472728

26482729
: |connection| . {{IDBDatabase/close()|close}}()
26492730
::
@@ -2674,7 +2755,11 @@ The <dfn method for=IDBDatabase>transaction(|storeNames|, |mode|, |options|)</df
26742755
1. If |mode| is not "{{IDBTransactionMode/readonly}}" or "{{IDBTransactionMode/readwrite}}",
26752756
[=exception/throw=] a {{TypeError}}.
26762757

2677-
1. Let |transaction| be a newly [=transaction/created=] [=/transaction=] with this [=/connection=], |mode|, |options|' {{IDBTransactionOptions/durability}} member, and the set of [=/object stores=] named in |scope|.
2758+
1. Let |transaction| be a newly [=transaction/created=] [=/transaction=] with this [=/connection=], |mode|, and the set of [=/object stores=] named in |scope|.
2759+
2760+
1. Set |transaction|'s [=transaction/manual commit flag=] to |options|' {{IDBTransactionOptions/manualCommit}} member.
2761+
2762+
1. Set |transaction|'s [=transaction/durability hint=] to |options|' {{IDBTransactionOptions/durability}} member.
26782763

26792764
1. Set |transaction|'s [=transaction/cleanup event loop=] to the
26802765
current [=/event loop=].
@@ -2683,6 +2768,12 @@ The <dfn method for=IDBDatabase>transaction(|storeNames|, |mode|, |options|)</df
26832768

26842769
</div>
26852770

2771+
<aside class=advisement>
2772+
&#x1F6A7;
2773+
The {{IDBTransactionOptions/manualCommit}} option is new in this edition.
2774+
&#x1F6A7;
2775+
</aside>
2776+
26862777
<aside class=advisement>
26872778
&#x1F6A7;
26882779
The {{IDBTransactionOptions/durability}} option is new in this edition.
@@ -4738,6 +4829,7 @@ the contents of the database.
47384829
interface IDBTransaction : EventTarget {
47394830
readonly attribute DOMStringList objectStoreNames;
47404831
readonly attribute IDBTransactionMode mode;
4832+
readonly attribute boolean manualCommit;
47414833
readonly attribute IDBTransactionDurability durability;
47424834
[SameObject] readonly attribute IDBDatabase db;
47434835
readonly attribute DOMException? error;
@@ -4772,6 +4864,10 @@ enum IDBTransactionMode {
47724864
("{{IDBTransactionMode/readonly}}" or "{{IDBTransactionMode/readwrite}}"), or "{{IDBTransactionMode/versionchange}}" for
47734865
an [=/upgrade transaction=].
47744866

4867+
: |transaction| . {{IDBTransaction/manualCommit}}
4868+
::
4869+
Returns true if the transaction was created with {{IDBTransactionOptions/manualCommit}}: `true` and false otherwise.
4870+
47754871
: |transaction| . {{IDBTransaction/durability}}
47764872
::
47774873
Returns the [=transaction/durability hint=] the transaction was created with
@@ -4807,6 +4903,14 @@ The <dfn attribute for=IDBTransaction>objectStoreNames</dfn> getter steps are:
48074903
The <dfn attribute for=IDBTransaction>mode</dfn> getter steps are to
48084904
return [=/this=]'s [=transaction/mode=].
48094905

4906+
The <dfn attribute for=IDBTransaction>manualCommit</dfn> attribute's getter must return [=/this=]'s [=transaction/manual commit flag=].
4907+
4908+
<aside class=advisement>
4909+
&#x1F6A7;
4910+
The {{IDBTransaction/manualCommit}} attribute is new in this edition.
4911+
&#x1F6A7;
4912+
</aside>
4913+
48104914
The <dfn attribute for=IDBTransaction>durability</dfn> getter steps are to return [=/this=]'s [=transaction/durability hint=].
48114915

48124916
<aside class=advisement>
@@ -4816,7 +4920,6 @@ The <dfn attribute for=IDBTransaction>durability</dfn> getter steps are to retur
48164920
&#x1F6A7;
48174921
</aside>
48184922

4819-
48204923
The <dfn attribute for=IDBTransaction>db</dfn> getter steps are to
48214924
return [=/this=]'s [=transaction/connection=]'s associated [=/database=].
48224925

@@ -4854,6 +4957,9 @@ none.
48544957
transaction to quickly finish, without waiting for pending requests to fire
48554958
{{IDBRequest/success!!event}} events before attempting to commit normally.
48564959

4960+
If the transaction was created with `manualCommit: true` then this method
4961+
<span class=allow-2119>must</span> be called to commit the transaction.
4962+
48574963
The transaction will abort if a pending request fails, for example due to a
48584964
constraint error. The {{IDBRequest/success!!event}} events for successful requests
48594965
will still fire, but throwing an exception in an event handler will not abort
@@ -4910,7 +5016,8 @@ The <dfn method for=IDBTransaction>abort()</dfn> method steps are:
49105016

49115017
The <dfn method for=IDBTransaction>commit()</dfn> method steps are:
49125018

4913-
1. If [=/this=]'s [=transaction/state=] is not [=transaction/active=],
5019+
1. If [=/this=]'s [=transaction/manual commit flag=] is false,
5020+
and [=/this=]'s [=transaction/state=] is not [=transaction/active=],
49145021
then [=exception/throw=] an "{{InvalidStateError}}" {{DOMException}}.
49155022

49165023
1. Run [=commit a transaction=] with [=/this=].
@@ -5170,7 +5277,7 @@ To <dfn>commit a transaction</dfn> with the |transaction| to commit, run these s
51705277
<aside class=note>
51715278
Even if an exception is thrown from one of the event handlers of
51725279
this event, the transaction is still committed since writing the
5173-
database changes happens before the event takes places. Only
5280+
database changes happens before the event takes place. Only
51745281
after the transaction has been successfully written is the
51755282
{{IDBTransaction/complete!!event}} event fired.
51765283
</aside>
@@ -5499,14 +5606,14 @@ To <dfn>fire a success event</dfn> at a |request|, run these steps:
54995606
1. If |transaction|'s [=transaction/state=] is [=transaction/active=],
55005607
then:
55015608

5502-
1. Set |transaction|'s [=transaction/state=] to [=transaction/inactive=].
5609+
1. If |transaction|'s [=transaction/manual commit flag=] is false, then set |transaction|'s [=transaction/state=] to [=transaction/inactive=].
55035610

55045611
1. If |legacyOutputDidListenersThrowFlag| is true,
55055612
then run [=abort a transaction=] with
55065613
|transaction| and a newly [=exception/created=]
55075614
"{{AbortError}}" {{DOMException}}.
55085615

5509-
1. If |transaction|'s [=transaction/request list=] is empty,
5616+
1. If |transaction|'s [=transaction/manual commit flag=] is false and if |transaction|'s [=transaction/request list=] is empty,
55105617
then run [=commit a transaction=] with |transaction|.
55115618

55125619
</div>
@@ -5538,7 +5645,7 @@ To <dfn>fire an error event</dfn> at a |request|, run these steps:
55385645
1. If |transaction|'s [=transaction/state=] is [=transaction/active=],
55395646
then:
55405647

5541-
1. Set |transaction|'s [=transaction/state=] to [=transaction/inactive=].
5648+
1. If |transaction|'s [=transaction/manual commit flag=] is false, then set |transaction|'s [=transaction/state=] to [=transaction/inactive=].
55425649

55435650
1. If |legacyOutputDidListenersThrowFlag| is true,
55445651
then run [=abort a transaction=] with
@@ -5559,7 +5666,7 @@ To <dfn>fire an error event</dfn> at a |request|, run these steps:
55595666
|transaction| and [=/request=]'s [=request/error=], and
55605667
terminate these steps.
55615668

5562-
1. If |transaction|'s [=transaction/request list=] is empty,
5669+
1. If |transaction|'s [=transaction/manual commit flag=] is false and |transaction|'s [=transaction/request list=] is empty,
55635670
then run [=commit a transaction=] with |transaction|.
55645671

55655672
</div>
@@ -6800,6 +6907,7 @@ For the revision history of the second edition, see [that document's Revision Hi
68006907
* Specified [[#transaction-scheduling]] more precisely and disallow starting read/write transactions while read-only transactions with overlapping scope are running. ([Issue #253](https://github.com/w3c/IndexedDB/issues/253))
68016908
* Added <a href="#accessibility">Accessibility considerations</a> section. ([Issue #327](https://github.com/w3c/IndexedDB/issues/327))
68026909
* Used [[infra]]'s list sorting definition. ([Issue #346](https://github.com/w3c/IndexedDB/issues/346))
6910+
* Specified [=transaction/manual commit flag=], {{IDBTransactionOptions/manualCommit}} option and {{IDBTransaction/manualCommit}} attribute. ([Issue #34](https://github.com/w3c/IndexedDB/issues/34))
68036911

68046912
<!-- ============================================================ -->
68056913
# Acknowledgements # {#acknowledgements}

0 commit comments

Comments
 (0)