0

我正在开发一个 VB.NET 应用程序。根据应用程序的性质,一个模块必须每秒监控数据库(SQLite DB)。此监控是通过简单的 select 语句完成的,该语句运行以检查数据是否符合某些条件。其他模块在同一 SQLite DB 上执行选择、插入和更新语句。关于 SQLite 并发选择语句工作正常,但我很难在这里找出,为什么它不允许插入和更新。我知道这是一个基于文件的锁,但有没有办法完成它?每个模块,实际上语句打开和关闭与 DB 的连接。我已经通过 GUI 设计限制用户一次运​​行单个语句。

任何帮助将不胜感激。

4

2 回答 2

0

You can use some locking mechanism to make sure the database works in a multithreading situation. Since your application is a read intensive one according to what you said, you can consider using a ReaderWriterLock or ReaderWriterLockSlim. (refer to here and here for more details)

If you have only one database, then creating just one instance of the lock is OK; if you have more than one database, each of them can be assigned a lock. Every time you do some read or write, enter the lock (by EnterReadLock() for ReaderWriterLockSlim, or by AcquireReaderLock() for ReaderWriterLock) before you do something, and after you're done exit the lock. Note that you can place the exit of the lock in a finally clause lest you forget to release it.

The strategy above is being used in our production applications. It's not so good as to use a single thread in your case because you have to take performance into account.

于 2013-07-22T19:43:12.480 回答
0

如果您的数据库文件不在网络上,您可以通过启用WAL 模式来允许一定数量的读/写并发。

但也许您应该只使用一个连接并为所有数据库访问进行自己的同步。

于 2013-04-21T09:49:08.987 回答