android SQLite 事物處理

android SQLite 對事物處理的支持提供了4個方法。

public void beginTransaction()

Begins a transaction in EXCLUSIVE 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.

 

Here is the standard idiom for transactions:

   db.beginTransaction();//開啓事物
   try {
     //批量處理
     ...
     db.setTransactionSuccessful();//事物處理成功
   } finally {
     db.endTransaction();//結束事物
   }


 

此外還提供其他類似方法:
public void beginTransactionNonExclusive() //Begins a transaction in IMMEDIATE mode.
public void beginTransactionWithListener(SQLiteTransactionListener transactionListener)//Begins a transaction in EXCLUSIVE mode.
public void beginTransactionWithListenerNonExclusive(SQLiteTransactionListener transactionListener)//Begins a transaction in IMMEDIATE mode

發佈了44 篇原創文章 · 獲贊 115 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章