5.2. Closing a database

To close a database connection with a connection object, and an optional forced flag, run these steps:

  1. Set connection’s close pending flag to true.

  2. If the forced flag is true, then for each transaction created using connection run abort a transaction with transaction and newly created[AbortError](https://heycam.github.io/webidl/#aborterror)[DOMException](https://heycam.github.io/webidl/#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 true, 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.

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

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.