Search code examples
dexie

Do you need to explicitly close your db when closing the browser especially when using syncable?


I am using Dexie.Syncable with the examples WebSocketSyncServer.js and WebSocketSyncProtocol.js from the docs page setup as Server and Client, however I am getting "ECONNRESET" on the server if I refresh the page.

If I explicitly close the db using db.close(); or disconnect using db.syncable.disconnect(url); I no longer get the "ECONNRESET". This makes sense to me but in your docs on db.close() you say that you don't really need to think about calling this function.

Should I just use db.syncable.disconnect(url) and not worry about db.close()

Even if you are not using db.syncable why wouldn't you want to close the db when the user closes/refreshes browser?


Solution

  • The browser will close db connections by itself when you refresh the page. That's why you don't need to close the connection explicitely when page is shutting down. If you are using a temporary Dexie instance, it is wise to close when you won't be using it anymore though.

    Your ECONNRESET error probably happens on the websocket connection. The sample WebSocketSyncProtocol forwards this error to Dexie.Syncable, treating it as a temporary network down event and informs it to try reconnect again. This error could also happen when net goes down temporarily. There's no issue, as this will happen in normal situations, including a page shutdown. Even though Dexie.Syncable will schedule a try to reconnect after a few seconds, this reconnect will in practive never happen as the page is shutting down and being reloaded.

    I would just recommend to silently ignore this specific error. Don't know if you receive it as an unhandled rejection or throught the statusChanged event for Dexie.Syncable, but these types of temporary errors can safely be ignored from the user, as they are normal errors.