sqlite多进程调用锁的问题

qiuheqing 2012-06-08 03:20:27
遇到了如下问题:
有2个进程同时操作一个sqlite数据库文件data.db;其中A进程的操作如下
sqlite3 * sqlitdb;
1.open 数据库文件db,默认打开。
2.执行其中的sqlite语句。这个语句数据集很大需要遍历很久。

这个进程都是读取数据的。

另一个进程的在A进程遍历数据集合的时候。
1.打开数据库。
2.起事务
3.update数据 这个表不是A进程执行的表。
4.提交事务 //这个时候会爆出提交失败,失败的原因是数据库被锁定了

问题是:即使我按照下面文档说的持续等待搜索。直到超时。。还是会失败。请问大家有什么建议没有。

这个是sqlite中关于多进程调用的阐述:
Can multiple applications or multiple instances of the same application access a single database file at the same time?

Multiple processes can have the same database open at the same time. Multiple processes can be doing a SELECT at the same time. But only one process can be making changes to the database at any moment in time, however.

SQLite uses reader/writer locks to control access to the database. (Under Win95/98/ME which lacks support for reader/writer locks, a probabilistic simulation is used instead.) But use caution: this locking mechanism might not work correctly if the database file is kept on an NFS filesystem. This is because fcntl() file locking is broken on many NFS implementations. You should avoid putting SQLite database files on NFS if multiple processes might try to access the file at the same time. On Windows, Microsoft's documentation says that locking may not work under FAT filesystems if you are not running the Share.exe daemon. People who have a lot of experience with Windows tell me that file locking of network files is very buggy and is not dependable. If what they say is true, sharing an SQLite database between two or more Windows machines might cause unexpected problems.

We are aware of no other embedded SQL database engine that supports as much concurrency as SQLite. SQLite allows multiple processes to have the database file open at once, and for multiple processes to read the database at once. When any process wants to write, it must lock the entire database file for the duration of its update. But that normally only takes a few milliseconds. Other processes just wait on the writer to finish then continue about their business. Other embedded SQL database engines typically only allow a single process to connect to the database at once.

However, client/server database engines (such as PostgreSQL, MySQL, or Oracle) usually support a higher level of concurrency and allow multiple processes to be writing to the same database at the same time. This is possible in a client/server database because there is always a single well-controlled server process available to coordinate access. If your application has a need for a lot of concurrency, then you should consider using a client/server database. But experience suggests that most applications need much less concurrency than their designers imagine.

When SQLite tries to access a file that is locked by another process, the default behavior is to return SQLITE_BUSY. You can adjust this behavior from C code using the sqlite3_busy_handler() or sqlite3_busy_timeout() API functions.


...全文
870 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2012-06-08
  • 打赏
  • 举报
回复
SQLITE是文件级数据库,必然会导致这种并发问题。只能自己在程序中控制。
chinaye1 2012-06-08
  • 打赏
  • 举报
回复
sqlite不支持并发吧,事物前加 BEGIN EXCLUSIVE;

2,209

社区成员

发帖
与我相关
我的任务
社区描述
其他数据库开发 其他数据库
社区管理员
  • 其他数据库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧