HTC Desire中的数据库锁问题 [英] database lock issue in HTC Desire

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

问题描述

在我的应用程序中,一个服务是从服务器获取数据,并将其插入到表A



去特定的UI,我需要从另一个表B 列表数据如果后台
操作正在做,它将生成数据库锁定异常。我有两个数据库
操作在两个不同的表上并行完成。



在samsung gt15801中工作正常。但是htc希望它将生成数据库锁定的
错误。


HTC的愿望 - 插入过程需要91秒。 b
$ b

三星gt15801 - 插入过程需要21秒。



解决方案

p>尝试使用一个 SqliteDatabaseHelper 。之后在完成操作后不关闭SqliteDatabase实例 p>

您可以在数据库上实现锁定



http://www.sqlite.org/lockingv3.html



当您开始数据库操作,它有许多并发操作,那么你必须使用

  database.beginTransaction / *用于开始事务* / 

并且在完成数据库操作后可以使用

  database.setTransactionSuccessful(); 
database.endTransaction();

但是如果你在事务之间给出错误,那么不要设置数据库。 setTransactionSuccessful(); ,以便事务将被回滚。



也可以检查错误时是否当前数据库在事务或者不是 database.inTransaction(); 如果它返回true,那么你在事务中,否则你不在事务中



此外,您可以通过调用

检查当前数据库是否已锁定

  database.isDbLockedByCurrentThread 
database.isDbLockedByOtherThreads();

这将返回布尔值。



您可以设置是否要锁定数据库,如果多个线程尝试通过

同时读写您的数据库

  database.setLockingEnabled(boolean); 


上述删除的方法已弃用,请勿使用。


In my application one service is getting data from server and inserting it to table A.

If I go to particular UI, I need to list data from another table B if background operation is doing it will generate database locked exception. I got two database operation done in parallel each on two different table.

It is working fine in samsung gt15801. But htc desire it will generate database locked error.

HTC desire - insertion process takes 91 seconds.

Samsung gt15801 - insertion process takes 21 seconds.

解决方案

Try to use one SqliteDatabaseHelper and make it single instance. After that don't close the SqliteDatabase instance after complete your operations.

You can implement lock on database for that see this

http://www.sqlite.org/lockingv3.html

When you start a database operation which has many concurrent operation then you have to use

database.beginTransaction(); /* for start transaction */

and after completing operation on database you can use

database.setTransactionSuccessful();    
database.endTransaction();

But if you give error in between transaction then don't set database.setTransactionSuccessful(); so that the transaction will be rollback.

Also you can check at the time of error whether currently the database is in transaction or not by database.inTransaction(); if it return true then you are in transaction else you are not in transaction

Also You can check for whether current database has locked or not by calling

database.isDbLockedByCurrentThread();
database.isDbLockedByOtherThreads();

this will return Boolean.

Also you can set whether you want to lock your database if multiple threads are trying to read and write your database at the same time by

database.setLockingEnabled(boolean);

Above deleted methods are deprecated so please do not use.

这篇关于HTC Desire中的数据库锁问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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