如何停止数据库文件被锁定的异常? [英] How do i stop the The database file is locked exception?

查看:65
本文介绍了如何停止数据库文件被锁定的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用sqlite的多线程应用程序。当两个线程尝试一次更新数据库时,我得到异常

I have a multithreaded app that uses sqlite. When two threads try to update the db at once i get the exception

Additional information: The database file is locked

认为它会在几毫秒内重试。我的查询不复杂。最复杂的(经常发生)是更新,选择,运行琐碎的代码更新/删除,提交。为什么会引发异常?我如何才能使其在引发异常之前重试几次?

I thought it would retry in a few milliseconds. My querys arent complex. The most complex one (which happens frequently) is update, select, run trivial code update/delete, commit. Why does it throw the exception? How can i make it retry a few times before throwing an exception?

推荐答案

SQLite对于访问而言不是线程安全的为什么会出现此错误消息。

SQLite isn't thread safe for access, which is why you get this error message.

每当您进行更新时,都应该同步对数据库的访问(创建一个对象,并锁定它)。这将导致第二个线程阻塞并等待,直到第一个线程的更新自动完成。

You should synchronize the access to the database (create an object, and "lock" it) whenever you go to update. This will cause the second thread to block and wait until the first thread's update finishes automatically.

这篇关于如何停止数据库文件被锁定的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆