文档章节

sqlite

g
 grasp_D
发布于 2017/04/26 18:06
字数 431
阅读 78
收藏 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
青岛
程序员
私信 提问

暂无文章

为什么?老程序员申请加薪至30K遭拒愤然辞职,公司转眼花35K招新

身在职场,经常会暗自打听同事工资,尤其是得知身边新入职同事的工资居然比自己高,还高出一大截时,心里自然很不平衡,一心想要离职。 那么,为什么公司宁愿花高价招聘新员工也不愿意给老员...

Java技术剑
14分钟前
5
0
云桌面到底是什么,企业该用云桌面吗

随着云计算和大数据时代的到来,当前企业里讨论最多的话题之一就是:企业到底要不要用云桌面这个话题的,有的人说企业应该使用云桌面的,因为可以降低使用成本和简化运维管理,同时也有的人说...

GZASD
19分钟前
3
0
技术分享 | gh-ost 原理剖析

作者简介: 杨奇龙,网名“北在南方”,7年DBA老兵,目前任职于杭州有赞科技DBA,主要负责数据库架构设计和运维平台开发工作,擅长数据库性能调优、故障诊断。 一、简介 上一篇文章(gh-ost ...

爱可生
22分钟前
5
0
手机短信删除了怎么恢复?几个方法就能恢复

  手机短信删除了怎么恢复?前几天有个小伙伴收到了一条来着面试的通知,这个面试对他很重要,但是可气的是刚好在清理手机里面的垃圾短信,然后收到了短信之后又被删除了,却又不知道该怎么...

科技第六人
37分钟前
5
0
浅谈Builder建造者模式

一、前言 Builder建造者模式和模板模式非常像,但是也有区别,模板模式中父类对子类中的实现进行操作,在父类之中进行一件事情的处理,但是在Builder模式之中,父类和子类都不用关心怎么处理...

青衣霓裳
41分钟前
7
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部