gpt4 book ai didi

c# - SQLiteConnection.BeginTransaction 是否在数据库锁定时重试?

转载 作者:行者123 更新时间:2023-11-30 15:26:50 25 4
gpt4 key购买 nike

我在多线程 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();
}
}

我强调程序使用 8 个并发线程同时写入数据库。有时我会在 Visual Studio 输出窗口中看到这条消息:

SQLite error (517): statement aborts at 1: [BEGIN IMMEDIATE] database is locked 

但是根本没有抛出异常,一切都按预期进行。所以我猜 SQLConnection.BeginTrasaction() 在收到 SQLite 错误 (517) 数据库被锁定时会重试。

我说得对吗?

如果是,SQLConnection.BeginTransaction 在抛出异常之前重试了多少次。这是可配置的吗?

最佳答案

这由 BUSY TIMEOUT 处理:

PRAGMA busy_timeout;
PRAGMA busy_timeout = milliseconds;

Query or change the setting of the busy timeout. This pragma is analternative to the sqlite3_busy_timeout() C-language interface whichis made available as a pragma for use with language bindings that donot provide direct access to sqlite3_busy_timeout().

Each database connection can only have a single busy handler. ThisPRAGMA sets the busy handler for the process, possibly overwriting anypreviously set busy handler.

超时间隔在 SQLite.dll 中默认定义为 30 秒。原来你不能改变它,只能通过调用不安全的 C 函数。

来自SQLite C Interface :

int sqlite3_busy_timeout(sqlite3*, int ms);

Calling this routine with an argument less than or equal to zero turns off all busy handlers.

关于c# - SQLiteConnection.BeginTransaction 是否在数据库锁定时重试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28478358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com