sqlite - 如何处理访问同一数据库的两个单独线程之间的潜在争用?
问题:如何处理访问同一 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 sqlite 中,您可以配置在遇到
SQLITE_BUSY
或SQLITE_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
orSQLITE_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.