当我执行以下代码时
public async Task<ObservableCollection<CommentModel>> GetTypeWiseComment(int refId, int commentType)
{
try
{
var conn = _dbOperations.GetSyncConnection(DbConnectionType.UserDbConnetion);
var sqlCommand = new SQLiteCommand(conn)
{
CommandText = "bit complex sqlite query"
};
List<CommentModel> commentList = null;
Task commentListTask =
Task.Factory.StartNew(() => commentList = sqlCommand.ExecuteQuery<CommentModel>().ToList());
await commentListTask;
var commentsList = new ObservableCollection<CommentModel>(commentList);
return commentsList;
}
catch (Exception)
{
throw;
}
finally
{
GC.Collect();
}
}
有时我会收到以下异常
Message: database is locked
InnerException: N/A
StackTrace: at SQLite.SQLite3.Prepare2(IntPtr db, String query)
at SQLite.SQLiteCommand.Prepare()
at SQLite.SQLiteCommand.<ExecuteDeferredQuery>d__12<com.IronOne.BoardPACWinAppBO.Meeting.MeetingModel>.MoveNext()
at System.Collections.Generic.List<System.Diagnostics.Tracing.FieldMetadata>..ctor(Collections.Generic.IEnumerable<System.Diagnostics.Tracing.FieldMetadata> collection)
at BoardPACWinApp!<BaseAddress>+0xaa36ca
at com.IronOne.BoardPACWinAppDAO.Comments.CommentsDAO.<>c__DisplayClass4_0.<GetCommentTypeWiseComment>b__0()
at SharedLibrary!<BaseAddress>+0x38ec7b
at SharedLibrary!<BaseAddress>+0x4978cc
谁能指出我的代码出了什么问题?
后台还有另一个同步过程,有时它包含大量记录,执行可能需要10秒钟以上。如果上述代码恰好在同步写入数据库的同时执行,那么它可能会阻止读取,对吗?
如果是这样,当另一个进程写入数据库时,如何从SQLite读取?
谢谢。
答案 0 :(得分:0)
@Mark Benningfield提到启用WAL模式几乎解决了我的问题。但是,还有一个问题在我的应用程序上创建了很多SQLite连接,因此我通过创建一个处理数据库连接的Singleton模块来解决了这个问题。
如果遇到类似问题,请发表评论并询问是否需要更多信息。谢谢。