文档章节

sqlite

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

暂无文章

Spring之IOC和AOP学习摘要

一、IOC(Inversion of Control)或者依赖注入(Dependency Injection) 1、底层实现原理:反射 2、三大核心接口: BeanFactory:简单容器系列,只是实现了容器最基本的功能。 ApplicationC...

狠一点
24分钟前
4
0
缓存架构SpringBoot集成Curator实现zookeeper分布式锁

一、分布式锁简介 1、什么是锁 在单机环境下,当存在多个线程可以同时改变某个共享变量时,就需要同步来实现该功能,使其线程安全。 而同步就是通过锁来实现的。锁保证了同一时刻只有一个线程...

架构师springboot
26分钟前
1
0
11《Java核心技术》之Java提供了哪些IO方式? NIO如何实现多路复用?

一、提出问题 IO 一直是软件开发中的核心部分之一,伴随着海量数据增长和分布式系统的发展,IO 扩展能力愈发重要。幸运的是,Java 平台 IO 机制经过不断完善,虽然在某些方面仍有不足,但已经...

飞鱼说编程
33分钟前
4
0
简单介绍Java 的JAR包、EAR包、WAR包区别

WAR包 WAR(Web Archive file)网络应用程序文件,是与平台无关的文件格式,它允许将许多文件组合成一个压缩文件。War专用于Web方面。大部分的JAVA WEB工程,都是打成WAR包进行发布的。 War是...

linuxprobe16
33分钟前
1
0
55:Mysql用户管理|常用sql语句|mysql数据库备份恢复

1、Mysql用户管理; 场景,为了安全,新建的站点,创建新的用户,或者给已有用户授权,对某个库或者某个表有权限; 语法: grant all on *.* to 'user'@'127.0.0.1' identified by 'password'; g...

芬野de博客
37分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部