5.2. Closing a database

The steps to close a database connection are as follows. These steps take two arguments, a connection object, and an optional forced flag.

  1. Set the close pending flag of connection.

  2. If the forced flag is set, then for each transaction created using connection run the steps to abort a transaction with transaction and newly created[AbortError](https://www.w3.org/TR/WebIDL-1/#aborterror)[DOMException](https://www.w3.org/TR/WebIDL-1/#idl-DOMException).

  3. Wait for all transactions created using connection to complete. Once they are complete, connection is closed.

  4. If the forced flag is set, then fire an event named close at connection.

    The “close“ event only fires if the connection closes abnormally, e.g. if the origin’s storage is cleared, or there is corruption or an I/O error. If [close()](#dom-idbdatabase-close) is called explicitly the event does not fire.

    🚧 This behavior is new in this edition. It is supported in Chrome 31, Firefox 50, and Safari 10.1. 🚧

Once the close pending flag has been set no new transactions can be created using connection. All methods that create transactions first check the close pending flag first and throw an exception if it is set.

Once the connection is closed, this can unblock the steps to run an upgrade transaction, and the steps to delete a database, which both wait for connections to a given database to be closed before continuing.