2010-11-30 39 views
4

我有數據庫繁重的操作發生,它爲我的數據庫增加了大約10,000條記錄。由於這可能需要很長時間,因此使用交易會更好。在Android中讀取未提交的事務

db.startTransaction(); 
.... 
do write operations. 
.... 
db.setTransactionSuccessful(); 
db.endTransaction(); 

現在,我有交易裏面的一些讀操作,並且由於插入不承諾直到endTransaction,這些記錄不牽強。我聽說過一些稱爲事務隔離級別的東西,它使我們能夠讀取骯髒的(未提交的)記錄。任何想法如何做到這一點?

+0

事務可以嵌套。這裏有什麼問題? – Pentium10 2010-11-30 13:45:32

+0

問題是我插入的內容在我提交之前不是「可讀」的。因爲讀取只發生在實際數據庫中,而不是髒插入。我想.. – Codevalley 2010-11-30 13:47:51

回答

1

我發現,在默認情況下,你將能夠讀取它們尚未提交的記錄。雖然如果您使用SQliteOpenHelper,則此操作將失敗。這是因爲SQliteOpenHelper給出了2個單獨處理(讀取&寫入),未提交的寫入將不能從另一個句柄讀取。 所以,如果你想讀無限記錄,使用SQLiteDatabase類。

2

你有沒有試過這種

// outer transaction 
db.startTransaction(); 
.... 

    //roll out a new transaction 
    db.startTransaction(); 
    .... 
    do write operations. 
    .... 
    db.setTransactionSuccessful();// <-- you do commint inside of this transaction 

    // you can read data from here on from the previous committed transaction 

.... 
db.setTransactionSuccessful(); 
db.endTransaction(); 
3

我會建議打電話的SQLite的SQL語句編譯:

database.execSQL("PRAGMA read_uncommitted = true;"); 

該聲明說,查詢將使用READ_UNCOMMITTED代替READ_SERIALIZABLE事務隔離級別(默認的SQLite的模式) 。

查找例如here