什么时候关闭android上的数据库连接?每次操作完成或应用退出后

When to close db connection on android? Every time after your operation finished or after your app exit(什么时候关闭android上的数据库连接?每次操作完成或应用退出后)
本文介绍了什么时候关闭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上的数据库连接?每次操作完成或应用退出后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)