Android的 - SQLite的+共享preferences,2线程同时进行读/写? [英] Android -- SQLite + SharedPreferences, 2 Threads Simultaneous Read/Write?
问题描述
我有一个程序(实际的应用程序和一个BroadcastReceiver)可能可能尝试连接到和修改SQLite数据库和共享preferences文件的两个部分。
1)我知道你可以建立多个连接到一个SQLite数据库。我也读了SQLite的可以锁定的数据库,当它试图修改或从数据库(UPDATE,INSERT,等等)读,让我怎么妥善处理,其中2个线程试图修改的情况下,在/读取单个SQLite数据库同一时间?
现在,我有这个code片段在我的数据库连接code /等级:
私人SQLiteDatabase MyDatabase的;
公共布尔插入(ContentValues columnValueMap)抛出异常
{
长期结果为0;
INT企图= 0;
做
{
如果(myDatabase.isDbLockedByOtherThreads()及!&安培;!myDatabase.isDbLockedByCurrentThread())
{
同步(MyDatabase的)
{
结果= myDatabase.insertOrThrow(TABLE_NAME,TEXT,columnValueMap);
如果(结果== 0 ||结果== -1)
{
返回false;
}
返回true;
}
}
其他
{
视频下载(50);
}
尝试++;
}而(尝试< = 5);
抛出新的异常(数据库被锁定!);
}
2)我也担心同时访问一个共享preferences文件(不要紧,哪个线程被访问的第一,我只是担心被抛出的错误)?
感谢。
建议更改
公共布尔插入(ContentValues columnValueMap)抛出异常
{
长期结果为0;
同步(MyDatabase的)
{
结果= myDatabase.insertOrThrow(TABLE_NAME,SMS_TEXT,columnValueMap);
如果(结果== 0 ||结果== -1)
{
返回false;
}
返回true;
}
}
首先,数据库已经建成的锁定支持,并且它默认打开。如果你想成为安全,请拨打 SQLiteOpenDatabase.setLockingEnabled(真)
。
至于访问共享preferences,我就封装了所有访问共享preferences(读,写),因为全部使用同步的小功能。 (确保它们的原子)。
I have two parts of a program (the actual app and a BroadcastReceiver) that could possibly try to connect to and modify a SQLite database and a SharedPreferences file.
1) I know you can make multiple connections to a single SQLite database. I also read that SQLite can "lock" the database when it attempts to modify or read from a database (UPDATE, INSERT, etc), so how do I properly handle the case where 2 threads try to modify/read a single SQLite database at the same time?
Right now, I have have this code snippet in my database connection code/class:
private SQLiteDatabase myDatabase;
public boolean insert(ContentValues columnValueMap) throws Exception
{
long result = 0;
int attempt =0;
do
{
if(!myDatabase.isDbLockedByOtherThreads() && !myDatabase.isDbLockedByCurrentThread())
{
synchronized (myDatabase)
{
result=myDatabase.insertOrThrow(TABLE_NAME, TEXT, columnValueMap);
if(result ==0 || result == -1)
{
return false;
}
return true;
}
}
else
{
Thread.sleep(50);
}
attempt++;
}while(attempt<=5);
throw new Exception("Database Locked!");
}
2) Do I also have to worry about simultaneous access for a SharedPreferences file (It doesn't matter which thread gets access first, I'm just worried about errors being thrown)?
Thanks.
[EDIT] Proposed change
public boolean insert(ContentValues columnValueMap) throws Exception
{
long result = 0;
synchronized (myDatabase)
{
result=myDatabase.insertOrThrow(TABLE_NAME, SMS_TEXT, columnValueMap);
if(result ==0 || result == -1)
{
return false;
}
return true;
}
}
First of all, databases have built in locking support, and it's turned on by default. If you want to be safe, call SQLiteOpenDatabase.setLockingEnabled(true)
.
As for access to the shared preferences, I would encapsulate all access to SharedPreferences (read and write) in small functions that all use synchronized. (Be sure that they are atomic).
这篇关于Android的 - SQLite的+共享preferences,2线程同时进行读/写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!