在单线程上使用事务时 sqlite 和 SubSonic 的锁定问题

发布于 2024-08-22 06:10:27 字数 3804 浏览 6 评论 0原文

当尝试使用 SubSonic 和 SQLite 的事务时,我遇到锁定异常。我从单个线程使用它,并且没有其他进程访问我的数据库,所以我真的没想到会出现任何此类问题。

如果我编写如下代码,我会在循环内第二次调用 Save() 时遇到异常 - 因此第三次调用 Save() 时会出现异常。

       using (TransactionScope ts = new TransactionScope())
       {
            using (SharedDbConnectionScope sharedConnectinScope = new SharedDbConnectionScope())
            { 
                SomeDALObject x = new SomeDALObject()
                x.Property1 = "blah";
                x.Property2 = "blah blah";
                x.Save();

                foreach (KeyValuePair<string, string> attribute in attributes)
                { 
                    AnotherDALObject y = new AnotherDALObject()
                    y.Property1 = attribute.Key
                    y.Property2 = attribute.Value
               
                    y.Save();  // this is where the exception is raised, on the 2nd time through this loop
                }
            }
       }

如果我有如上所述的 using() 语句,或者如果我只有 using (TransactionScope ts = new TransactionScope()) 那么我会得到一个 System.Data.SQLite.SQLiteException 有留言

数据库文件被锁定

数据库已锁定

堆栈跟踪是:

   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()
   at System.Data.SQLite.SQLiteTransaction..ctor(SQLiteConnection connection, Boolean deferredLock)
   at System.Data.SQLite.SQLiteConnection.BeginDbTransaction(IsolationLevel isolationLevel)
   at System.Data.SQLite.SQLiteConnection.BeginTransaction()
   at System.Data.SQLite.SQLiteEnlistment..ctor(SQLiteConnection cnn, Transaction scope)
   at System.Data.SQLite.SQLiteConnection.EnlistTransaction(Transaction transaction)
   at System.Data.SQLite.SQLiteConnection.Open()
   at SubSonic.SQLiteDataProvider.CreateConnection(String newConnectionString)
   at SubSonic.SQLiteDataProvider.CreateConnection()
   at SubSonic.SQLiteDataProvider.ExecuteScalar(QueryCommand qry)
   at SubSonic.DataService.ExecuteScalar(QueryCommand cmd)
   at SubSonic.ActiveRecord`1.Save(String userName)
   at SubSonic.ActiveRecord`1.Save()
   at (my line of code above).

如果我以相反的方式嵌套了 using 语句,SharedDbConnectionScope 位于外部,那么我会收到一个带有消息“操作不是”的 TransactionException对交易状态有效。”堆栈跟踪是:

at System.Transactions.TransactionState.EnlistVolatile(InternalTransaction tx, IEnlistmentNotification enlistmentNotification, EnlistmentOptions enlistmentOptions, Transaction atomicTransaction)
   at System.Transactions.Transaction.EnlistVolatile(IEnlistmentNotification enlistmentNotification, EnlistmentOptions enlistmentOptions)
   at System.Data.SQLite.SQLiteEnlistment..ctor(SQLiteConnection cnn, Transaction scope)
   at System.Data.SQLite.SQLiteConnection.EnlistTransaction(Transaction transaction)
   at System.Data.SQLite.SQLiteConnection.Open()
   at SubSonic.SQLiteDataProvider.CreateConnection(String newConnectionString)
   at SubSonic.SQLiteDataProvider.CreateConnection()
   at SubSonic.SQLiteDataProvider.ExecuteScalar(QueryCommand qry)
   at SubSonic.DataService.ExecuteScalar(QueryCommand cmd)
   at SubSonic.ActiveRecord`1.Save(String userName)
   at SubSonic.ActiveRecord`1.Save()
   at (my line of code above)

内部异常是“事务超时”

我生成的 DAL 类中没有任何自定义代码,或者我能想到的任何其他聪明的东西会导致此问题。

其他人遇到过这样的交易问题,或者有人可以建议我从哪里开始寻找问题?

谢谢!

更新:我注意到版本 1.0.61-65 的发行说明中提到了与事务相关的内容(例如 此处),因此更新 SubSonic 以使用最新版本的 .Net 数据提供程序可能会解决其中一些问题...

I'm getting locking exceptions when trying to use transactions with SubSonic and SQLite. I'm using this from a single thread and there are no other processes accessing my db, so I really didn't expect any such problems.

If I write code like this below, I get an exception on the second call to Save() within the loop - so the third call to Save() over all.

       using (TransactionScope ts = new TransactionScope())
       {
            using (SharedDbConnectionScope sharedConnectinScope = new SharedDbConnectionScope())
            { 
                SomeDALObject x = new SomeDALObject()
                x.Property1 = "blah";
                x.Property2 = "blah blah";
                x.Save();

                foreach (KeyValuePair<string, string> attribute in attributes)
                { 
                    AnotherDALObject y = new AnotherDALObject()
                    y.Property1 = attribute.Key
                    y.Property2 = attribute.Value
               
                    y.Save();  // this is where the exception is raised, on the 2nd time through this loop
                }
            }
       }

If I have the using() statements as above, or if I just have using (TransactionScope ts = new TransactionScope()) then I get a System.Data.SQLite.SQLiteException with message

The database file is locked

database is locked

The stack trace is:

   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()
   at System.Data.SQLite.SQLiteTransaction..ctor(SQLiteConnection connection, Boolean deferredLock)
   at System.Data.SQLite.SQLiteConnection.BeginDbTransaction(IsolationLevel isolationLevel)
   at System.Data.SQLite.SQLiteConnection.BeginTransaction()
   at System.Data.SQLite.SQLiteEnlistment..ctor(SQLiteConnection cnn, Transaction scope)
   at System.Data.SQLite.SQLiteConnection.EnlistTransaction(Transaction transaction)
   at System.Data.SQLite.SQLiteConnection.Open()
   at SubSonic.SQLiteDataProvider.CreateConnection(String newConnectionString)
   at SubSonic.SQLiteDataProvider.CreateConnection()
   at SubSonic.SQLiteDataProvider.ExecuteScalar(QueryCommand qry)
   at SubSonic.DataService.ExecuteScalar(QueryCommand cmd)
   at SubSonic.ActiveRecord`1.Save(String userName)
   at SubSonic.ActiveRecord`1.Save()
   at (my line of code above).

If I have the using statments nested the other way around, with SharedDbConnectionScope on the outside, then I get a TransactionException with message "The operation is not valid for the state of the transaction." Stack trace is:

at System.Transactions.TransactionState.EnlistVolatile(InternalTransaction tx, IEnlistmentNotification enlistmentNotification, EnlistmentOptions enlistmentOptions, Transaction atomicTransaction)
   at System.Transactions.Transaction.EnlistVolatile(IEnlistmentNotification enlistmentNotification, EnlistmentOptions enlistmentOptions)
   at System.Data.SQLite.SQLiteEnlistment..ctor(SQLiteConnection cnn, Transaction scope)
   at System.Data.SQLite.SQLiteConnection.EnlistTransaction(Transaction transaction)
   at System.Data.SQLite.SQLiteConnection.Open()
   at SubSonic.SQLiteDataProvider.CreateConnection(String newConnectionString)
   at SubSonic.SQLiteDataProvider.CreateConnection()
   at SubSonic.SQLiteDataProvider.ExecuteScalar(QueryCommand qry)
   at SubSonic.DataService.ExecuteScalar(QueryCommand cmd)
   at SubSonic.ActiveRecord`1.Save(String userName)
   at SubSonic.ActiveRecord`1.Save()
   at (my line of code above)

and the inner exception is "Transaction Timeout"

I don't have any custom code in my generated DAL classes, or anything else clever that I can think of that would be causing this.

Anyone else encountered transaction problems like this or can someone suggest where I start looking for the problem?

thanks!

UPDATE: I notice mention of transaction-related things in the release notes for versions 1.0.61-65 (e.g. here), so perhaps updating SubSonic to work with the latest version of the .Net Data Provider would solve some of these issues...

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

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

发布评论

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

评论(3

長街聽風 2024-08-29 06:10:27

在为 subsonic 2.x 修改 sqlite 提供程序时,我基于现有的 subsonic sqlserver 测试创建了一整套单元测试。 (这些测试也通过修订后的代码进行了检查。)唯一失败的测试是与事务相关的测试(也许还有迁移测试)。正如您所看到的,“数据库文件已锁定”错误消息。 Subsonic 主要是为 sql server 编写的,它不像 SQLite 那样进行文件级锁定,因此有些功能不起作用;需要重写才能更好地处理这个问题。

我从来没有像你一样使用过 TransactionScope。我像这样进行亚音速 2.2 事务,到目前为止 SQLite 提供程序没有出现任何问题。我可以确认,如果处理多行,您需要使用 SQLite 事务,否则速度非常慢。

public void DeleteStuff(List<Stuff> piaRemoves)
{
    QueryCommandCollection qcc = new QueryCommandCollection();

    foreach(Stuff item in piaRemoves)
    {
        Query qry1 = new Query(Stuff.Schema);
        qry1.QueryType = QueryType.Delete;
        qry1.AddWhere(Stuff.Columns.ItemID, item.ItemID);
        qry1.AddWhere(Stuff.Columns.ColumnID, item.ColumnID);
        qry1.AddWhere(Stuff.Columns.ParentID, item.ParentID);
        QueryCommand cmd = qry1.BuildDeleteCommand();
        qcc.Add(cmd);
    }
    DataService.ExecuteTransaction(qcc);
}

In doing the revised sqlite provider for subsonic 2.x I created a full set of unit tests based on the existing subsonic sqlserver tests. (These tests were also checked in with the revised code.) The only tests that failed were the ones related to transactions (maybe the migration ones too). "The database file is locked" error message, as you have seen. Subsonic was written mainly for sql server that doesn't do file level locking like SQLIte does, so some things don't work; it would need to be rewritten to handle this better.

I have never used the TransactionScope as you have. I do my subsonic 2.2 transactions like this, and so far no problems with the SQLite provider. I can confirm that you need to use transactions with SQLite if dealing with multiple rows or it's really slow.

public void DeleteStuff(List<Stuff> piaRemoves)
{
    QueryCommandCollection qcc = new QueryCommandCollection();

    foreach(Stuff item in piaRemoves)
    {
        Query qry1 = new Query(Stuff.Schema);
        qry1.QueryType = QueryType.Delete;
        qry1.AddWhere(Stuff.Columns.ItemID, item.ItemID);
        qry1.AddWhere(Stuff.Columns.ColumnID, item.ColumnID);
        qry1.AddWhere(Stuff.Columns.ParentID, item.ParentID);
        QueryCommand cmd = qry1.BuildDeleteCommand();
        qcc.Add(cmd);
    }
    DataService.ExecuteTransaction(qcc);
}
探春 2024-08-29 06:10:27

我最终使用了 Paul 的建议,并将我的代码重写为如下所示:

    QueryCommandCollection qcc = new QueryCommandCollection();

    SomeDALObject x = new SomeDALObject()
    x.Property1 = "blah";
    x.Property2 = "blah blah";
    qcc.Add(x.GetSaveCommand());

    foreach (KeyValuePair<string, string> attribute in attributes)
    { 
        AnotherDALObject y = new AnotherDALObject()
        y.Property1 = attribute.Key
        y.Property2 = attribute.Value

        qcc.Add(y.GetSaveCommand());
    }

    DataService.ExecuteTransaction(qcc);

这实际上要好得多,因为数据库命中的所有准备工作都是在事务打开之前完成的,因此事务打开的时间会少得多。

如果您需要取回自动生成的 ID 以便对子记录运行 INSERT,则此方法效果不太好;您需要为此使用不同的方法。

然后我遇到了一些其他线程/事务问题:当我有多个线程同时执行 DataService.ExecuteTransaction() 时,我会得到 AccessViolationExceptions 和 NullReferenceExceptions,基本上有点混乱。但更改为使用 Paul 的 SubSonic 分支 以及更新的 SQLDataProvider 并更改为使用 < a href="http://sourceforge.net/projects/sqlite-dotnet2/" rel="nofollow noreferrer">System.Data.SQLite v1.0.65.0 似乎立即修复了它。万岁!

更新:实际上,我在使用 SubSonic 和 sqlite 时仍然遇到线程问题。基本上 SubSonic 中的 SQLiteDataProvider 并不是为处理多线程而编写的。更多内容即将推出...

I ended up using Paul's suggestion and rewrote my code to something like this:

    QueryCommandCollection qcc = new QueryCommandCollection();

    SomeDALObject x = new SomeDALObject()
    x.Property1 = "blah";
    x.Property2 = "blah blah";
    qcc.Add(x.GetSaveCommand());

    foreach (KeyValuePair<string, string> attribute in attributes)
    { 
        AnotherDALObject y = new AnotherDALObject()
        y.Property1 = attribute.Key
        y.Property2 = attribute.Value

        qcc.Add(y.GetSaveCommand());
    }

    DataService.ExecuteTransaction(qcc);

This is in fact much better since all preparation for the database hits are done before the transaction is opened, therefore the transaction will be open for much less time.

This won't work so well if you need to get back auto-generated IDs in order to run INSERTs for child records; you'll need to use a different approach for that.

I then hit some other threading/transaction problems: when I had multiple threads executing DataService.ExecuteTransaction() at the same time I would get AccessViolationExceptions and NullReferenceExceptions, basically a bit of a mess. But changing to use Paul's fork of SubSonic with the updated SQLDataProvider and also changing to use System.Data.SQLite v1.0.65.0 seems to instantly fixed it. Hooray!

UPDATE: Actually I'm still encountering threading problems using SubSonic with sqlite. Basically the SQLiteDataProvider in SubSonic isn't written to deal with multithreading. More to come...

辞旧 2024-08-29 06:10:27

我们使用 SQL lite 来测试与数据库操作相关的代码。
SQL lite 不支持嵌套事务。
我们在 NHibernate 和 .Net 事务中也遇到了类似的问题。
最终我们不得不使用 SQL Express 来测试数据库相关代码。

We are using SQL lite to test are code related to database actions.
SQL lite does not support nested Transaction.
We had similar issues where we had NHibernate and .Net transaction.
Ultimately we had to settle down using SQL Express to test the database related code.

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