In the development of Android applications, transaction processing is very important when using a database.
First, the operation of the Android database (especially the write operation) is very slow, and packing all the operations into one transaction can greatly improve the processing speed.
The second is to ensure the consistency of the data so that all operations in one transaction execute successfully, or fail, or all operations are rolled back.
Tags: Android Business
[1]. Code [Java] Code Skip to [1] ?
| 1234567891011121314 |
mDatabase.beginTransaction();try{ //在这里执行多个数据库操作//执行过程中可能会抛出异常 mDatabase.setTransactionSuccessful(); //在setTransactionSuccessful和endTransaction之间不进行任何数据库操作}catch(Exception e){ //当数据库操作出现错误时,需要捕获异常,结束事务mDatabase.endTransaction(); throwe; } //当所有操作执行完成后结束一个事务 mDatabase.endTransaction(); } |
Android Database Transaction operations