尝试使用带有 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
with 消息
数据库文件被锁定
数据库被锁定
堆栈跟踪是:
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 数据提供程序可能会解决其中一些问题。 .