Jes*_*pez 5 c# sqlite multithreading system.data.sqlite
我在多线程 Windows 桌面程序中使用 SQLite (System.Data.SQLite.dll)。我使用这个代码:
using (var cn = new SQLiteConnection(connectionString))
{
cn.Open();
using (var tx = cn.BeginTransaction())
{
// do some work here
tx.Commit();
}
}
Run Code Online (Sandbox Code Playgroud)
我正在强调使用 8 个并发线程同时写入数据库的程序。有时我会在 Visual Studio 输出窗口中看到此消息:
SQLite error (517): statement aborts at 1: [BEGIN IMMEDIATE] database is locked
Run Code Online (Sandbox Code Playgroud)
但是根本没有抛出异常,一切都按预期进行。所以我猜 SQLConnection.BeginTrasaction() 在收到 SQLite 错误 (517) 数据库被锁定时重试。
我对吗?
如果是,则 SQLConnection.BeginTransaction 在抛出异常之前重试多少次。这个可以配置吗?
这是由BUSY TIMEOUT处理的:
PRAGMA busy_timeout;
PRAGMA busy_timeout = 毫秒;查询或修改忙超时设置。此编译指示是 sqlite3_busy_timeout() C 语言接口的替代方案,它可作为编译指示与不提供直接访问 sqlite3_busy_timeout() 的语言绑定一起使用。
每个数据库连接只能有一个繁忙处理程序。此 PRAGMA 设置进程的繁忙处理程序,可能会覆盖任何先前设置的繁忙处理程序。
超时间隔默认定义为 30 秒SQLite.dll。事实证明你无法改变这一点,只能通过调用不安全的 C 函数。
int sqlite3_busy_timeout(sqlite3*, int 毫秒);
使用小于或等于零的参数调用此例程将关闭所有繁忙的处理程序。
| 归档时间: |
|
| 查看次数: |
2869 次 |
| 最近记录: |