0
我實現了ContentProvider bulkInsert()
方法在表中插入大量的行在事務中,但在此之後,我需要在不同的表中進行更新,我只想做到這一點如果批量插入已提交。這兩項行動必須是原子的,無論做還是沒做,我該怎麼做?ContentProvider bulkInsert嵌套事務,插入和更新需要
這是bulkInsert方法:
@Override
public int bulkInsert(Uri uri, ContentValues[] values){
int nrInserted = 0;
String TABLE;
int uriType = mUriMatcher.match(uri);
switch (uriType){
case FEEDS:
TABLE = FeedsProviderContract.TABLE_NAME;
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
SQLiteDatabase db = mDbHelper.getWritableDatabase();
//Begin inner transaction
db.beginTransaction();
try {
for (ContentValues cv : values){
db.insertOrThrow(TABLE, null, cv);
nrInserted++;
}
db.setTransactionSuccessful();
getContext().getContentResolver().notifyChange(uri,null);
} catch (SQLException ex){
ex.printStackTrace();
}
finally {
db.endTransaction();
}
return nrInserted;
}
現在,當我把它我可以做這樣的外部事務?我猜這是行不通的。
...
//Begin outer transaction
getContentResolver().getContentProvider().bulkInsert(...);
//Update the other table
//End outer Transaction
你是說如果兩個操作都在同一個事務中,它不起作用? – DFord
@Dord沒有那樣,我不知道Sqlite事務的行爲,所以想象一下:內部事務提交但外部事務回滾,內部事務是否知道和回滾呢?如果內部事務回滾外部事務也知道並回滾?這就是我所要求的,但是在搜索之後,我想我需要的是'ApplyBatch',因爲我在不同的表格中進行了CRUD操作。 –
爲什麼在外部交易中需要兩筆交易? – DFord