SQLITE_OPEN_NOMUTEX 改为 SQLITE_OPEN_FULLMUTEX模式,确保外部锁&内部操作的原子性#172
Closed
hsjcom wants to merge 1 commit into
Closed
SQLITE_OPEN_NOMUTEX 改为 SQLITE_OPEN_FULLMUTEX模式,确保外部锁&内部操作的原子性#172hsjcom wants to merge 1 commit into
hsjcom wants to merge 1 commit into
Conversation
Owner
|
不应该直接修改默认行为, 我在 2.8.0 版本中开放了 dbOpenFlags 属性, 可以基于 onCreateWithLKDBHelper 回调,自行修改对应配置 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
一直有用户出现11 - database disk image is malformed 、14 - errorMessage:unable to open database file 等数据库损坏问题
#define LKDBOpenFlags (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_PRIVATECACHE | SQLITE_OPEN_FILEPROTECTION_NONE)这里用的是
SQLITE_OPEN_NOMUTEX,禁用了 SQLite 内部的互斥锁。是否可以把SQLITE_OPEN_NOMUTEX改为SQLITE_OPEN_FULLMUTEX?因为业务的关系,会用到不同 GCD 对同一个 helper 实例的访问。虽然LKDBHelper 内部用 NSRecursiveLock 保护 executeDB: 方法,但这个锁只能防止同一线程的重入,不能有效协调不同 GCD 队列之间的并发访问。多个队列同时调用 executeDB: 时,锁虽然能串行化,但在 SQLITE_OPEN_NOMUTEX 下,FMDatabaseQueue 内部的 SQLite 连接本身没有互斥保护,仍然可能出问题。比如切换用户登录数据库切换,closeDB / openDB 之间的同步机制。OC 层的锁可能只保护了 SQLite 的调用,但没保护SQLite 内部行为,可能外部已经 closeDB,内部还在回写。改为
SQLITE_OPEN_NOMUTEX可以确保内部操作的原子性,性能影响较小。