SQLite数据库抛出锁定的异常 [英] SQLite database throws locked exception

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

问题描述





我在我的项目中使用SQLite数据库,我尝试了一个包含三个更新语句的事务,而执行此事务有时候我正面临着提交称为数据库的事务时异常被锁定。请检查一段代码:



Hi,

I am using SQLite database in my project, i tried a transaction which contains three update statements, while executing this transaction some times i am facing an exception while committing the transaction called data base is locked. Please check the piece of code:

string connectionString = GetConnectionString();

            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();

                SQLiteTransaction transaction = connection.BeginTransaction();
                SQLiteCommand newCommand = connection.CreateCommand();

                try
                {
                    newCommand.CommandText = string.Format("UPDATE {0} SET MainVersion = {1}, CurrentVersion = {2}", "Version", (serverCurrentVersion - 1), (serverCurrentVersion + 1));
                    newCommand.ExecuteNonQuery();

                    newCommand.CommandText = string.Format("UPDATE {0} SET AppVersion = {1} WHERE AppVersion = {2}", "QueryTable", serverCurrentVersion + 1, serverCurrentVersion);
                    newCommand.ExecuteNonQuery();

                    newCommand.CommandText = string.Format("UPDATE {0} SET AppVersion = {1} WHERE AppVersion = {2}", "IndexQueryTable", serverCurrentVersion + 1, serverCurrentVersion);
                    newCommand.ExecuteNonQuery();

                    transaction.Commit();
                    status = true;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                }
                finally
                {
                    newCommand.Dispose();
                    transaction.Dispose();
                }
            }    





有人可以帮帮我。?



添加了代码块 - OriginalGriff [/ edit]



could anybody please help me out.?

[edit]Code block added - OriginalGriff[/edit]

推荐答案

快速浏览互联网说正常原因得到这个是在处理事务时,不同的代码访问数据库进行读取或写入 - 如果你间歇性地得到这个错误,那么这就有意义了。



您是否可以在另一个线程中从数据库中读取数据?或者您是否尝试将SqLite用于多用户?

如果它是您的线程,那么可能只是锁定会解决它?如果它是访问同一文件的不同应用程序,那么您有两个选项:捕获异常并重试多次,或者更改为专为多用户设计的基于服务器的数据库(SQL Server或MySql)。

我从来没有使用基于文件的DB(SqLite,Access)获得多用户访问的良好结果 - 它可以工作,但它通常比它的价值更麻烦。
A quick look at the internet says that the "normal" reason for getting this is that the database was accessed by different code for reading or writing while the transaction was being processed - and if you are getting this error intermittently, then then that would make sense.

Are you perhaps reading from the DB in a different thread? Or are you trying to use SqLite for multiuser?
If it's your threads, then perhaps just locking would solve it? If it's a different app accessing the same file, then you have two options: catch the exception and retry a number of times, or change to a server based DB (SQL Server, or MySql) which is designed for multiuser.
I've never had good results with "file based" DB's (SqLite, Access) for multiuser access - it works, but it's generally more hassle than it's worth.


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

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