IDBTransaction: `abort`-Ereignis

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2021.

Das abort-Ereignis wird ausgelöst, wenn eine IndexedDB-Transaktion abgebrochen wird.

Dies kann aus einem der folgenden Gründe geschehen:

  • Falsche Anfragen (z.B. der Versuch, denselben Schlüssel zweimal hinzuzufügen, oder denselben Indexschlüssel zu setzen, wenn auf dem Schlüssel eine Einzigartigkeitsbedingung liegt).
  • Ein expliziter Aufruf von abort().
  • Eine nicht abgefangene Ausnahme im Erfolgs-/Fehlerhandler der Anfrage.
  • Ein E/A-Fehler (ein tatsächlicher Schreibfehler auf die Festplatte, beispielsweise abgetrennte Festplatte oder ein anderer Betriebssystem-/Hardwarefehler).
  • Überschreiten des Speicherplatzkontingents.

Dieses nicht abbrechbare Ereignis bubbelt zum zugehörigen IDBDatabase-Objekt.

Syntax

Verwenden Sie den Ereignisnamen in Methoden wie addEventListener(), oder setzen Sie eine Ereignis-Handler-Eigenschaft.

js
addEventListener("abort", (event) => {});
onabort = (event) => {};

Ereignistyp

Ein generisches Event.

Bubbling

Dieses Ereignis bubblet zu IDBDatabase. Die Eigenschaft event.target bezieht sich auf das IDBTransaction-Objekt, das hochbubbelt.

Für weitere Informationen siehe Ereignis-Bubbling.

Beispiele

Dieses Beispiel öffnet eine Datenbank (und erstellt die Datenbank, falls sie nicht existiert), öffnet dann eine Transaktion, fügt einen Listener für das abort-Ereignis hinzu und bricht dann die Transaktion ab, um das Ereignis auszulösen.

js
// Open the database
const DBOpenRequest = window.indexedDB.open("toDoList", 4);

DBOpenRequest.onupgradeneeded = (event) => {
  const db = event.target.result;

  db.onerror = () => {
    console.log("Error creating database");
  };

  // Create an objectStore for this database
  const objectStore = db.createObjectStore("toDoList", {
    keyPath: "taskTitle",
  });

  // define what data items the objectStore will contain
  objectStore.createIndex("hours", "hours", { unique: false });
  objectStore.createIndex("minutes", "minutes", { unique: false });
  objectStore.createIndex("day", "day", { unique: false });
  objectStore.createIndex("month", "month", { unique: false });
  objectStore.createIndex("year", "year", { unique: false });
};

DBOpenRequest.onsuccess = (event) => {
  const db = DBOpenRequest.result;

  // open a read/write db transaction, ready for adding the data
  const transaction = db.transaction(["toDoList"], "readwrite");

  // add a listener for `abort`
  transaction.addEventListener("abort", () => {
    console.log("Transaction was aborted");
  });

  // abort the transaction
  transaction.abort();
};

Dasselbe Beispiel, aber der Ereignishandler wird der onabort-Eigenschaft zugewiesen:

js
// Open the database
const DBOpenRequest = window.indexedDB.open("toDoList", 4);

DBOpenRequest.onupgradeneeded = (event) => {
  const db = event.target.result;

  db.onerror = () => {
    console.log("Error creating database");
  };

  // Create an objectStore for this database
  const objectStore = db.createObjectStore("toDoList", {
    keyPath: "taskTitle",
  });

  // define what data items the objectStore will contain
  objectStore.createIndex("hours", "hours", { unique: false });
  objectStore.createIndex("minutes", "minutes", { unique: false });
  objectStore.createIndex("day", "day", { unique: false });
  objectStore.createIndex("month", "month", { unique: false });
  objectStore.createIndex("year", "year", { unique: false });
};

DBOpenRequest.onsuccess = (event) => {
  const db = DBOpenRequest.result;

  // open a read/write db transaction, ready for adding the data
  const transaction = db.transaction(["toDoList"], "readwrite");

  // add a listener for `abort`
  transaction.onabort = (event) => {
    console.log("Transaction was aborted");
  };

  // abort the transaction
  transaction.abort();
};

Spezifikationen

Specification
Indexed Database API 3.0
# eventdef-idbtransaction-abort

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
abort event

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Siehe auch