Comments (3)
一、常见异常
1、SQLiteDatabaseLockedException: database is locked
- 原因:同时读,但是不支持同时写,并且写数据时也不可读
SQLite是文件级别的锁.SQLite3对于并发的处理机制是允许同一个进程的多个线程同时读取一个数据库,但是任何时刻只允许一个线程/进程写入数据库。在操行写操作时,数据库文件被琐定,此时任何其他读/写操作都被阻塞,如果阻塞超过5秒钟(默认是5秒,能过重新编译sqlite可以修改超时时间),就报"database is locked"错误。
简单来说,sqlite数据库支持不同的线程同时读取,但是在同一时间只能有一个线程在写入数据,此时有其他线程写入或者读取数据库就会报“database is locked”异常。
我们和数据库只能有一条连接,即我们在调用dbHelper.getWritableDatabase()返回的是同一个对象,也就意味着我们在不同的线程需要用同一个dbHelper获取db。
- 解决办法:单例模式提供SQLiteOpenHelper
2、java.lang.IllegalStateException: attempt to re-open an already-closed object
-
原因:数据库只有一条共有的连接,如果别处关闭了共有连接,也就无法进行数据操作了。
-
解决办法:
+ 不关闭法:用完不关闭(调用db.close方法),连接一直开着,不过存在性能的影响,观察源码可知。
+ 计数管理法:在外部添加一个获取连接的管理类,并进行计数,每打开一个加1,用完减1,如果没有了则调用关闭连接,调用db.close。
参考资料
android sqlite数据库并发问题的详细描述和解决方案
from androidbox.
二、两个线程并发“写操作”
1、测试程序:
public static void test( Context context) {
final ServerConfigDao serverConfigDao = ServerConfigDao.getInstance(context);
// 线程1 修改值为7
new Thread(new Runnable() {
@Override
public void run() {
all(serverConfigDao,1);
currentDbServerConfigEntity.setType(7);
serverConfigDao.update(currentDbServerConfigEntity);
all(serverConfigDao,1);
}
}).start();
// 线程2 修改值为8
new Thread(new Runnable() {
@Override
public void run() {
all(serverConfigDao,2);
currentDbServerConfigEntity.setType(8);
serverConfigDao.update(currentDbServerConfigEntity);
all(serverConfigDao,2);
}
}).start()
}
// 打印结果
private static void all(ServerConfigDao serverConfigDao,int test) {
List<DBServerConfigEntity> dnsServerConfigList = serverConfigDao.selectAll();
LogUtil.w(TAG, "zjf dao test: "+test + " "+Thread.currentThread()+" >>> " + Arrays.toString(dnsServerConfigList.toArray()));
}
2、测试结果
线程2写操作之后执行了线程1的写操作 ,所以最终是线程1的操作的结果
zjf dao test: 1 Thread[Thread-1103,5,main] >>> 1
zjf dao test: 2 Thread[Thread-1104,5,main] >>> 1
zjf dao test: 2 Thread[Thread-1104,5,main] >>> 7
zjf dao test: 1 Thread[Thread-1103,5,main] >>> 7
线程1写操作之后执行了线程2的写操作 ,所以最终是线程2的操作的结果
zjf dao test: 1 Thread[Thread-1118,5,main] >>> 1
zjf dao test: 2 Thread[Thread-1119,5,main] >>> 1
zjf dao test: 2 Thread[Thread-1119,5,main] >>> 8
zjf dao test: 1 Thread[Thread-1118,5,main] >>> 8
3、测试结论
多线程并发情况下,根据线程时间片随机分配,无法保证写操作的执行顺序。
from androidbox.
三、SQLite事务
AndroidSQLite的事务机制,保障了数据修改的准确性,避免出现脏数据。通过beginTransaction开启一个事务,然后执行自己的数据库操作语句,如果顺利执行完,就会执行setTransactionSuccessful,此时设置了一个标志位 mTransactionStack.mMarkedSuccessful = true。最后执行endTransaction,会进行前面标志位的判断,如果为true表明此次操作没有问题。就执行“COMMIT”提交数据,否则执行“ROLLBACK”,将数据回滚到本次事务操作之前的状态。
from androidbox.
Related Issues (20)
- Toast Show的时候出现 BadTokenException: Unable to add window -- token
- java.lang.IllegalStateException: Fragment already added HOT 2
- ANR: "main" prio=5 tid=1 WaitingForGcToComplete HOT 2
- Exception: The specified message queue synchronization barrier token has not been posted or has already been removed.
- ExceptionInInitializerError HOT 2
- 为什么Receiver不允许进行BindService
- Fragment生命周期
- 《Android应用性能优化》 HOT 1
- 内存优化 HOT 1
- Native闪退 HOT 1
- java.lang.IllegalStateException: The specified child already has a parent.
- RxJava线程 HOT 1
- Service启动成功onCreate和onStartCmmand都不回调
- CursorWindowAllocationException HOT 1
- SharedPreferences解析
- Component class com.huawei.hms.support.api.push.PushEventReceiver does not exist in com.xtc.watch HOT 2
- SQLite查询操作源码分析:从 IllegalArgumentException:the bind value at index 2 is null 说起
- startActivityForResult的坑
- java.lang.IllegalArgumentException: pointerIndex out of range HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from androidbox.