Android SQLite是线程安全的吗?

来源:互联网 发布:数据库性能测试 编辑:程序博客网 时间:2024/05/16 09:49

本文转载自

SQLite数据库本身不具有线程安全性
Android SQLiteDatabase提供了线程安全的保证,里面添加了同步逻辑。
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#setLockingEnabled%28boolean%29
http://stackoverflow.com/questions/6675240/is-sqlite-database-instance-thread-safe

我们操作数据库一般使用ContentProvider,SQLiteOpenHelper,应用中使用SQLiteDatabase db = SQLiteOpenHelper.getReadableDatabase(),这个db需要调用db.close()或者openHelper.close()方法吗?

不需要。

为什么?
http://stackoverflow.com/questions/4547461/closing-the-database-in-a-contentprovider?lq=1
ContentProvider随它的宿主进程创建,与该进程有相同的生命周期,所以不需要手动去关闭,在内核清理进程资源的时候会将它一起清理,也就是这时候关闭的。

A content provider is created when its hosting process is created, and remains around for as long as the process does, so there is no need to close the database – it will get closed as part of the kernel cleaning up the process’s resources when the process is killed.

0 0