Reading uncommitted transactions in Android
I have database heavy ope开发者_运维技巧rations happening, which adds around 10,000 records to my Database. Since this could take a very long time, it would be better to use transactions.
db.startTransaction();
....
do write operations.
....
db.setTransactionSuccessful();
db.endTransaction();
Now, I have some read operations inside the transactions, and since the inserts are not committed until endTransaction, those records are not fetched. I have heard about something called Transaction Isolation Levels, which enables us to read dirty (uncommitted) records as well. Any idea how to do this?
I would advise call SQLite's SQL statement pragma:
database.execSQL("PRAGMA read_uncommitted = true;");
This statement says that queries will use transaction isolation level READ_UNCOMMITTED instead of READ_SERIALIZABLE (default SQLite's mode).
Look for instance here
Have you tried this
// 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();
I found that by default you will be able to read records which are yet to be committed. Though this fails if you are using SQliteOpenHelper
. This is because SQliteOpenHelper
gives 2 separate handled (read& write) and uncommitted writes won't be available for read from the other handle.
So, if you want to read uncommited records, use SQLiteDatabase
class.
精彩评论