当尝试将 SQLite 与 System.Transactions TransactionScope 与身份生成器作为增量一起使用时,我注意到当 NHibernate 试图检索下一个身份编号时,我遇到了一个异常(下面连同代码一起给出)。
这似乎是因为新的 SQLite 连接正在执行当前事务的自动登记。据我所知,SQLite 只支持单写事务,但应该支持多读,所以我很惊讶我得到一个读操作的数据库锁定异常。有没有人以这种方式使用带有事务范围的 SQLite。
如果我使用 NHibernate Transaction 而不是 TransactionScope,则相同的代码可以正常工作
代码块:
using (var scope = new TransactionScope())
{
var userRepository =
container.GetInstance<IUserRepository>();
var user = new User();
userRepository.SaveOrUpdate(user);
scope.Complete();
}
例外:
19:34:19,126 ERROR [ 7] IncrementGenerator [(null)]- could not get
increment value
System.Data.SQLite.SQLiteException: The database file is locked
database is locked
at System.Data.SQLite.SQLite3.Step(SQLiteStatement stmt)
at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
at System.Data.SQLite.SQLiteTransaction..ctor(SQLiteConnection
connection, Boolean deferredLock)
at System.Data.SQLite.SQLiteConnection.BeginTransaction(Boolean
deferredLock)
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 NHibernate.Connection.DriverConnectionProvider.GetConnection()
at NHibernate.Id.IncrementGenerator.GetNext(ISessionImplementor
session)
19:34:20,063 ERROR [ 7] ADOExceptionReporter [(null)]- The database
file is locked
database is locked