When to close db connection on android? Every time after your operation finished or after your app exit(什么时候关闭android上的数据库连接?每次操作完成或应用退出后)
问题描述
我有一个经常访问本地 sqlite3 数据库的 android 应用程序,出于性能考虑,所以我始终保持连接打开.但是我的一位朋友建议我每次操作都打开/关闭连接.
I have an android application that access local sqlite3 db frequently, for performance consideration so i always keep the connection open. But one of my friends recommended me to open/close the connection every operation.
1) 你们对这两种方法有什么看法?缺点/优点.2)我做了一些测试,发现数据库连接没有太多的性能开销.DB连接的性能开销是否因DB的大小而异?
1) What's your guys opinion on these two methods ? cons/pros. 2) I did some testing and find the DB connection doesn't have too much perf overhead. Does the performance overhead of DB connection varies on the size of the DB ?
推荐答案
我不知道频繁关闭/打开数据库(无论其大小)有什么性能损失.我认为这个问题的答案还取决于访问数据库的应用程序类型.
I don't know of any performance penalties in frequent closing/opening of the database (regardless of its size). I think the answer to this question also depends on what type of application is accessing the database.
您是否经常重新查询"数据库?
然后它似乎纠正了保持打开状态.
Do you "re-query" the database a lot?
Then it seems rectified to keep it open.
每次获取数据时是否获取不同的数据?
同样,将其保持打开状态似乎是合理的(因为您不会获得缓存数据的好处).
Do you fetch different data each time you fetch something?
Again, it seems reasonable to leave it open (as you won't gain in caching the data instead).
是否有其他应用程序访问同一数据库?
如果存在并发或阻塞问题的风险,在完成读取/写入数据库后关闭数据库可能是明智之举.
Are there any other applications accessing the same database?
If there is a risk for concurrency or blocking issues, it might be wise to close the database after finished reading/writing from/to it.
通常我会说,在优化性能时,缓存数据可能比保持数据库打开(相反关闭它)获得更多.
Generally I would say that you might gain more in caching data than in leaving the database open (contra closing it) when optimizing for performance.
这篇关于什么时候关闭android上的数据库连接?每次操作完成或应用退出后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么时候关闭android上的数据库连接?每次操作完成或应用退出后


基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01