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

时间:2017-04-10 02:08:36

标签: dexie

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?

1 个答案:

答案 0 :(得分:1)

刷新页面时,浏览器将自动关闭数据库连接。这就是为什么当页面关闭时你不需要明确地关闭连接的原因。如果您使用的是临时Dexie实例,那么当您不再使用它时,关闭是明智的。

您的ECONNRESET错误可能发生在websocket连接上。示例WebSocketSyncProtocol将此错误转发给Dexie.Syncable,将其视为临时网络关闭事件,并通知它再次尝试重新连接。当网暂时中断时,也可能发生此错误。没有问题,因为这会在正常情况下发生,包括页面关闭。尽管Dexie.Syncable将在几秒钟后安排尝试重新连接,但这种重新连接将永远不会发生,因为页面正在关闭并重新加载。

我只是建议默默忽略这个特定的错误。不知道你是作为未处理的拒绝还是通过dexie.Syncable的statusChanged事件收到它,但是这些类型的临时错误可以安全地被用户忽略,因为它们是正常错误。