Database Open/Close Frequency

时光总嘲笑我的痴心妄想 提交于 2019-12-12 04:28:41

问题


I am working on a chat application and in chat we are getting lots of update from server. And we are also saving the updates into the local SQLite database file.

Can someone suggest me, after every single insertion/update, should we close the database or we should close the database when it is actually needed ?


回答1:


Closing the connection throws away the page cache, and requires that the schema version is checked and the entire schema is re-parsed the next time it is opened.

In most apps, there are not enough database accesses so that the overhead of continually re-opening the database would actually become noticeable. But this is no reason to add useless code to your app.

Please note that the SQLiteDatabase object is reference counted. So if you are using a global open helper instance, you can keep the DB open with an extra getWritableDatabase() call, even when all your other code calls close().




回答2:


You should close the connection after each query. In .net (and in most other frameworks), SQLConnections are stored in the background anyway. It won´t hit your runtime.

Also: connection pooling is your friend.



来源:https://stackoverflow.com/questions/40828442/database-open-close-frequency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!