文档章节

sqlite

g
 grasp_D
发布于 2017/04/26 18:06
字数 431
阅读 74
收藏 0

As with lots of things in computer programming, there is no single "correct proceed" for the timing of closing a SQLite database (via close() on SQLiteOpenHelper if you are using that, or close()on SQLiteDatabase otherwise).

The one fairly hard-and-fast rule is to not use multiple SQLiteDatabase objects for the same database across multiple threads at the same time. That can get you in trouble, unless you are doing your own thread synchronization. If you stick to one SQLiteDatabase object per database, thread synchronization is handled inside the SQLiteDatabase.

Because of the one-database-for-all-threads rule, and because in many cases tracking when threads may or may not want the database can be tricky when you have many different components, some developers take the approach of having a singleton database manager, which lazy-opens the database... and never closes it. For example, a database-backed ContentProvideruses this pattern, as a ContentProvider is never destroyed, so there is no clear time when to close the database.

Personally, I would go that route over the open-use-close approach, particularly if you are using SQLCipher for Android (an encrypted SQLite replacement). However, there is nothing especially wrong with the open-use-close approach if you are sure that only one thread at a time will be working with the database.

If only a single component uses the database, there is nothing wrong with triggering opening the database in onCreate() and closing it in onDestroy(). After all, if that is the only component that needs the database, you do not need the database when the component itself goes away.

Close your db in onDestroy() Comments: "OnDestroy() does not get called always! Use onStop()!" --> So onStart() has to open it.

If onDestroy() does not get called on a component, either you crashed with an unhandled exception in that component, or your process was terminated. The latter scenario is not a problem (again, this is how a ContentProvider works, since a provider is never destroyed). The former scenario should not be a problem, so long as you do not accidentally use that older SQLiteDatabase and some newer SQLiteDatabase at the same time across multiple threads.

The db works efficiently. No need to close it.

It is not a matter of efficiency. SQLite is transactional. Failing to close a database is not a problem, as there are no unflushed buffers or something that you have to worry about.

Open your db once and create a field and an accessor.

That has nothing directly to do with when you close a database.

Close after use.

That is a pattern, and done properly it can work.r

© 著作权归作者所有

共有 人打赏支持
g
粉丝 1
博文 126
码字总数 20266
作品 0
青岛
程序员
私信 提问

暂无文章

Jmeter参数的AES加密使用

在Jmeter日常实践中,大家应该都遇到过接口传参需要加密的情况。以登陆为例,用户名和密码一般都需要进行加密传输,在服务端再进行解密,这样安全系数会更高,但在使用jmeter进行接口测试的时...

程序猿拿Q
23分钟前
2
0
MYSQL 日期函数 Date and Time Functions

Table 12.13 Date and Time Functions Name Description ADDDATE() Add time values (intervals) to a date value ADDTIME() Add time CONVERT_TZ() Convert from one time zone to another ......

_liucui_
29分钟前
2
0
Android代码混淆ProGuard工作原理简介

ProGuard能够对Java类中的代码进行压缩(Shrink),优化(Optimize),混淆(Obfuscate),预检(Preveirfy)。    1. 压缩(Shrink): 在压缩处理这一步中,用于检测和删除没有使用的类,字段...

SuShine
31分钟前
1
0
Idea 2018激活

教程地址: https://www.52pojie.cn/thread-781394-1-1.html 亲测可用

一个不正经的程序员
37分钟前
1
0
Android组件化开发实践和案例分享

目录介绍 1.为什么要组件化 1.1 为什么要组件化 1.2 现阶段遇到的问题 2.组件化的概念 2.1 什么是组件化 2.2 区分模块化与组件化 2.3 组件化优势好处 2.4 区分组件化和插件化 2.5 applicatio...

潇湘剑雨
38分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部