c# - 池化的只读 SQLite 连接用于读写访问

标签 c# .net sqlite sqlite-net

使用的 nuget 包:System.Data.SQLite.Core,1.0.98.1

问题:在我的程序中,我在某些情况下使用启用了池和只读访问的 SQLite。通常它工作正常,但如果对数据库有大量混合的只读/读写密集型请求,则程序会失败并出现以下异常:

Unhandled Exception: System.Data.SQLite.SQLiteException: attempt to write a readonly database
attempt to write a readonly database
at System.Data.SQLite.SQLite3.Reset(SQLiteStatement stmt)
at System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt)
at System.Data.SQLite.SQLiteDataReader.NextResult()
at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(CommandBehavior behavior)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()

如果我禁用池化,那么程序可以正常工作。我假设以某种方式合并的只读连接用于读写连接。我是否遗漏了什么 - 即它是否是预期的行为?

要重现的最少代码(在 INSERT 或 DELETE 时失败)。如果我引入延迟,例如 Thread.Sleep(10000),它会正常工作。如果我删除循环,它也可以正常工作。

const string DbFilePath = "test.sqlite";
string readOnlyConnectionString = new SQLiteConnectionStringBuilder
    {
        DataSource = DbFilePath,
        Pooling = true,
        ReadOnly = true
    }.ConnectionString; // data source=test.sqlite;pooling=True;read only=True
string readWriteConnectionString = new SQLiteConnectionStringBuilder
    {
        DataSource = DbFilePath,
        Pooling = true,
        ReadOnly = false
    }.ConnectionString; // data source=test.sqlite;pooling=True;read only=False
File.Delete(DbFilePath);
using (SQLiteConnection conn = new SQLiteConnection(readWriteConnectionString))
using (SQLiteCommand cmd = new SQLiteCommand("CREATE TABLE items(id INTEGER NOT NULL PRIMARY KEY)", conn))
{
    conn.Open();
    cmd.ExecuteNonQuery();
}
while (true) // <= if we comment the loop, the program executes without error
{
    using (SQLiteConnection conn = new SQLiteConnection(readWriteConnectionString))
    using (SQLiteCommand cmd = new SQLiteCommand("INSERT INTO items(id) VALUES (1)", conn))
    {
        conn.Open();
        cmd.ExecuteNonQuery();
    }
    using (SQLiteConnection conn = new SQLiteConnection(readOnlyConnectionString))
    using (SQLiteCommand cmd = new SQLiteCommand("SELECT COUNT(*) FROM items", conn))
    {
        conn.Open();
        cmd.ExecuteScalar();
    }
    using (SQLiteConnection conn = new SQLiteConnection(readWriteConnectionString))
    using (SQLiteCommand cmd = new SQLiteCommand("DELETE FROM items", conn))
    {
        conn.Open();
        cmd.ExecuteNonQuery();
    }
}

最佳答案

来自 SQLiteConnectionPool 的源代码class 看起来它在管理池条目时只考虑文件名。

这是一个简短的摘录:

internal static void Add(
    string fileName,
    SQLiteConnectionHandle handle,
    int version

没有提到使用的连接字符串,只有文件名。因此,当您需要不同的连接字符串表现不同并且单独池化时,使用内置的连接池机制是行不通的。


现在,作为概念的“连接池”是 ADO.NET 类层次结构的实现者自己决定的东西。 SqlConnection 类池每个唯一的连接字符串,但这绝不是 IDbConnection 的实现所必需的。

因此,这可能只是 System.Data.SQLite 的创建者做出的设计决定。但是,我会向他们发送电子邮件,询问这是否是故意的。

关于c# - 池化的只读 SQLite 连接用于读写访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33231720/

相关文章:

c# - 如何替换 TreeView 中的 TreeNode?

java - Android 数据库到数组

database - 易于使用/嵌入二进制安全的键/值数据库

c# - c#中的托管指针数组

c# - 运行选择查询时,Dapper 以什么顺序填充类成员?

c# - 使构建前和构建后的事件脚本漂亮吗?

javascript - ASP.NET 隐藏字段在回发后不更新

c# - 如何在 C# 中覆盖 default(T)?

cakephp - 将 Sqlite3 与 CakePHP 结合使用

c# - WiX 工具集 PermissionEx 问题 - 安装后应用程序无法运行