我可以为当前线程锁定 SQLite 表吗?

发布于 2024-12-13 04:37:34 字数 329 浏览 2 评论 0原文

我的应用程序管理 SQLiteDatabase 中的所有数据,该数据库由多个线程访问。

现在,我一直在数据库本身上保持所有数据库调用同步。

我想要这样做的原因是,有时我想通过从服务器获取最新版本并从 stratch 重建表来刷新表。为了节省时间,我实际上正在制作第二个表,然后在完成后替换原始表(在执行此操作时用同步将其锁定)。

问题是,我的 SQL 调用偶尔会停滞很长一段时间(由于同步锁),或者当 SQL 调用尝试在我的一个表被复制的短时间内自行运行时,我会收到错误。

我是否可以在刷新时锁定数​​据库以防止其他操作,但让操作 A、B、C 等同时运行?

干杯!

My application manages all its data in a a SQLiteDatabase that is accessed by a number of threads.

Right now I've been keeping all my DB calls synchronized on the database itself.

The reason I want to do this is that occasionally I want to refresh the table by grabbing the most recent version from a server and reconstructing the table from stratch. To save time I'm actually making a second table and then replacing the original when I'm done (locking it out with a synchronized while I'm doing this).

The problem is either I occasionally have my SQL calls stall for a long while (due to the synchronous locks) or I get an error when a SQL call tries to run itself in the brief period when my one table is getting copied over.

Is there a was for me to lock my database from other operations while it refreshes but let operations A,B,C,etc run concurrently?

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不语却知心 2024-12-20 04:37:34

我很确定你想要的是 读写锁。基本上,它在数据库上放置两种不同的锁,一种用于读取,一种用于写入。当写锁被锁定时,其他人都无法读取或写入。如果任何读锁被锁定(被任意数量的线程锁定),则写锁将无法锁定,直到每个人都完成读取。

I'm pretty sure what you want is a ReadWriteLock. Basically, it puts two different locks on your database, one for reading and one for writing. While the write lock is locked, nobody else can read or write. If any of the read lock is locked (by any number of threads), the write lock won't be able to lock until everyone is finished reading.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文