3

I have recently converted my workspace file format for my application to sqlite. In order to ensure robust operation on NFS I've used a common update policy, I do all modifications to a copy stored in a temp location on the local harddisk. Only when saving do I modify the original file (potentially on NFS) by copying over the original file with the temp file. I only open the orginal file to keep an exclusive lock on it so it someone else tries to open they will be warned that someone else is using it.

The problem is this: When I go to save my temp file back over the original file I must release the lock on the orginal file, this provides a window for someone else to get in and take the original, albeit a small window.

I can think of a few ways around this:

(1) being to simply dump the contents of the temp in to the orginal by using sql, i.e. drop tables on original, vacumm original, select from temp and insert into orginal. I don't like doing sql operations on a sqlite file stored on NFS though. This scares me with corruptions issues. Am I right to think like this?

(2) Use various extra files to act as a guard to prevent other from coming in while copying the temp over the original. Using files as a mutex is problematic at best. I also don't like the idea of having extra files hanging around if the application crashes.

I'm wondering if anyone has any different solutions for this. Again to copy the temp file over the original file while ensuring other application don't sneak in and grab the original file while doing so?

I'm using python2.5, sqlalchemy 0.6.6 and sqlite 3.6.20

Thanks, Dean

4

1 回答 1

2

SQLite NFS 问题是由于缓存和锁定损坏造成的。如果您的进程是唯一一个访问 NFS 上的文件的进程,那么您就可以了。

SQLite 备份 API 旨在准确解决您的问题。您可以直接备份到 NFS 数据库或另一个本地临时文件,然后将其复制。备份 API 处理所有锁定和并发问题。

您可以使用 APSW 访问备份 API 或最新版本的 pysqlite。(披露:我是 APSW 的作者。)

于 2011-02-23T18:59:03.467 回答