Android事务 IMMEDIATE与EXCLUSIVE模式
来源:互联网 发布:mac美国官网价格 编辑:程序博客网 时间:2024/06/05 23:47
事务是数据库保证数据唯一性和一致性的技术,对于数据库一个或一组写操作要保证是一个原子操作就需要使用事务,android使用事务的常见形式如下:
SQLiteDatabase db = null;... db.beginTransaction();try { db.setTransactionSuccessful(); ...} finally { db.endTransaction();}
那么db.beginTransaction是一个什么操作? 我们来看下SQLiteDatabase的源码:
/** * Begins a transaction in EXCLUSIVE mode. * <p> * Transactions can be nested. * When the outer transaction is ended all of * the work done in that transaction and all of the nested transactions will be committed or * rolled back. The changes will be rolled back if any transaction is ended without being * marked as clean (by calling setTransactionSuccessful). Otherwise they will be committed. */ public void beginTransaction() { beginTransaction(null /* transactionStatusCallback */, true); }/** * Begins a transaction in IMMEDIATE mode. *Transactions can be nested. When * the outer transaction is ended all of the work done in that transaction * and all of the nested transactions will be committed or rolled back. The * changes will be rolled back if any transaction is ended without being * marked as clean (by calling setTransactionSuccessful). Otherwise they * will be committed. */ public void beginTransactionNonExclusive() { beginTransaction(null /* transactionStatusCallback */, false); }从注释中可以看到beginTransaction的调用使用EXCLUSIVE mode, beginTransactionNonExclusive使用IMMEDIATE mode,以上两个方法都是调用SQLiteDatabase的私有方法beginTransaction,两个方法不同之处在于第二个实参true|false, 这个私有方法源码:
private void beginTransaction(SQLiteTransactionListener transactionListener, boolean exclusive) { verifyDbIsOpen(); lockForced(BEGIN_SQL); boolean ok = false; try { ... // This thread didn't already have the lock, so begin a database // transaction now. if (exclusive && mConnectionPool == null) { execSQL("BEGIN EXCLUSIVE;"); } else { execSQL("BEGIN IMMEDIATE;"); } ... } finally { if (!ok) { // beginTransaction is called before the try block so we must release the lock in // the case of failure. unlockForced(); } } }当形参exclusive为true并且mConnectionPool==null是执行:execSQL("BEGIN EXCLUSIVE;"); false执行execSQL("BEGIN IMMEDIATE;");
BEGIN EXCLUSIVE:当前事务在没有结束之前任何android中的其他线程或进程都无法对数据库进行读写操作。
BEGIN IMMEDIATE:确保android中其他线程或者进程之间读取数据不能修改数据库。
为什么需要判断mConnectionPool==null这个条件,如果当mConnectionPool!=null 表示调用了enableWriteAheadLogging,也就是使用了WAL MODE。 使用WAL模式是能够提高并发性,读与写互不阻塞,而执行BEGIN EXCLUSIVE却降低了并发,互相矛盾,所以当以上两个条件都成立的情况下执行BEGIN EXCLUSIVE。
关于WAL模式会在另外一个blog讲解。
- Android事务 IMMEDIATE与EXCLUSIVE模式
- InnoDB事务模式与锁定
- 排它式AQS(EXCLUSIVE模式)
- Hibernate的事务模式与反模式
- 事务的生命周期与事务模式(autocommit)
- Mysql InnoDB 事务模式与锁定
- A.Q.S源码分析(exclusive模式下加锁)
- HDU 1305 Immediate Decodability(与1671类似)
- android sqlite事务理解与处理
- Oracle EXCLUSIVE MODE与SHARE MODE的区别
- 事务与分布式事务
- InnoDB 中文参考手册 --- 8 InnoDB 事务模式与锁定
- InnoDB中文参考手册---8InnoDB事务模式与锁定
- Android与MVC 模式
- 代理模式与Android
- 工作流模式详解之流程控制模式(4)——Exclusive Choice
- 工作流模式详解之流程控制模式(4)——Exclusive Choice
- Exclusive or
- Oracle数据库rman环境配置详解
- Struts的html标签与普通html标签的区别之button
- 程序猿更需要有全局观的高度思考问题
- 单例设计模式
- SQL Server并发问题表分区解决方案
- Android事务 IMMEDIATE与EXCLUSIVE模式
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions(素数筛的简单应用)
- rails 中的respond_to 对不同的文件要求给予相应
- 获取 Linux命令源代码
- 短小精悍的线性时间素数筛法(转)
- uva 10305 - Ordering Tasks
- dl.google.com,dl-ssl.google.com连不上的解决办法
- UVa350 Pseudo-Random Numbers
- 链接对网站自身权重的影响