什么时候关闭 android 上的数据库连接?每次操作完成后或应用退出后 [英] When to close db connection on android? Every time after your operation finished or after your app exit

查看:22
本文介绍了什么时候关闭 android 上的数据库连接?每次操作完成后或应用退出后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个经常访问本地 sqlite3 db 的 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 上的数据库连接?每次操作完成后或应用退出后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆