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

本文是小编为大家收集整理的关于HTC Desire中的数据库锁定问题的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

如果我去特定UI,我需要列出另一个table B如果背景的数据 操作正在执行它将生成数据库锁定的异常.我有两个数据库 在两个不同的表上并行地完成操作.

它在三星GT15801中正常工作.但是HTC愿望它将生成锁定的数据库 错误.

HTC Desire - 插入过程需要91秒.

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

推荐答案

尝试使用一个 sqlitedatabasehelper 并使其单个实例.之后,在完成操作后不要关闭sqlitedatabase实例.

您可以在数据库上实现锁定,以查看此

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

当您启动具有许多并发操作的数据库操作时,您必须使用

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

和在数据库上完成操作后,您可以使用

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

但如果您在交易之间给出错误,则不会设置database.setTransactionSuccessful();,以便交易将回滚.

还可以在错误时检查当前数据库是否在事务中,如果返回true,则在返回true,则在交易中您不在事务

也可以通过调用

检查当前数据库是否已锁定或不锁定
database.isDbLockedByCurrentThread();
database.isDbLockedByOtherThreads();

这将返回布尔值.

如果您可以通过

同时尝试读取和编写数据库,可以设置是否要锁定数据库.
database.setLockingEnabled(boolean);

弃用了上面的删除方法,所以请不要使用.

本文地址:https://www.itbaoku.cn/post/102392.html

问题描述

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.