sqlite - 如何处理访问同一数据库的两个单独线程之间的潜在争用?

发布于 2024-08-25 15:21:39 字数 308 浏览 1 评论 0原文

问题:如何处理访问同一 Sqlite 数据库的两个单独线程之间的潜在争用?

背景:我有一个通过 ADO.net 使用 Sqlite 的 C# Winforms 应用程序。我在 winforms 应用程序中有一个后台工作线程。我注意到,当主线程和后台工作线程尝试更新 sqlite 数据库时,即调用 DBDataAdaptor.Update() 时,我会遇到异常。

所以我对检查我的代码的内容感兴趣(尽管主线程代码和/或在后台工作程序中运行的代码)来优雅地处理这个问题,或者通过预先检查的方式,或者阻塞直到确定,或者引发一个我可以捕获的特定错误...

谢谢

QUESTION: How can I handle potential contention between two separate threads accessing the same Sqlite database?

BACKGROUND: I have a C# Winforms application that uses Sqlite via ADO.net. I do have a backgroundworker thread in the winforms application. I have noticed that I can get an exception when both the main thread, and the background worker thread, attempt to update the sqlite database, i.e. calling a DBDataAdaptor.Update()..

So I'm interested in what checks my code (albeit main thread code &/or code that runs in backgroundworker) to handle this gracefully, either via a way to check beforehand, or blocking until OK, or raising a specific error I can catch...

thanks

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

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

发布评论

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

评论(1

鲜血染红嫁衣 2024-09-01 15:21:40

在 sqlite 中,您可以配置在遇到 SQLITE_BUSYSQLITE_IOERR_BLOCKED 错误时触发的回调(请参阅 http://www.sqlite.org/c3ref/busy_handler.html)。

由于大多数时候您想要做的就是休眠并在繁忙时重试查询,因此 sqlite 有一个内置的回调可以做到这一点;阅读 http://www.sqlite.org/ 上的 sqlite3_busy_timeout c3ref/busy_timeout.html

In sqlite you can configure a callback that is triggered when you encounter a SQLITE_BUSY or SQLITE_IOERR_BLOCKED error (see http://www.sqlite.org/c3ref/busy_handler.html).

As most of the time what you'd want to do is sleep and retry the query on busy, sqlite has a built in callback that does exactly that; read up on sqlite3_busy_timeout at http://www.sqlite.org/c3ref/busy_timeout.html.

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