sqlite数据库锁定错误
我正在使用后端 sqlite 数据库开发 C# 应用程序。在我的应用程序中使用了多线程概念。所有线程都会调用下面提到的代码。那么它会抛出错误,因为数据库被锁定。
lock (localLockHandle)
{
SQLiteCommand cmd = conn.CreateCommand();
cmd.CommandText = sqlExpr;
int ireturn = cmd.ExecuteNonQuery();
return ireturn;
}
有什么办法可以摆脱这个数据库锁定错误。我在每个进程后打开和关闭连接。甚至有时它会抛出这个锁定错误。请给我一个解决方案,因为这对我来说非常重要。
谢谢
I am developing c# application with a backend sqlite db. In my application mutithreaded concepts are being used. All the threads will call the below mentioned code. then it will throw error as database is locked.
lock (localLockHandle)
{
SQLiteCommand cmd = conn.CreateCommand();
cmd.CommandText = sqlExpr;
int ireturn = cmd.ExecuteNonQuery();
return ireturn;
}
Is there any way to get rid from this database lock error. i am opening and closing the connection after each process. even sometimes it throws this lock error. please give me a solution as it is very critical for me.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解您的代码片段,那么您正在本地范围内的对象上使用
lock
。这意味着其他线程将锁定不同的对象。尝试定义一个全局锁,如下所示:
static readonly object DatabaseLock = new object()
,然后在任何访问数据库的地方使用
lock(DatabaseLock)
。If I understand your code snippet correctly, you are using
lock
on an object in the local scope. This would mean that other threads wouldlock
on a different object.Try defining a global lock, like this:
static readonly object DatabaseLock = new object()
and then using
lock(DatabaseLock)
wherever you access the database.